Adam Brave Posted January 16, 2013 Share Posted January 16, 2013 (edited) 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 January 16, 2013 by Adam Brave Link to comment Share on other sites More sharing options...
thescientist Posted January 16, 2013 Share Posted January 16, 2013 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 More sharing options...
Adam Brave Posted January 16, 2013 Author Share Posted January 16, 2013 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? Link to comment Share on other sites More sharing options...
thescientist Posted January 16, 2013 Share Posted January 16, 2013 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 More sharing options...
justsomeguy Posted January 16, 2013 Share Posted January 16, 2013 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 More sharing options...
Adam Brave Posted January 16, 2013 Author Share Posted January 16, 2013 which one? have you looked up either of those? what specific questions do you have? you can start right here on w3schoolshttp://www.w3schools...php/php_get.asphttp://www.w3schools...hp_sessions.asp I saw those links but how can I apply the Sessions to my case? Link to comment Share on other sites More sharing options...
jeffman Posted January 16, 2013 Share Posted January 16, 2013 (edited) 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 January 16, 2013 by Deirdre's Dad Link to comment Share on other sites More sharing options...
Adam Brave Posted January 17, 2013 Author Share Posted January 17, 2013 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 More sharing options...
thescientist Posted January 17, 2013 Share Posted January 17, 2013 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 More sharing options...
jeffman Posted January 17, 2013 Share Posted January 17, 2013 (edited) 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 January 17, 2013 by Deirdre's Dad Link to comment Share on other sites More sharing options...
Adam Brave Posted January 17, 2013 Author Share Posted January 17, 2013 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 More sharing options...
jeffman Posted January 17, 2013 Share Posted January 17, 2013 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); Link to comment Share on other sites More sharing options...
justsomeguy Posted January 17, 2013 Share Posted January 17, 2013 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 More sharing options...
jeffman Posted January 17, 2013 Share Posted January 17, 2013 How'd I miss that? Link to comment Share on other sites More sharing options...
Adam Brave Posted January 17, 2013 Author Share Posted January 17, 2013 (edited) 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 January 17, 2013 by Adam Brave Link to comment Share on other sites More sharing options...
justsomeguy Posted January 17, 2013 Share Posted January 17, 2013 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 More sharing options...
Adam Brave Posted January 17, 2013 Author Share Posted January 17, 2013 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 + '"}; Link to comment Share on other sites More sharing options...
justsomeguy Posted January 17, 2013 Share Posted January 17, 2013 Use json_decode then, check post 5. Link to comment Share on other sites More sharing options...
Adam Brave Posted January 17, 2013 Author Share Posted January 17, 2013 With the decode: <?php$dayphp = $_GET['xpto'];$obj = json_decode($dayphp);echo $dayphp;?> My output is: "object(stdClass)#1 (1) { ["jday"]=> string(2) "24" } " =\ Link to comment Share on other sites More sharing options...
justsomeguy Posted January 17, 2013 Share Posted January 17, 2013 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 More sharing options...
Adam Brave Posted January 17, 2013 Author Share Posted January 17, 2013 But with the true parameter what I get is: " array(1) { ["jday"]=> string(2) "24" } " Link to comment Share on other sites More sharing options...
justsomeguy Posted January 17, 2013 Share Posted January 17, 2013 Yes, that's what I said. Now it's an associative array, now it would be $obj['jday'] instead of $obj->jday. Link to comment Share on other sites More sharing options...
Adam Brave Posted January 18, 2013 Author Share Posted January 18, 2013 Tkz man, it works Link to comment Share on other sites More sharing options...
Adam Brave Posted January 18, 2013 Author Share Posted January 18, 2013 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 Link to comment Share on other sites More sharing options...
Adam Brave Posted January 18, 2013 Author Share Posted January 18, 2013 Solved: myJSON = '{ "jday" : "' + day + '", "jmonth" : "' + month + '", "jyear" : "' + year + '" }'; Tkz for the help Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now