Well,
Unless you are needing multiple selections from the select field, it is uneccessary to loop it. Use the loop if you are using this attribute:
<select name = \"doctornames\" multiple=\"multiple\" size=\"4\">
Doing it this way would allow you to toggle multiple selections, thereby making it neccessary to have a loop, which you also had some errors in:
Code:
foreach ($HTTP_POST_VARS['doctornames'] as $key => $value) {
$doctor = $HTTP_POST_VARS['doctornames'][$key];
} // snip snip
The counter variable goes in its own array, and use use the $key, $value to keep in line with the selections made by the user.
Code:
<?php
global $HTTP_POST_VARS; global $doctor;
Host = "***";
$DBName = "***";
$TableName = "***";
$Link = mysql_connect ($Host, '****', '****');
/*
Making a database connection is resource intensive, you should do this only once at very beginning of a script, and definitely not within a loop
*/
if ($Link==false) {
echo mysql_errno().": ".mysql_error()."<BR>\n";
}
mysql_select_db ($DBName, $Link);
foreach ($HTTP_POST_VARS['doctornames'] as $key => $value) {
# I don't understand the point of this:
# $doctor = $HTTP_POST_VARS['doctornames[$x]'];
if (isset($HTTP_POST_VARS['mybutton'])) {
echo "1.You clicked the button\n";
echo "2.You selected ". $HTTP_POST_VARS['doctornames'][$key] . " from the select field.<br>\n";
$Query = "INSERT INTO $TableName VALUES ('{$HTTP_POST_VARS['doctornames'][$key]}')";
if (mysql_query ($Query, $Link)) {
print "The query was successfully executed!<BR>\n";
} else {
print "The query could not be executed!<BR>\n";
echo mysql_errno().": ".mysql_error()."<BR>\n";
}
mysql_close($Link);
} else {
echo "you must have come here from somewhere else.\n";
}
?>
If you did in fact want multiple <select> choices that's how it would be done. You were trying to assign the $HTTP_POST_VARS value to a regular global, which is fine, but you didn't treat the $doctors variable as an array. And not to mention were trying to insert the counter in the string.
For a regular <select> field, without the multiple="multiple" attribute it would be exactly the same as accessing data from a text field. Just a single name, value pair - no sub array.
hth,
: )
Rich
:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::