I've searching in the web for like 1 week and found like 15 questions asking the same thing as me, but I don't see to find the correct solution.
The problem is simple. I have a HTML table in one page and I want to pass this table thru AJAX(Jquery) and execute a php script, but I don't see where I'm doing wrong, here's all the code, I can post more if needed.
ajax by now(the real problem here is the array definition?):
$(function(){
$('#nice_button').on('click', function(e){
// Using the core $.ajax() method
$.ajax({
// The URL for the request
url: "ajax_insert_suba_def.php",
var data2 = [{
value1 : [1,2,3,4,5],
value2 : [5,4,3,2,1]
}];
// The data to send (will be converted to a query string)
data: { 'data': data2 },
// Whether this is a POST or GET request
type: "POST",
// The type of data we expect back
// dataType : "json",
// Code to run regardless of success or failure
complete: function( xhr, status ) {
alert( "The request is complete!" );
}
});
});
});
With this HTML:
<form method="post" name="my_form">
<button id="nice_button" type="button" name="btn_go" class="btn btn-success btn-lg"> Insert into DB</button>
</form>
With some basic PHP: (the functions are ok ... I also need to know how to properly 'get' those 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 INTO test_json (test_text) VALUES('" . $val_1 . "'); ";
$result = $my_sqli->query($query);
$my_sqli->close();
return "ok";
?>
By now the error i'm getting is this:
SyntaxError: missing : after property id
(in var data2= ... line)
Thanks for help!