1

I have a javascript code that sends a data to my php file...

a sample value of this is:

array(
   array('story_name'=>'mystory', 'priority'=>1),
   array('story_name'=>'mystory2', 'priority'=>2)
)

so my question is how can i convert this string into the same format of php array so that i can append this to my queries?

3
  • 5
    is there a reason for using this format instead of a standard one? (like JSON) Commented Nov 30, 2011 at 10:46
  • You can not send array into php via JS. Only string in some format (like JSON for example). Commented Nov 30, 2011 at 10:49
  • yeah thats a string not an actual array.. i did use escape(thestringArray) to send it to my php file Commented Nov 30, 2011 at 10:53

4 Answers 4

3

Use JSON:

echo json_encode($yourarray);
echo json_decode($_GET['fromjs']);
Sign up to request clarification or add additional context in comments.

3 Comments

You could serialize() then urlencode() it but json_encode is better in every way (smaller, more readable, more portable...)
the problem is those arrays is just a string and not an array structure, still json can do that?
Convert it to an array. Why using a string anyway here... Is there a reason for it has to be a string there? I makes no sense to me ;) Work with array structure in js and convert it to a JSON string for interaction with PHP.
1

The javascript array values can be passed to a php file using the following method

Step 1:

Let us consider we have a js array as

<script language=javascript>
scriptAr = new Array();
scriptAr[0] = "one";
scriptAr[1] = "two";
scriptAr[2] = "three";
<script>

Step 2:

Now we will create a hidden form field as follows
<form action="phpArrayTest.php" method=post name=test onSubmit=setValue()>
<input name=arv type=hidden>
<input type=submit>
</form>

Here what we have done is, when the submit is called we first do some work ("onSubmit=setValue()") by using the onSubmit method. The onSubmit method will invoke setValue() function defined by us. After that the action will take place and stringTokens.php will be called.

Step 3: Here we define the setValue method. The method will convert the array periviously defined in to a string and then set it to the hidden field.

<script language=javascript>    

function setValue()
{
var arv = scriptAr.toString();
// This line converts js array to String document.test.arv.value=arv;
// This sets the string to the hidden form field. }
</script>

Step 4: In the php file the string will be split back into array.

From http://www.hscripts.com/tutorials/php/jsArrayToPHP.php

Comments

1

Are you sure you want to send native php code to the server? That can really cause some major security holes. It's much better to send data in a much more php friendly format.

mystory,1|mystory,2

Then you can just use the explode() method to convert the data into arrays

Others have suggested using JSON. You would format your above data as follows in JSON

[       //Used to indicate sequential array i.e. $myarray[0]
    {   //Used to indicate associative array i.e. $myarray["story_name"]
        story_name: "mystory",
        priority: 1
    },
    {
        story_name: "mystory2",
        priority: 2
    }
]

Comments

1

You can use eval:

$arr = eval($data);

Note that this could result in security problems since eval() executes any PHP code from a string. You should use this with cauntion.

You also need to add a return to your code.

More information on eval() can be found here: http://php.net/manual/en/function.eval.php

The better way would be to send JSON to your php script like this:

In javascript:

var json = array2json(YOUR_JS_ARRAY);

Send this to your PHP script and there decode this to a PHP array like this:

json_decode($_POST['data']);

See http://php.net/manual/en/function.json-decode.php for more information on JSON decode. See http://en.wikipedia.org/wiki/JSON for more information on the JavaScript Object Notation (JSON).

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.