javascript - Js, Ajax, Json - How not to insert "Array" into db from autocomplete -
im building little script automatically fill few input fields based on autocomplete of first input field.
i have script working, when hit submit button seems ok, when db see inserted, fields inserted "array" instead of supposed inserted. on autocomplete, can see field being field right info, guess php doesn't understand is, , inserts "array" instead.
any ideas how fix it?
my form:
<form action="job_post.php" method="post"> <div class="form-group has-success col-md-3"> <label class="control-label" for="inputsuccess1">first name: </label> <input type='text' id='firstname' name='firstname[]'/> </div> <div class="form-group has-success col-md-3"> <label class="control-label" for="inputsuccess1">last name: </label> <input type='text' id='lastname' name='lastname[]'/> </div> <div class="form-group has-success col-md-3"> <label class="control-label" for="inputsuccess1">age: </label> <input type='text' id='age' name='age[]'/> </div> <div class="form-group has-success col-md-3"> <label class="control-label" for="inputsuccess1">company name: </label> <input type='text' id='coname' name='coname[]'/> </div> <input type="submit"> </form>
my job_post.php
<?php require_once 'db_connect.php'; $firstname=$_post['firstname']; $lastname=$_post['lastname']; $age=$_post['age']; $coname=$_post['coname']; $qry=mysql_query("insert jobs (firstname, lastname, age, coname) values ('$firstname', '$lastname', '$age', '$coname')", $con); if(!$qry) { mysql_close($con); die("query failed: ". mysql_error()); } else { mysql_close($con); header("location:index.php"); } mysqli_close($con); ?>
my js:
$('#firstname').autocomplete({ source: function( request, response ) { $.ajax({ url : 'ajax.php', datatype: "json", data: { name_startswith: request.term, type: 'firstname', row_num : 1 }, success: function( data ) { response( $.map( data, function( item ) { var code = item.split("|"); return { label: code[0], value: code[0], data : item } })); } }); }, autofocus: true, minlength: 2, select: function( event, ui ) { var names = ui.item.data.split("|"); console.log(names[1], names[2], names[3]); $('#lastname').val(names[1]); $('#age').val(names[2]); $('#coname').val(names[3]); } });
my ajax:
<?php /* site : http:www.smarttutorials.net author :muni */ require_once 'db_connect.php'; if($_get['type'] == 'firstname'){ $row_num = $_get['row_num']; $result = mysql_query("select firstname, lastname, age, coname jobs firstname '".strtoupper($_get['name_startswith'])."%'"); $data = array(); while ($row = mysql_fetch_array($result)) { $name = $row['firstname'].'|'.$row['lastname'].'|'.$row['age'].'|'.$row['coname'].'|'.$row_num; array_push($data, $name); } echo json_encode($data); } ?>
it pull data, im looking insert db submit button.
any highly appreciated.
updated info
then place variables in query have now.
page redone:
<?php require_once 'db_connect.php'; $firstname = $_post["firstname"][0]; $coname = $_post["coname"][0]; /* * last name , age not in array, removed them query * need place them in array if wish add them */ $qry=mysql_query("insert jobs (firstname, coname) values ('$firstname', '$coname')", $con); if(!$qry) { mysql_close($con); die("query failed: ". mysql_error()); } else { mysql_close($con); header("location: index.php"); } ?>
updated again, misread array.
Comments
Post a Comment