Jump to content

Passing arrays between php files


Adam Brave

Recommended Posts

Hi, can I pass an array from one php file to another php file like this: First file:

function NewPage(Day, Month, Year){xpto[0] = Day;xpto[1] = Month;xpto[2] = Year;location.href='new.php?xpto';}

Second file (new.php):

<?php$dayphp = $_GET[xpto[0]];$monthphp = $_GET[xpto[1]];$yearphp = $_GET[xpto[2]]; ?>

I have tried but just got an error like this "Parse error: syntax error, unexpected '[', expecting ']' in C:\Program Files\EasyPHP-12.1\www\new.php on line 13" Can I do something similar?

Edited by Adam Brave
Link to comment
Share on other sites

no. you'll just have pass them as individual params in the query string. another option is to use $_SESSION. You can save pretty much any data type in a SESSION member, since it's just an array.

Link to comment
Share on other sites

which one? have you looked up either of those? what specific questions do you have? you can start right here on w3schoolshttp://www.w3schools.com/php/php_get.asphttp://www.w3schools.com/php/php_sessions.asp

Link to comment
Share on other sites

You can JSON encode the array in Javascript, send the encoded array as a querystring variable, and use json_decode in PHP to turn it back into an array. https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/JSON/stringifyhttp://www.php.net/manual/en/function.json-decode.php

Link to comment
Share on other sites

I saw those links but how can I apply the Sessions to my case?
You can't. Confusion arose because your first post talks about passing data from one PHP file to another. That's not what you're doing. You're sending data from JavaScript running in the browser to a PHP script. Your option is to send data in a query string, or as POST data from a form, or to use AJAX. Your first idea was fine. What you didn't realize was that in the URL 'new.php?xpto', xpto is no longer a variable. It's just a string. What you need instead is a string that contains all the data. JSON is one technique that will work. Edited by Deirdre's Dad
Link to comment
Share on other sites

You can't. Confusion arose because your first post talks about passing data from one PHP file to another. That's not what you're doing. You're sending data from JavaScript running in the browser to a PHP script. Your option is to send data in a query string, or as POST data from a form, or to use AJAX. Your first idea was fine. What you didn't realize was that in the URL 'new.php?xpto', xpto is no longer a variable. It's just a string. What you need instead is a string that contains all the data. JSON is one technique that will work.
Hmm ok, I wil use the JSON object but from what I see I dont understand how can I use the value of the JSON in a second file. Can you help me?
Link to comment
Share on other sites

Hmm ok, I wil use the JSON object but from what I see I dont understand how can I use the value of the JSON in a second file. Can you help me?
You can JSON encode the array in Javascript, send the encoded array as a querystring variable, and use json_decode in PHP to turn it back into an array. https://developer.mo.../JSON/stringifyhttp://www.php.net/m...json-decode.php
did you read JSG's links? It's a fairly straightforward process. As he said, JSON stringify the array you're trying to send in Javascript, (the first link) and then json_decode it in PHP (the second link). Try it, give it a shot, etc. Post back with what you've tried and any questions/problems/errors you have.
Link to comment
Share on other sites

Note that to send to retrieve the data in $_GET['xpto', you will have to change your location something like this:

location.href = 'new.php?xpto=' + string;

Where string is the stringified JSON data.

Edited by Deirdre's Dad
Link to comment
Share on other sites

Well..here is what I have done. On the first file I have:

text += "<td align=center><span id=sp" + aa + " onClick='NewPage(" + aa + ")'></span> </td>";...function NewPage(day){myJSON = { "jday" : day}; // I'm trying to pass the function argument (day) to the "jday" field.location.href = 'new.php?xpto=' + myJSON;}

On the second file I have:

<?php$dayphp = $_GET[xpto];echo $dayphp;?>

My output is: "Notice: Use of undefined constant xpto - assumed 'xpto' in C:\Program Files\EasyPHP-12.1\www\new.php on line 13[object Object] ,, " Help..lol

Link to comment
Share on other sites

myJSON in your Javascript code is an object, not a string. So when you try to print the object in the URL it prints the text "[object Object]". You can only send string values on the querystring. That's why I suggested using JSON.stringify to automatically convert an array or object to a string. If you want to do it manually then you need to make it a string, not an object:

myJSON = '{"jday":"' + day + '"}';

You also need to make sure all special characters get escaped correctly: location.href = 'new.php?xpto=' + encodeURIComponent(myJSON);

Link to comment
Share on other sites

With this:

function NewPage(day){myJSON = '{"jday":"' + day + '"}';location.href = 'new.php?xpto=' + encodeURIComponent(myJSON);}....<?php$dayphp = $_GET['xpto'];var_dump($dayphp);?> 

My output is: " string(13) "{"jday":"24"}" ,, " But I just want the number 24. How do I get rid of the rest? Tkz for the help guys :)

Edited by Adam Brave
Link to comment
Share on other sites

If you only have a single value then there's no reason to send an entire JSON structure, just send that one value. You said you wanted to send an entire array though, so this is how to do it. On the PHP side you use json_decode to turn that string back into an object or array, look at the link I posted above.

Link to comment
Share on other sites

That's right, that's an object. $obj is actually the object, not $dayphp. $dayphp is the JSON string. You can pass true as the second parameter to json_decode if you want an associative array instead of a generic object. Otherwise, you can access the day using $obj->jday.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...