gerrydewar Posted March 15, 2006 Share Posted March 15, 2006 I've been working on this problem for 3 days now and i'm slightly stressed. Never mind that though. Here is what is happening. I have an array of ints that need to be passed from one page to another. The array is called $source. I know that the array has values in it because i can use a foreach loop on the first page to echo my array variables. However, it would appear that when i look inside my array after it has been passed it is empty. Here is my code for the destination page: <?php if(isset($_POST['source'])){ $ints = NULL; foreach($_POST['source'] as $key=>$value){ $ints .="$value, "; } $source = TRUE; }else{ $ints = 0; echo '<p>The array is empty</p>'; } if($source) { echo"Show array $ints"; } ?> </body> </html>Here is my form action for the source page:<form name ="sourcepage" action="handle_source.php"method="post">Can anyone tell from looking at this what might be wrong? Any help would be gratefully appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/5042-cant-pass-an-array-variable-between-pages/ Share on other sites More sharing options...
trq Posted March 15, 2006 Share Posted March 15, 2006 You'll need to [a href=\"http://php.net/serialize\" target=\"_blank\"]serialize[/a]() the array first, then [a href=\"http://php.net/unserialize\" target=\"_blank\"]unserialize[/a]() it on the next page. Quote Link to comment https://forums.phpfreaks.com/topic/5042-cant-pass-an-array-variable-between-pages/#findComment-17833 Share on other sites More sharing options...
obsidian Posted March 15, 2006 Share Posted March 15, 2006 if you're passing an array through a form, you need to serialize() it, then pass it as a string, then you can unserialize() it on the receiving page. this is much easier than trying to setup your form fields to allow arrays to be recognized correctly:here is my sending page:[code]<?php$ints = array(1,2,3,4,5);$passedValue = serialize($ints);?><form name='myForm' action='test.php' method='post'><input type='hidden' name='ints' value='<?= $passedValue ?>' /><input type='submit' name='submit' value='Pass It' /></form>[/code]and my receiving page:[code]<?phpif (isset($_POST['submit'])) { $ints = unserialize($_POST['ints']); echo "<pre>\n"; print_r($ints); echo "</pre>\n";}?>[/code]this should echo out all the values on the receiving page.hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/5042-cant-pass-an-array-variable-between-pages/#findComment-17834 Share on other sites More sharing options...
gerrydewar Posted March 15, 2006 Author Share Posted March 15, 2006 Obsidian, i used your code exactly just to test that my machine was able to send variables from page to page and it worked. When i try and use the concepts in my own code i just end up with a blank page. Could you have a look and see if you can see where i'm going wrong?*Source page*[MySql query here]if($result) {echo '<table align="left" cellspacing="10" cellpadding="10">';$testarray=array();$count = 0;while ($row=mysql_fetch_array($result,MYSQL_NUM)){$id = $row[0];$testarray[$count] = $id;[Table stuff on screen.]$count++;}//whileecho '</table>';}else{ //else no result echo'<p>failed</p>'; }$passedValue = serialize($testarray);mysql_close();?><form name ="myForm" action="test.php"method="post"><input type='hidden' name='ints' value='<?=$passedValue?>'/><input type="submit" name="submit"value="Pass it"></form></body></html>*Receiving page code*<?phpif(isset($_POST['submit'])){$ints = unserialize($_POST['testarray']);echo "<pre>\n";print_r($ints);echo "</pre>\n";}?></body></html> Quote Link to comment https://forums.phpfreaks.com/topic/5042-cant-pass-an-array-variable-between-pages/#findComment-17841 Share on other sites More sharing options...
gerrydewar Posted March 15, 2006 Author Share Posted March 15, 2006 I would like to add another bit of the jigsaw. If i put in a foreach loop outside of my while loop to check the array has contents then i get the array contents displayed on my source page. For example:foreach($testarray as $key =>$value) {echo $value;}This produces a list of 10 numbers across the screen which i'm assuming means the array contains 10 items. So why on earth does the array appear to be empty when i pass it to the next page? On the line below the foreach loop i have:$passedValue = serialize($testarray);Its gone from having 10 values to nothing in one line. WHY? For the sake of my sanity help please. Quote Link to comment https://forums.phpfreaks.com/topic/5042-cant-pass-an-array-variable-between-pages/#findComment-17883 Share on other sites More sharing options...
kenrbnsn Posted March 15, 2006 Share Posted March 15, 2006 An easier way to see what's in an array is:[code]<?php echo '<pre>' . print_r($testarray,true) . '</pre>'; ?>[/code]On the sending page, do a "show source" after the page is displayed. Post what is shown for the form.Ken Quote Link to comment https://forums.phpfreaks.com/topic/5042-cant-pass-an-array-variable-between-pages/#findComment-17885 Share on other sites More sharing options...
gerrydewar Posted March 15, 2006 Author Share Posted March 15, 2006 [!--quoteo(post=355424:date=Mar 15 2006, 06:08 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Mar 15 2006, 06:08 PM) [snapback]355424[/snapback][/div][div class=\'quotemain\'][!--quotec--]An easier way to see what's in an array is:[code]<?php echo '<pre>' . print_r($testarray,true) . '</pre>'; ?>[/code]On the sending page, do a "show source" after the page is displayed. Post what is shown for the form.Ken[/quote]Ok, tried the print_r on $testarray and there is definitely 10 values in the array which is reassuring. Not so good news however is what is shown in the page source of the sent page. All i get the html brackets for body, pre. There is no sign of the form!!?! Quote Link to comment https://forums.phpfreaks.com/topic/5042-cant-pass-an-array-variable-between-pages/#findComment-17890 Share on other sites More sharing options...
kenrbnsn Posted March 15, 2006 Share Posted March 15, 2006 Can you (re)post the current code you're testing? Be sure to put the [b][/code][/b] tag at the end and [b][code][/b] tag at the beginning.Ken Quote Link to comment https://forums.phpfreaks.com/topic/5042-cant-pass-an-array-variable-between-pages/#findComment-17921 Share on other sites More sharing options...
gerrydewar Posted March 15, 2006 Author Share Posted March 15, 2006 The source file[code]if($result) { echo '<table align="left" cellspacing="10" cellpadding="10">'; $ints=array(); $counter = 0; while ($row=mysql_fetch_array($result,MYSQL_NUM)){ $id = $row[0]; $ints[$counter] = $id; //A table with data// $counter++; }//while echo '</table>';}else{ //else no resultecho'<p>Test failed</p>';}echo '<pre>' . print_r($ints,true).'<pre>';$passedValue1 = serialize($ints);mysql_close();?><form name="testing"action="test1.php"method="post"><input type="hidden" name="ints" value="<?=$passedValue1?>"/><input type="submit" name="submit"value="Pass it"></form></body></html>[/code]The destination file[code]<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type"content="text/html;charset=encoding"/> <title>Test</title> </head> <body> <?php if(isset($_POST['submit'])){ $ints = unserialize($_POST['ints']); echo "<pre>\n"; print_r($ints); echo "</pre>\n"; } ?> </body> </html>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/5042-cant-pass-an-array-variable-between-pages/#findComment-17935 Share on other sites More sharing options...
kenrbnsn Posted March 15, 2006 Share Posted March 15, 2006 I took your code, modified it slighty to work without a database (I generate 10 random id's) and it ran fine. Here is the modified code:[code]<?php$result = true; // you didn't indicate where $result is gotten, so I just set it to true.if($result) { echo '<table align="left" cellspacing="10" cellpadding="10">'; $ints=array(); $counter = 0; while ($counter < 10){ $id = rand(10,100); $ints[] = $id; //A table with data// $counter++; }//while echo '</table>';}else{ //else no resultecho'<p>Test failed</p>';}echo '<pre>' . print_r($ints,true).'<pre>';$passedValue1 = serialize($ints);?><form name="testing"action="test1.php"method="post"><input type="hidden" name="ints" value="<?=$passedValue1?>"/><input type="submit" name="submit"value="Pass it" /></form>[/code]Doing a "show source" on the browser, show:[code]<body><table align="left" cellspacing="10" cellpadding="10"></table><pre>Array( [0] => 97 [1] => 87 [2] => 77 [3] => 24 [4] => 32 [5] => 55 [6] => 42 [7] => 84 [8] => 59 [9] => 81)<pre><form name="testing"action="test1.php"method="post"><input type="hidden" name="ints" value="a:10:{i:0;i:97;i:1;i:87;i:2;i:77;i:3;i:24;i:4;i:32;i:5;i:55;i:6;i:42;i:7;i:84;i:8;i:59;i:9;i:81;}"/><input type="submit" name="submit"value="Pass it" /></form></body>[/code]I didn't modify the source for test1.php at all. Everything worked fine.Ken Quote Link to comment https://forums.phpfreaks.com/topic/5042-cant-pass-an-array-variable-between-pages/#findComment-17946 Share on other sites More sharing options...
gerrydewar Posted March 15, 2006 Author Share Posted March 15, 2006 I did exactly as you said and it worked fine here too. Thing is the database connection is crucial. It seems to be a problem with my query code. Here is what i have above the while loop. [code]<?php//Connect to db.require_once ('../mysql_connect.php');$query = "SELECT question_id, question, answer, choice1, choice2, choice3 FROM questions WHERE subject_id = 1 ORDER BY RAND() LIMIT 10";$result = @mysql_query ($query); //Run queryif($result) {echo '<table align="left" cellspacing="10" cellpadding="10">';$ints=array();$counter = 0;while ($row=mysql_fetch_array($result,MYSQL_NUM)){[/code]Does this look ok? Quote Link to comment https://forums.phpfreaks.com/topic/5042-cant-pass-an-array-variable-between-pages/#findComment-17957 Share on other sites More sharing options...
kenrbnsn Posted March 15, 2006 Share Posted March 15, 2006 Let's change it slightly (although it does look ok)[code]<?php//Connect to db.require_once ('../mysql_connect.php');$query = "SELECT question_id, question, answer, choice1, choice2, choice3 FROM questions WHERE subject_id = 1 ORDER BY RAND() LIMIT 10";$result = mysql_query ($query) or die('Problem with query: ' . $query . '<br />' . mysql_error()); //Run query -- exit with a message if there is a problem//// don't need the "if" now, since if there is a problem, then script exits//echo '<table align="left" cellspacing="10" cellpadding="10">';$ints=array();while ($row=mysql_fetch_assoc($result)){ // get an associative array -- this will make determining where the data is coming from much easier $ints[] = $row['question_id']; //A table with data// }//while echo '</table>';//// remove the end of the "if" and the "else"//?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/5042-cant-pass-an-array-variable-between-pages/#findComment-17959 Share on other sites More sharing options...
gerrydewar Posted March 15, 2006 Author Share Posted March 15, 2006 Exactly the same result - blank page and no page source except the basic html stuff. This is so frustrating but i have to take this time to thankyou for all your help. If you can think of anything else then feel free. Quote Link to comment https://forums.phpfreaks.com/topic/5042-cant-pass-an-array-variable-between-pages/#findComment-17966 Share on other sites More sharing options...
kenrbnsn Posted March 15, 2006 Share Posted March 15, 2006 Are there any records in the database that meet your selection criteria?Add this line after the "$result = " line[code]<?php echo 'Number of records found: ' . mysql_num_rows($result) . "<br />\n"; ?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/5042-cant-pass-an-array-variable-between-pages/#findComment-17968 Share on other sites More sharing options...
gerrydewar Posted March 15, 2006 Author Share Posted March 15, 2006 Number of records found:10Which is correct because i am generating 10 questions from the database. Quote Link to comment https://forums.phpfreaks.com/topic/5042-cant-pass-an-array-variable-between-pages/#findComment-17972 Share on other sites More sharing options...
kenrbnsn Posted March 16, 2006 Share Posted March 16, 2006 There must be an error that isn't being reported. You host may have turned off error reporting.Put this line at the start of your script:[code]<?php error_reporting(E_ALL); ?>[/code]and see if any errors are reported. A blank screen usually is indicative of an error of some sort.Ken Quote Link to comment https://forums.phpfreaks.com/topic/5042-cant-pass-an-array-variable-between-pages/#findComment-18001 Share on other sites More sharing options...
gerrydewar Posted March 16, 2006 Author Share Posted March 16, 2006 Ok the source script is ok but i do have an error on the destination script:Notice:unserialize():Error at offset 10 of 14 bytes in ..........I tried to google it but didn't find much. One solution was to turn off the magic quotes in my php.ini file. I tried this but it didn't work so i put them back on again. Could this be a problem with my version of php? I'm using 4.3.11. Quote Link to comment https://forums.phpfreaks.com/topic/5042-cant-pass-an-array-variable-between-pages/#findComment-18031 Share on other sites More sharing options...
kenrbnsn Posted March 16, 2006 Share Posted March 16, 2006 At the top of the processing script, put this line to see what is being recieved:[code]<?php echo '<pre>' . print_r($_POST,true) . '</pre>'; ?>[/code]If you see any backslashes in the serialized array, change this line:[code]<?php $ints = unserialize($_POST['ints']); ?>[/code] to [code]<?php $ints = unserialize(stripslashes($_POST['ints'])); ?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/5042-cant-pass-an-array-variable-between-pages/#findComment-18036 Share on other sites More sharing options...
gerrydewar Posted March 16, 2006 Author Share Posted March 16, 2006 When i put in the print_r i get:[code]Array( [ints] => a:10:{i:0;s:2: [submit] => Pass it)[/code]The Error at offset is still there too. I added the stripslashes and it stayed the same. Quote Link to comment https://forums.phpfreaks.com/topic/5042-cant-pass-an-array-variable-between-pages/#findComment-18044 Share on other sites More sharing options...
kenrbnsn Posted March 16, 2006 Share Posted March 16, 2006 For some reason the entire serialized array isn't being passed. My first guess is that there is a double quote in the string which is terminating the data in the form.In the passing program, put this line before the form:[code]<?php echo '<pre>' . $passedValue1 . '</pre>'; ?>[/code]You could just change the <input> line to[code]<input type="hidden" name="ints" value="<? echo addslashes($passedValue1) ?>"/>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/5042-cant-pass-an-array-variable-between-pages/#findComment-18049 Share on other sites More sharing options...
sasa Posted March 16, 2006 Share Posted March 16, 2006 [!--quoteo(post=355591:date=Mar 16 2006, 06:19 PM:name=gmd06)--][div class=\'quotetop\']QUOTE(gmd06 @ Mar 16 2006, 06:19 PM) [snapback]355591[/snapback][/div][div class=\'quotemain\'][!--quotec--]When i put in the print_r i get:[code]Array( [ints] => a:10:{i:0;s:2: [submit] => Pass it)[/code]The Error at offset is still there too. I added the stripslashes and it stayed the same.[/quote]Change line [code]<input type="hidden" name="ints" value="<?=$passedValue1?>"/>[/code] in form to[code]<input type="hidden" name="ints" value='<?=$passedValue1?>'/>[/code] " -> 'and [code]$ints = unserialize($_POST['ints']);[/code] to [code]$ints = unserialize(stripslashes($_POST['ints']));[/code] Quote Link to comment https://forums.phpfreaks.com/topic/5042-cant-pass-an-array-variable-between-pages/#findComment-18054 Share on other sites More sharing options...
gerrydewar Posted March 16, 2006 Author Share Posted March 16, 2006 Ken tried what you said but it was still the same. offset error was still thereSasa, i tried what you said and the offset error has gone but i only get:[code]Array( [Ints] => [submit] =>)[/code]There is still no array info.Ken, meant to say that when i put in[code]<?php echo '<pre>' . $passedValue1 . '</pre>'; ?>[/code]i get a:10:{i:0;s:1:"1";i:1;s:2:"17";i:2;s:1:"3";i:3;s:1:"4";i:4;s:2:"13";i:5;s:1:"2";i:6;s:1:"5";i:7;s:1:"9";i:8;s:2:"11";i:9;s:2:"10";}across the screen. Quote Link to comment https://forums.phpfreaks.com/topic/5042-cant-pass-an-array-variable-between-pages/#findComment-18059 Share on other sites More sharing options...
kenrbnsn Posted March 16, 2006 Share Posted March 16, 2006 You'll notice that there are double quotes in the serialized string.Use the following line for the <input>:[code]<input type="hidden" name="ints" value="<?php echo htmlentities($passedValue1)?>"/>[/code]and leave the stripslashes() on the unserialize line.Ken Quote Link to comment https://forums.phpfreaks.com/topic/5042-cant-pass-an-array-variable-between-pages/#findComment-18075 Share on other sites More sharing options...
gerrydewar Posted March 16, 2006 Author Share Posted March 16, 2006 Guess what? It worked. I now have my array elements on screen. Thankyou to everyone that contributed, particularly Ken. I'm sure to have some more questions in the coming weeks. hopefully not as demanding as this one. Thankyou.I'll buy you all a cyber drink Quote Link to comment https://forums.phpfreaks.com/topic/5042-cant-pass-an-array-variable-between-pages/#findComment-18080 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.