Pass variable to another php page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
moussa854
Forum Newbie
Posts: 23
Joined: Thu Apr 30, 2009 4:10 pm

Pass variable to another php page

Post by moussa854 »

Hi I am new to PHP but learning. I am try to display query from database in one page and then pass selected variable to another page wih more ditails here is my code

page1.php:

Code: Select all

<form action="page2.php" method="post">
<?
$query=mysql_query("select * from guest_book order by question desc");
while($nt=mysql_fetch_array($query)){ 
$dtl=nl2br($nt['dtl']); // this will change  the line breaks to html line breaks
echo '<p><input type="checkbox" name="'.$nt['question'].'" value="'.$nt['question'].'">'.$nt['question'].'<br></p>';
echo '<input type="checkbox" name="checkboxes[]" value="' . $nt['question'] . '">';
echo "<hr />";
}
?>
<input type="submit" value="View Selected">
</form>
 
page2.php

Code: Select all

<?
$question2=$_POST['question'];
 
echo $question2;
?>
 
but it is not working :(
Last edited by moussa854 on Sun May 24, 2009 9:48 pm, edited 2 times in total.
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: Pass variable to another php page

Post by mikemike »

I'm not sure what exactly you're trying to do here.

What is the value of $nt['question']? I realise it's pulled from your database but unless you have a database entry named 'question2' you will never be able to access the data in the way you have tried. If you're trying to load in data and then work out what the user has pressed, perhaps to make some sort of questionairre then I suggest using HTML / PHP arrays, take the following:

Code: Select all

 
<?php
# Page one
 
// Start form
echo '<form action="page2.php" method="post">';
 
$query = mysql_query("SELECT * FROM guestbook ORDER BY question DESC") or die(mysql_error());
 
// Loop through
while($row = mysql_fetch_assoc($query)){
  echo '<p>'.nl2br($row['dtl']).'<br /><input type="checkbox" name="question[]" value="'.$row['question'].'" /></p>
}
 
// Close form
echo '</form>';
 
?>
 
 
And to process the code...

Code: Select all

 
<?php
# Page 2
// Print a formatted array
echo '<pre>'.print_r($_POST, true).'</pre>'; 
?>
 

Page one will run through your database and output the 'dtl' (which i assume is 'detail') along with a checkbox. Each checkbox is named 'question[]' which means it will populate an array named 'question', each checked checkbox will have it's own entry in that array with the value found in value="". You'll see what I mean when you view page2.

I'm not sure whether this is what you were after but your description wasn't too clear.

Mike
Last edited by Weirdan on Sun May 24, 2009 7:16 pm, edited 1 time in total.
Reason: php tags
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Pass variable to another php page

Post by califdon »

Your form shows action="select-view.php" but you are expecting to read values in page2.php. That won't work.
moussa854
Forum Newbie
Posts: 23
Joined: Thu Apr 30, 2009 4:10 pm

Re: Pass variable to another php page

Post by moussa854 »

Hi Mike,
this is acually close to what I want to do, however I have a database and it has a set of questions and multiple choice answers. In page1.php I want to show the questions only, and let the viewer chose from the set of questions to answer. So page1.php will have queston1 queston2 queston3 queston4 queston5 (showing the question only) the view will chose 2, 3 or 5 questions to answer, will press the button, will go to page2.php and will see queston2 queston3 queston5 with the choices.

I modified page1.php but still not working:

Code: Select all

<?php
# Page one
 
echo '<form action="page2.php" method="post">';
 
$query = mysql_query("SELECT * FROM guest_book ORDER BY question DESC") or die(mysql_error());
 
// Loop through
while($row = mysql_fetch_assoc($query)){
  echo '<p><input type="checkbox" name="question[]" value="'.$row['question'].'" />'.$row['question'].'<input type="hidden" name="A[]" value="'.$row['A'].'" /><input type="hidden" name="B[]" value="'.$row['B'].'" /></p>';
}
 echo'<input type="submit" value="View">';
echo '</form>';
?>
 
Last edited by Benjamin on Tue May 26, 2009 10:38 am, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: Pass variable to another php page

Post by mikemike »

Cool. You just need to pass the question ID over to page 2 really.

So in your loop:

Code: Select all

<?php
// This is inside your loop
echo '<p><input type="checkbox" name="question_id[]" value="'.$row['question_id'].'" />'.$row['dtl'].'</p>';
?>
That will pass an array full of question ID's that the user has chosen to page2.php

Hope this helps,
Mike
Last edited by Benjamin on Tue May 26, 2009 10:38 am, edited 1 time in total.
Reason: Changed code type from text to php.
moussa854
Forum Newbie
Posts: 23
Joined: Thu Apr 30, 2009 4:10 pm

Re: Pass variable to another php page

Post by moussa854 »

Well, my variables are $question, $A, $B, $C, $D, and $E. I do not have variables call 'question_id' nor 'dtl'. Your method passes the question but not the corresponding choices !!
Post Reply