Jump to content

Can't pass an array variable between pages


gerrydewar

Recommended Posts

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.
Link to comment
Share on other sites

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]
<?php
if (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
Link to comment
Share on other sites

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++;
}//while
echo '</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*

<?php

if(isset($_POST['submit'])){
$ints = unserialize($_POST['testarray']);
echo "<pre>\n";
print_r($ints);
echo "</pre>\n";
}
?>
</body>
</html>
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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
Link to comment
Share on other sites

[!--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!!?!
Link to comment
Share on other sites

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 result
echo'<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]

Link to comment
Share on other sites

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 result
echo'<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
Link to comment
Share on other sites

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 query

if($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?
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

[!--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]
Link to comment
Share on other sites

Ken tried what you said but it was still the same. offset error was still there

Sasa, 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.
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.