pull down menu

A

Anonymous

Guest
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:
<? $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";
	}		
?>

[Edited by Swirlee because it was breaking the forum layout.]
 
I'm not sure..

can you please tidy up your code? I don't read ....

Anyway, you can't do this:

Code:
$number_of_array[subject1]$number_of_array[subject2]etc....
you need to cocatenate variables like this $var1.$var2

Maybe you want to read this post:
http://www.php-forum.com/p/viewtopic.php?p=10513
 
Joan Garnet said:
Code:
$number_of_array[subject1]$number_of_array[subject2]etc....
you need to cocatenate variables like this $var1.$var2

Yes, definitely clean up your code -- there's no reason to have that much code (365 characters!) on one line. I try to keep my lines to 80 characters, if possible (though occasionally it isn't). Using string concatenation as Joan suggests will help a lot with this. Instead of this (which nobody could even pretend do be able to read at a glance):

Code:
echo "<td><form name="main" method="post" action="verify_attendance.php?us
ername1=$number_of_array[username1]&subject=$number_of_array[subject1]$number_o
f_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>";

Do this:

Code:
echo  '<td>' . "\n" .
      '<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'] .
      "">\n" .
      '<input type="submit" name="Submit" value="Submit" />' . "\n" .
      '</form>' . "\n" .
      '</td>" . "\n";

See? It's actually readable a) to us, b) to you 3 months from now, c) to other people looking at your PHP (if you choose to distribute the code), and, as a bonus, to you when you're looking at the resulting HTML in your browser. It really goes a long way.
 
There are tutorials out there where you can look up how to do it. Basic idea is a form with a drop down list and pass the variable chosen. For example:

Code:
<form method="post" action="passMenu.php">
                <select name="menuVar" id="menuVar">
                  <option selected>Choose a category...</option>
                  <option value="Advertising">Advertising</option>
                  <option value="Utilities">Utilities</option>
                  <option value="Video">Video</option>
                  <option value="Weddings">Weddings</option>
                  <option value="Women">Women</option>
                  <option value="Work at Home">Work at Home</option>
                  <option value="Writing">Writing</option>
                </select>
                <input name="submit" type="submit" value="Submit">
              </form>
 
what tryton 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.
 
If you want to use an array or whatever from a DB, apply a select statement like
select * from table_name where col_name lik '%variable%';

then check for the number of rows that are given out, if it is more than zero, then, that variable is there

then populate the list
using a select statement and feeding the contents with a loop[ into a select tag
:D

Hope that is what you want!!! :wink:
 
Back
Top