0

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)

5 Answers 5

1
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
}
Sign up to request clarification or add additional context in comments.

6 Comments

I have a doubt can u reslove it. I have a function in collect.php so I want array to be received In that function can that happen
You cannot directly execute the function from ajax.Instead of that you can do necessary checkins and call the function.
Can you expand it a bit for me .. Sry I could not get you
What didnt you get.?Whats makes you confused.
I am getting an Uncaught ReferenceError: array is not defined in console.
|
0

hope this will help you

data: {
'leftData': leftData,
'rigthData': rigthData 
}

and you can user this variable ('leftData', 'rigthData') on php page as php variable

2 Comments

Thanks @Bhupendra but i want those value to be fetched in a function called values in collect.php . How do I do that .. realy i am very poor at this any help would be grateful for me.
You can simply fetch this variable as php variable like $leftData abd $rightData,
0

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);

Comments

0

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 
          }
    });
 }

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.