我想用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>
我認爲這將有助於你削減代碼示例,以消除行,你肯定是不相關的這個特定的問題。 –
上面的部分是底部是整個頁面的情況下的相關部分,以防有人想看整個圖片。特別是<選項標記 –