Page 1 of 1
Pass variable to another php page
Posted: Sun May 24, 2009 5:57 pm
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

Re: Pass variable to another php page
Posted: Sun May 24, 2009 6:58 pm
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
Re: Pass variable to another php page
Posted: Sun May 24, 2009 8:40 pm
by califdon
Your form shows action="select-view.php" but you are expecting to read values in page2.php. That won't work.
Re: Pass variable to another php page
Posted: Mon May 25, 2009 5:10 am
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>';
?>
Re: Pass variable to another php page
Posted: Mon May 25, 2009 5:50 am
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
Re: Pass variable to another php page
Posted: Mon May 25, 2009 7:26 am
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 !!