Page 1 of 1
pull down menu
Posted: Sat Aug 23, 2003 10:20 am
by sguy
How to pass a value from one page to another? As below, if I using a pull down menu and I choose ‘Subject2’. If i click submit button, all values were sent to another page. How do I pass only the ‘Subject2’ values to another page??
something wrong with this code? how can i improve it?
Code: Select all
<? $Connect = mysql_connect("localhost","root","");
mysql_select_db("mw");
$result=mysql_query("select * from student where username1 = '$username1'");
$number_of_array = mysql_num_rows($result);
print"<TABLE CELLSPACING="0" CELLPADDING="0" BORDER="1">";
print "<TR><TD>Student ID</TD><TD>Programme</TD><TDSubject</TD><TD>Action</TD></TR>\n";
while ($number_of_array = mysql_fetch_array($result))
{
echo "<tr>\n";
echo "<TD>$number_of_array[username1]</TD>\n";
echo "<TD>$number_of_array[programme]</TD>\n";
echo "<TD><select name="subject"><option value=subject>$number_of_array[subject1]</option><option value=subject>$number_of_array[subject2]</option><option value=subject>$number_of_array[subject3]</option><option value=subject>$number_of_array[subject4]</option><option value=subject>$number_of_array[subject5]</option></select></TD>\n";
echo "<td><form name="main" method="post" action="verify_attendance.php?username1=$number_of_array[username1]&subject=$number_of_array[subject1]$number_of_array[subject2]$number_of_array[subject3]$number_of_array[subject4]$number_of_array[subject5]&programme=$number_of_array[programme]"><input type="submit" name="Submit" value="Submit"></form></td>";
echo "</tr>\n";
}
?>
Posted: Sat Aug 23, 2003 11:40 am
by patrikG
I suggest you create a form and use POST instead of GET (which you implicitely use).
There are tutorials
here,
here and many more
here.
Posted: Sat Aug 23, 2003 10:52 pm
by sguy
what you said was the value in drop down menu is fixed, but i want use array to check whether there has value in the database, if yes, show all the values in the drop down menu, because i can add the value to the drop down menu in another page.
Posted: Sun Aug 24, 2003 4:04 am
by patrikG
If I understand you correctly, you have the following scenario:
you have a couple of select-boxes. Upon submit, these values are get passed to the next page via GET (the POST you define as form-method gets bypassed and is redundant, along with the values in the select-boxes).
On this new page, you want another select-box to be populated with values you pull from the database based on the values in the $_POST-array?
That is at least what I read from the code above.
If that is what you intend:
Code: Select all
<?$Connect = mysql_connect("localhost","root","");
mysql_select_db("mw");
$result=mysql_query('select * from student where username1 = "'.$_POST["username1"].'"');
$number_of_array = mysql_num_rows($result);
print"<TABLE CELLSPACING="0" CELLPADDING="0" BORDER="1">";
print "<TR><TD>Student ID</TD><TD>Programme</TD><TDSubject</TD><TD>Action</TD></TR>\n";
while ($number_of_array = mysql_fetch_array($result))
{
echo "<tr>\n";
echo '<td><form name="main" method="post" action="verify_attendance.php"></td>';
// In order to use form-values, you need to define <form ...> before any html-tages
// for form-values. I've moved your <form...> tag to the top.
// On the page you submit to do a echo '<pre>'.print_f($_POST).'</pre>'; to have display the
// values this form has posted
echo '<TD>'.$number_of_array["username1"].'</TD>\n';
echo '<TD>'.$number_of_array["programme"].'</TD>\n';
echo '<TD><select name="subject">
<option value="'.$number_of_array["subject"].'">'.$number_of_array["subject"].'</option>
</select></TD>\n';
echo '<td><input type="submit" name="Submit" value="Submit"></form></td>';
echo '</tr>\n';
}
?>
This is not exactly the code you want, but it has the funcationality you want (if I understood you correctly). You won't need to modify much to get exactly what you wanted.
Posted: Sun Aug 24, 2003 9:59 am
by sguy
i tried to modify like this, but when i click submit, nothing happen, it can't pass to next page.
Code: Select all
<? $Connect = mysql_connect("localhost","root","");
mysql_select_db("mw");
$result=mysql_query("select * from student where username1 = '$username1'");
$number_of_array = mysql_num_rows($result);
print"<TABLE CELLSPACING="0" CELLPADDING="0" BORDER="1" align=center width="80%">";
print " <TR><TD>Student ID</TD><TD>Programme</TD><TD>Subject</TD><TD>Action</TD></TR>\n";
while ($number_of_array = mysql_fetch_array($result))
{
echo "<tr>\n";
"<td><form name="main" method="post" action="verify_attendance.php"></td>\n";
echo "<TD>$number_of_array[username1]</TD>\n";
echo "<TD>$number_of_array[programme]</TD>\n";
echo "<TD><select name="subject"><option value=$number_of_array[subject1]>$number_of_array[subject1]</option><option value=$number_of_array[subject2]>$number_of_array[subject2]</option><option value=$number_of_array[subject3]>$number_of_array[subject3]</option><option value=$number_of_array[subject4]>$number_of_array[subject4]</option><option value=$number_of_array[subject5]>$number_of_array[subject5]</option></select></TD>\n";
echo "<td><input type="submit" name="Submit" value="Submit"></td>";
echo "</tr>\n";
}
?>
Posted: Sun Aug 24, 2003 10:12 am
by patrikG
You are creating multiple forms by putting the <form>-tag in the while-loop.
sguy wrote:
Code: Select all
<? $Connect = mysql_connect("localhost","root","");
mysql_select_db("mw");
$result=mysql_query('select * from student where username1 = "'.$_POST["username1"].'"');
// use $_POST["username1"] in the query!
$number_of_array = mysql_num_rows($result);
print"<TABLE CELLSPACING="0" CELLPADDING="0" BORDER="1" align=center width="80%">";
print " <TR><TD>Student ID</TD><TD>Programme</TD><TD>Subject</TD><TD>Action</TD></TR>\n";
echo "<td><form name="main" method="post" action="verify_attendance.php"></td>\n";
while ($number_of_array = mysql_fetch_array($result))
{
echo "<tr>\n";
echo "<TD>$number_of_array[username1]</TD>\n";
echo "<TD>$number_of_array[programme]</TD>\n";
echo "<TD><select name="subject"><option value=$number_of_array[subject1]>$number_of_array[subject1]</option><option value=$number_of_array[subject2]>$number_of_array[subject2]</option><option value=$number_of_array[subject3]>$number_of_array[subject3]</option><option value=$number_of_array[subject4]>$number_of_array[subject4]</option><option value=$number_of_array[subject5]>$number_of_array[subject5]</option></select></TD>\n";
echo "<td> </td></tr>\n";
}
echo "<tr><td colspan=4><input type="submit" name="Submit" value="Submit"></td></tr>";
?>
Also, do note that it is recommended to use quotes for associative arrays, e.g. $number_of_array["subject5"] instead of just $number_of_array[subject5].
Posted: Sun Aug 24, 2003 10:34 am
by sguy
if i put double qoutes inside $number_of_array["subject5"], it shows the error message
Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in c:\phpweb/student_attendance.php on line 41
if don't put double qoutes, it shows the page, but can't pass to next page.
Posted: Sun Aug 24, 2003 10:43 am
by patrikG
Post your code, please.
Posted: Sun Aug 24, 2003 10:50 am
by sguy
Code: Select all
<? $Connect = mysql_connect("localhost","root","");
mysql_select_db("mw");
$result=mysql_query("select * from student where username1 = '$username1'");
$number_of_array = mysql_num_rows($result);
print"<TABLE CELLSPACING="0" CELLPADDING="0" BORDER="1" align=center width="80%">";
print " <TR><TD align=center height="30"><font face="Arial Narrow">Student ID</TD><TD>Programme</TD><TD>Subject</TD><TD>Action</TD></TR>\n";
while ($number_of_array = mysql_fetch_array($result))
{
echo "<tr>\n";
"<td><form name="main" method="post" action="verify_attendance.php"></td>\n";
echo "<TD align=center>$number_of_array[username1]</TD>\n";
echo "<TD align=center>$number_of_array[programme]</TD>\n";
echo "<TD align=center><select name="subject"><option value=$number_of_array[subject1]>$number_of_array[subject1]</option><option value=$number_of_array[subject2]>$number_of_array[subject2]</option><option value=$number_of_array[subject3]>$number_of_array[subject3]</option><option value=$number_of_array[subject4]>$number_of_array[subject4]</option><option value=$number_of_array[subject5]>$number_of_array[subject5]</option></select></TD>\n";
echo "<td align=center><input type="submit" name="Submit" value="Submit"></form></td>";
echo "</tr>\n";
}
?>
Posted: Sun Aug 24, 2003 10:55 am
by patrikG
Read my previous post which includes a code-example.
Posted: Sun Aug 24, 2003 11:07 am
by patrikG
Let me clarify: if you have a look at the generated source-code, you'll notice plenty of form-tags. That's unnecessary and causing the problem. Take the form-tag out of the while-loop.