Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

This question already has an answer here:

I am using codeIgniter and my js code is in my view.. and i want to pass the value in my controller..

var storage =[];

function something()
{

storage.push('the value');

}

now i want a better way to pass the storage array into my php. Thanks in advance

this code is not working because it is in the seperate folder

$.ajax({
    url: 'yourPHPFile.php',
    type: 'POST',
    data: {data:storage.toString()},
    success: function(result) {
            // handle your success
    },
    error: function() {
      // alert("error");
    }
});
share|improve this question

marked as duplicate by deceze javascript Jul 25 '14 at 8:27

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
You can use JSON for this purpose – Ali Gajani Jul 25 '14 at 6:08
1  
Can you elaborate on "push storage array into my php". It's rather broad and vague. – Darren Jul 25 '14 at 6:09
    
i know that but i don't know the syntax – CodeSlayer Jul 25 '14 at 6:09
    
don't mind the push..it's all working..the only problem is passing the array to php file – CodeSlayer Jul 25 '14 at 6:10
    
How do you want to use it in php? You need to supply more information. – Darren Jul 25 '14 at 6:10

Easiest way is to use jQuery.

You can do it by sending data to the server using $.ajax

$.ajax({
    url: 'yourPHPFile.php',
    type: 'POST',
    data: {data:storage.toString()},
    success: function(result) {
            // handle your success
    },
    error: function() {
      // alert("error");
    }
});

On server side, in your PHP code do,

$data = $_POST['data'];
echo $data;

$data will contain the value.

share|improve this answer

Use JQuery to post the data

$.ajax({ type: "POST",
             url: "Filename.php",
             data: storage,//no need to call JSON.stringify etc... jQ does this for you
             success: function(resopnse)
             {//check response: it's always good to check server output when developing...
                 }});

And in your PHP file

$array=json_decode($_POST['jsondata']);
share|improve this answer

Use JSON to write in javascript...

JSON.stringify(array)

and retrieve using...

$array=json_decode($_POST['jsondata']);
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.