我正在尝试使用 HTML 和 PHP 编写一个粘性选择框。我希望在按下提交后突出显示用户在提交之前输入的选择。我尝试了几种不同的方法,但似乎不起作用。这是我现在拥有的那条线。
<select name="selectBox[]" multiple="multiple" size="5">
<?php
$i=0;
while($temp=mysql_fetch_array($result,MYSQL_NUM))
{
//echo $temp[1];
?>
<option value="<?php echo $temp[1] ?>"
<?php if(isset($_POST[$temp[1]])) {echo "selected";} ?> >
<?php echo $temp[1] ?> </option>
<?php
$i++;
}
?>
</select>
我知道这是可行的,echo "selected"
因为我已经在选项标签中尝试过。
这是整个代码,选择标签中的选项标签略有不同,但它仍然不用担心。
<html>
<head><title>Business Registration</title></head>
<body>
<h1>
Business Registration
</h1>
<?php
$user="*********";
$DBserver="localhost";
$password="*********";
$myDatabase='nedwards';
$myTable1='categories';
$myTable2='businesses';
$myTable3='biz_categories';
//connect to the database server.
$con=mysql_connect($DBserver,$user,$password);
$con or die('Could not connect to MySQL: ' . mysql_error());//check connection
mysql_select_db($myDatabase) or die("Unable to select database");//select the databse we will be using
//check selection
$result = mysql_query("SELECT * FROM $myTable1");//gets the category table.... we will use the fetch_array and take the 2nd element of the array for select box
$submitted=$_POST['submitted'];
if($submitted)
{
$sql="INSERT INTO $myTable2 (name, address, city, telephone, url) VALUES ('$_POST[bis_name]', '$_POST[addr]', '$_POST[city]', '$_POST[tele]' , '$_POST[url]')";
mysql_query($sql, $con) or die('Error dude: ' .mysql_error());
// echo "$_POST[bis_name]";
// echo "$_POST[addr]";
// echo "$_POST[city]";
$chosenTitles=$_POST[selectBox];
//echo "$_POST[bis_name]";
foreach ($chosenTitles as $temp)//will run through each title chosen
{
//echo "$temp";//for testing
//get cat id
$catTitle2IDtemp=mysql_query("SELECT * FROM $myTable1 WHERE title='$temp'", $con);
$catTitle2ID=mysql_fetch_array($catTitle2IDtemp, MYSQL_NUM);
$catID=$catTitle2ID[0];//this is the category ID
//get biz id
$temp=$_POST[bis_name];
$bis_name2IDtemp=mysql_query("SELECT * FROM $myTable2 WHERE name='$temp'", $con);
$bis_name2ID=mysql_fetch_array($bis_name2IDtemp, MYSQL_NUM);
$bizId=$bis_name2ID[0];//this is the biz ID
mysql_query("INSERT INTO $myTable3 (business_id, category_id) VALUES ('$bizId', '$catID')");
}
}
?>
<table border="1">
<tr>
<td rowspan="5">
<form name="input" action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
<select name="selectBox[]" multiple="multiple" size="5">
<?php
$i=0;
while($temp=mysql_fetch_array($result,MYSQL_NUM))//gets each row of categroy table "$result" and takes second element of array $tempto select box
{
//echo $temp[1];
?>
<option value="<?php echo $temp[1] ?> " <?php if(in_array($temp[1], $chosenTitles)){echo "selected";} ?> > <?php echo $temp[1] ?> </option>
<?php
$i++;
}
?>
</select>
</td>
<td>Buisness Name</td>
<td> <input type="text" name="bis_name" value="<?php if($submitted==1){echo $_POST['bis_name'];}?>"/></td>
</tr>
<tr>
<td>Address</td>
<td><input type="text" name="addr" value="<?php if($submitted==1){echo $_POST['addr'];}?>" /></td>
</tr>
<tr>
<td>City</td>
<td><input type="text" name="city" value="<?php if($submitted==1){echo $_POST['city'];}?>"/></td>
</tr>
<tr>
<td>Telephone</td>
<td><input type="text" name="tele" value="<?php if($submitted==1){echo $_POST['tele'];}?>"/></td>
</tr>
<tr>
<td>URL</td> <td><input type="text" name="url" value="<?php if($submitted==1){echo $_POST['url'];}?>" /></td>
</tr>
<input type="hidden" name="submitted" value="1">
</table>
<input type="Submit" value="Add Business" />
</form>
<?php
$submitted="0";
$_POST['submitted']="0";
?>
</html>