Jump to content
Sign in to follow this  
Adam Brave

Passing arrays between php files

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

Share this post


Link to post
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.

Share this post


Link to post
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.
Can you give me an example of how do it?

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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?

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

You simply forgot to put xpto in 'quotation marks'. That's the first problem. The second problem is that you can't echo the contents of an object. Try var_dump($dayphp);

Share this post


Link to post
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);

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

I put here just the "day" element to simplify and get some result but in the end what I want to pass is something like that:

myJSON = {  "jday" : "' + day + '",                     "jmonth" : "' + month + '",                      "jyear" : "' + year + '"};

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Yes, that's what I said. Now it's an associative array, now it would be $obj['jday'] instead of $obj->jday.

Share this post


Link to post
Share on other sites

Well it works with just the jday but when I do this on the first file:

myJSON = {  "jday" : "' + day + '",                    "jmonth" : "' + month + '",                    "jyear" : "' + year + '"};

I can't see none of the three elements :(

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...