Web Hosting Talk







View Full Version : Help needed with dynamic PHP form passing more than one variables into next script


adlep
12-07-2002, 07:00 PM
Hello!
I do have a dynamic form script which generates the list of the tables (chapters) being present in my MySQL database.
It works just fine, but I have problem after enabling multivalue option in my script... How to modify my script so it can pass more than one variable into the next script? (User selects few tables instead of just one, so something like that
$chapter =$_POST[chname] will not work anymore because it can not hold more than one selection)
I was thinking about array but got lost...
Help.

Here is the script:

$counter = 0;
$table_list = mysql_list_tables($dbname); // lists tables
echo "or, just select chapters (amount of material) for the exam:";
print (" <p> ");
echo "<form method=\"post\" action=\"chapterlist.php\">"; // dynamic form code starts here!
echo "<select name= \"chname\" multiple>";

while ($row =mysql_fetch_row($table_list))
{
echo "<option name=\"$dbname\">";
echo $result = mysql_tablename($table_list, $counter);
echo "</option>";
$counter++;
}
echo "<input type=\"Submit\" Value=\"Submit\"></form>";

?>

Like I said most likely I would have to incorporate an array into this script...but how?

adlep
12-07-2002, 10:00 PM
Got it!
Explanation in just a second!!!

adlep
12-07-2002, 10:20 PM
$counter = 0;
$table_list = mysql_list_tables($dbname); // lists tables
echo "or, just select chapters (amount of material) for the exam:";
print (" <p> ");
echo "<form method=\"post\" action=\"chapterlist.php\">"; // dynamic form code starts here!
echo "<select name= \"chname[]\" multiple>"; //here is where magic happens, just declare the array here!

while ($row =mysql_fetch_row($table_list))
{
echo "<option name=\"$dbname\">";
echo $result = mysql_tablename($table_list, $counter);
echo "</option>";
$counter++;
}
echo "<input type=\"Submit\" Value=\"Submit\"></form>";

?>


and the target script -chapterlist.php:

<html>
<body>

<?php
$choice=$_REQUEST['chname']; // REQUEST seems to work as good as POST or GET
// also note that it knows that chname is an array
$i=0;
while ($i < count ($choice))
{
print $choice[$i]; // first version, it doesnt actually do anything beside listing user's selection
print ", "; // need to build into this loop MySQL queries and output, and links..
$i++;
}


?>
</body>
</html>

This is very simple script, but it works great... try it....
Regards,
Adam