Reputation: 482
How do I send jquery array to to php page .It to silly to ask this but I am new to jquery and php page and receive it on the other end.
function SendValuesToPage()
{
var leftData = JSON.Stringify(addedLeftValues);
var rightData = JSON.Stringify(addedRightValues);
// ajax code
}
And I tried it something like this
function SendValuesToPage()
{
var leftData = JSON.Stringify(addedLeftValues);
var rightData = JSON.Stringify(addedRightValues);
$.ajax({
url: "test/collect.php",
type: "GET",
data: {
'leftData[]': leftData,
'rigthData[]': rigthData
}
});
}
I am using php codeigniter
(MVC architecture)
Upvotes: 0
Views: 245
Reputation: 23
addedLeftValues and addedRightValues are string, you must parse it to json object.
$.ajax({
url: "test/collect.php",
type: "GET",
data: {
'leftData': JSON.parse(leftData),
'rigthData': JSON.parse(rigthData)
}
});
and php code
$leftData = $_GET['leftData'];
$leftData is a array.
Upvotes: 0
Reputation: 4506
Try adding datatype: json in your SendValuesToPage().
function SendValuesToPage()
{
var leftData = JSON.Stringify(addedLeftValues);
var rightData = JSON.Stringify(addedRightValues);
$.ajax({
url: "test/collect.php",
type: "GET",
dataType: "json",
data: {
'leftData[]': leftData,
'rigthData[]': rigthData
}
});
}
Upvotes: 0
Reputation: 11984
function SendValuesToPage() {
var leftData = JSON.stringify(addedLeftValues);
var rightData = JSON.stringify(addedRightValues);
$.ajax({
url: "test/collect.php",
type: "POST",
data: {
'leftData': leftData,
'rigthData': rigthData,
'func':'myFunc'
}
});
}
And in your collect.php
$arrleftData = json_decode($_POST['leftData']);
$arrrigthData = json_decode($_POST['rigthData']);
Update:
if(isset($_POST['func']) && !empty($_POST['func'])) {
$action = $_POST['func'];
switch($action) {
case 'myFunc' :
$arrArgs = array();
$arrArgs['leftData'] = json_decode($_POST['leftData']);
$arrArgs['rigthData'] = json_decode($_POST['rigthData']);
myFunc($arrArgs);
break;
case 'blah' : blah();break;
// ...etc...
}
}
function myFunc($arrArgs){
//Do something
}
Upvotes: 1
Reputation: 160833
Don't Stringify
the javascript object to string, or you need to decode them in your php side.
function SendValuesToPage(addedLeftValues, addedRightValues)
{
$.ajax({
url: "test/collect.php",
type: "GET",
data: {
'leftData': addedLeftValues,
'rigthData': addedRightValues
}
});
}
Then see what you get in your php file: var_dump($_GET);
Upvotes: 0
Reputation: 258
hope this will help you
data: {
'leftData': leftData,
'rigthData': rigthData
}
and you can user this variable ('leftData', 'rigthData') on php page as php variable
Upvotes: 0