我正在尝试使用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>
答案 0 :(得分:3)
你可以使用in_array php函数。
以下是一些指导您的示例代码。
<?
if(isset($_POST['submit']))
{
$selectBox = $_POST['selectBox'];
}
?>
<form name="input" action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
<select name="selectBox[]" multiple="multiple" size="5">
<option <? if(in_array('one',$selectBox)){ echo 'selected';}?> value="one">one</option>
<option <? if(in_array('two',$selectBox)){ echo 'selected';}?> value="two">two</option>
<option <? if(in_array('three',$selectBox)){ echo 'selected';}?> value="three">three</option>
<option <? if(in_array('four',$selectBox)){ echo 'selected';}?> value="four">four</option>
</select>
<input type="Submit" name="submit" value="Add Business"/>
</form>
答案 1 :(得分:0)
如果我正确地阅读
<option value="<?php echo $temp[1] ?>"
<?php if(isset($_POST[$temp[1]])) {echo "selected";} ?> >
<?php echo $temp[1] ?> </option>
产生
<option value="some value"selected> some value </option>
要指定应选择option
,您需要使用selected
属性:
<option value="some value" selected="selected"> some value </option>
试试这个:
<option value="<?php echo $temp[1] ?>"
<?php if(isset($_POST[$temp[1]])) {echo ' selected="selected"';} ?> >
<?php echo $temp[1] ?> </option>