pull down menu

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
User avatar
sguy
Forum Commoner
Posts: 61
Joined: Sun Aug 10, 2003 2:44 am

pull down menu

Post 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";
	}		
?>
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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.
User avatar
sguy
Forum Commoner
Posts: 61
Joined: Sun Aug 10, 2003 2:44 am

Post 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.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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.
User avatar
sguy
Forum Commoner
Posts: 61
Joined: Sun Aug 10, 2003 2:44 am

Post 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";
	}
?>
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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>&nbsp;</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].
User avatar
sguy
Forum Commoner
Posts: 61
Joined: Sun Aug 10, 2003 2:44 am

Post 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.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Post your code, please.
User avatar
sguy
Forum Commoner
Posts: 61
Joined: Sun Aug 10, 2003 2:44 am

Post 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";
	}
?>
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Read my previous post which includes a code-example.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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.
Post Reply