javascript - Passing values to PHP with JQuery AJAX not working, array confusion -
i've searching in web 1 week , found 15 questions asking same thing me, don't see find correct solution.
problem simple. have html table in 1 page , want pass table thru ajax(jquery) , execute php script, don't see i'm doing wrong, here's code, can post more if needed.
ajax now(the real problem here array definition?):
$(function(){ $('#nice_button').on('click', function(e){ // using core $.ajax() method $.ajax({ // url request url: "ajax_insert_suba_def.php", var data2 = [{ value1 : [1,2,3,4,5], value2 : [5,4,3,2,1] }]; // data send (will converted query string) data: { 'data': data2 }, // whether post or request type: "post", // type of data expect // datatype : "json", // code run regardless of success or failure complete: function( xhr, status ) { alert( "the request complete!" ); } }); }); }); with html:
<form method="post" name="my_form"> <button id="nice_button" type="button" name="btn_go" class="btn btn-success btn-lg"> insert db</button> </form> with basic php: (the functions ok ... need know how 'get' values)
<?php header("content-type: application/json"); require "includes/functions.php" ; $my_sqli = connect_to_mysql(); $data = $_post['data']; $val_1 = $data["value1"][0]; $val_2 = $data["value2"][0]; $query = "insert test_json (test_text) values('" . $val_1 . "'); "; $result = $my_sqli->query($query); $my_sqli->close(); return "ok"; ?> by error i'm getting this:
syntaxerror: missing : after property id (in var data2= ... line)
thanks help!
syntaxerror: missing : after property id
you have take data2 outside of ajax request, this:
$(function(){ $('#nice_button').on('click', function(e){ var data2 = [ { value1 : [1,2,3,4,5], value2 : [5,4,3,2,1] } ]; // using core $.ajax() method $.ajax({ // url request url: "ajax_insert_suba_def.php", // data send (will converted query string) data: { data: data2 }, // whether post or request type: "post", // type of data expect // datatype : "json", // code run regardless of success or failure complete: function( xhr, status ) { alert( "the request complete!" ); } }); }); }); i need know how 'get' values
this how can individual values:
<?php // code $data = $_post['data']; $val_1 = $data[0]['value1'][0]; $val_2 = $data[0]['value1'][1]; // on $val_5 = $data[0]['value1'][4]; $val_6 = $data[0]['value2'][0]; $val_7 = $data[0]['value2'][1]; // on $val_10 = $data[0]['value2'][4]; // code ?>
Comments
Post a Comment