javascript - Can't insert into database an autofill fields? -


it's first time asking here, i've been trying similar in other questions asked, , couldn't find it.

i have form zip code line(textbox) , state line(textbox), now, stateboxes auto-filled javascript entering valid zip code. form bit longer. show relevant code has been edited me, select menu before (and worked fine - data entered databse), , changed it, no select needed.

there css file, it's irrelevant (designing isn't issue)

so, here html code :

<html> <head>some content here</head> <body> <form method="post" action="process.php"> <div class="title"><h2>my form title</h2></div><br> <div align="center" class="element-name">                              </span> <span align="center" id="zipbox" class="namefirst"> <input type="text" class="medium" pattern="[0-9]*" name="col2" id="col2" maxlength="5" placeholder="type zip code" onkeypress='validate(event)'required/>                         </span> <span align="center" id="zipbox2" class="namelast"> <input type="text" class="medium" pattern="[0-9]*" name="col4" id="col4" maxlength="5" placeholder="type zip code" onkeypress='validate(event)'required/>                 </span></div> <div align="center" class="element-name"> <span class="required"></span> <span align="center" id="statebox" class="namefirst"> <input type="text" class="medium" name="col1" id="col1" placeholder="" required /> <label class="subtitle">from</label>                             </span> <span align="center" id="statebox2" class="namelast"> <input type="text" class="medium" name="col3" id="col3" placeholder="" required /> <label class="subtitle">to</label>                                </span></div> <p align="center"><input type="reset" value="clear"></p> </body> </html> 

some javescript !

<script src="//code.jquery.com/jquery-1.11.1.js"></script>     <script>     $(document).ready(function() {     $("#col2").keyup(function() {         var el = $(this);          if (el.val().length === 5) {             $.ajax({                 url: "http://zip.elevenbasetwo.com",                 cache: false,                 datatype: "json",                 type: "get",                 data: "zip=" + el.val(),                 success: function(result, success) {                     $("#city").val(result.city);                     $("#col1").val(result.state);                 }             });         }     }); });     </script>     <script>         $(document).ready(function() {     $("#col4").keyup(function() {         var el = $(this);          if (el.val().length === 5) {             $.ajax({                 url: "http://zip.elevenbasetwo.com",                 cache: false,                 datatype: "json",                 type: "get",                 data: "zip=" + el.val(),                 success: function(result, success) {                     $("#city2").val(result.city);                     $("#col3").val(result.state);                 }             });         }     }); });      </script> 

and php code process form : - it's 13 columns, know sure other values correct. - col0 represent date.

<?php require_once('recaptchalib.php');  $privatekey = "mycaptchakey";  $resp = recaptcha_check_answer ($privatekey,                                  $_server["remote_addr"],                                  $_post["recaptcha_challenge_field"],                                  $_post["recaptcha_response_field"]);  if (!$resp->is_valid) { // happens when captcha entered incorrectly    die ("the recaptcha wasn't entered correctly. go , try again." .         "(recaptcha said: " . $resp->error . ")");         $process = false;  } else {     // code here handle successful verification // code here handle successful verification  } define('const_server_timezone', 'edt'); define('const_server_dateformat', 'ymdhis'); $current_date = date("y-m-d h:i:s");   $col0 = $_post['col0']; $col1 = strtoupper($_post['col1']); $col2 = strtoupper($_post['col2']); $col3 = strtoupper($_post['col3']); $col4 = strtoupper($_post['col4']);   if ( isset($col1) && isset($col2) isset($col3) && isset($col4) && $error == false ) {   $process = true; } else {   $process = false; } $mode = "mysql";{  define ('db_user', 'uname'); define ('db_password', 'pass'); define ('db_host', 'host'); define ('db_name', 'dbname'); $dbc = @mysql_connect (db_host, db_user, db_password) or die('failure: ' . mysql_error() ); mysql_select_db(db_name) or die ('could not select database: ' . mysql_error() );  $query = "insert mytable values ('$current_date','$col1','$col2','$col3','$col4')"; $q = mysql_query($query);  if (!$q) {   exit("<p>mysql insertion failure.</p>"); } else {   mysql_close();   //for testing   //echo "<p>mysql insertion successful</p>";  }}    header( 'location: http://mywebsite.com/index.php' );  ?> 

i'm not sure if i'm doing right here mytable structure :

1 - sid - int(11) auto_increment 2 - col0 - date 3 - col1 - text utf8_unicode_ci 4 - col2 - text utf8_unicode_ci 5 - col3 - text utf8_unicode_ci 6 - col4 - text utf8_unicode_ci , on 12 columons. 

help please ! going wrong here ?

edit :

thank r3wt usefull information, there lot fix when comes php part of :)

ok, able fix insertion. missed critical value -

$query = "insert mytable values ('$current_date','$col1','$col2','$col3','$col4')"; 

should have been:

$query = "insert mytable values ('','$current_date','$col1','$col2','$col3','$col4')"; 

that's because form info going phpgrid table , had hidden column 'sid' automatically beeing filled.

i promise in next time prepare more knowledge :)

thanks again.

this waits until second has past after have finished typing in element, submits ajax request.

$(function(){      $("#col4").keyup(function() {         var col4val = $(this).val();         cleartimeout(timer);         timer = settimeout(function(){filllocation(col4val)}, 1000);     });      var timer = null;     function filllocation(value){         $.get('http://zip.elevenbasetwo.com', {zip: value} , function(data){             var result = json.parse(data);             $("#city2").val(result.city);             $("#col3").val(result.state);         });     } }); 

also, php code considered woefully insecure because using mysql.

also, noticed glaring error, missing , and operator between $col2 isset , $col3, check ajax guarantee returning 500 internal server error:

if ( isset($col1) && isset($col2) isset($col3) && isset($col4) && $error == false ) {   $process = true; } else {   $process = false; } 

also, query wrong. obvious copy , pasting things here. go read mysql manual on insert statements , go read on mysqli , pdo extensions php.

a valid mysql statement looks like:

insert mytable (column1,column2,column3) values ('val1','val2','val3') 

realizing this, construct statement in php so

$query = mysql_query("insert mytable (column1,column2,column3) values ('".$val1."','".$val2."','".$val3."')"); 

if continue use mysql site hacked, matter of time, since don't sanitize of data. please make smart choice , use mysqli or pdo interface database php.

as per request of dikei, i'm going introduce briefly prepared statements mysqli may learn use safe methods interacting database.

$mysqli = new mysqli('host','username','password','databasename'); $mysqli->set_charset("utf8");  $stmt = $mysqli->prepare("insert mytable (column1,column2,column3) values (?,?,?)");//bind variables in same order!  //s string, d double floating point integer, , unsigned int. $stmt->bind_param('sss',$col1,$col2,$col3); if($stmt->execute()) {     $row = $stmt->insert_id ?: null;     if(!empty($row))     {         //query success     }else{         //query failure     }     $stmt->close(); } $mysqli->close(); 

if need more info, provided broader example of working mysqli using object oriented approach here(its in part 2 of answer): login session destroyed after refresh


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -