文本框限于数组内容 [英] Text box limited to array contents
问题描述
我现在有一个下拉框,当其中一个选项被选中它会比回声 -
你最喜欢的车是(选购件)
我现在需要做的是改变这种做法,它是一个文本框,但用户只能输入数组中的选项之一,如果被选为另一个会说你不能有这样的作为选择
之一,它也将是能够输入在多个所以理论上它可以回声 -
你最喜欢的车是马自达,日产,雷诺!
下面是code现在我有,我有工作的下拉框。
<形式方法=邮报>
< DIV ID =下拉菜单>
< PHP
如果(使用isset($ _ POST ['汽车']))
{
$ mycar = $ _ POST ['汽车'];
}
其他
{
$ mycar =;
}
$数组1 =阵列('大众','雷诺','路虎');
呼应'<选择名称=车的onchange =this.form.submit()>';
的foreach($数组1为$汽车)
{&GT?;
<期权价值=< PHP的echo $汽车;>中< PHP如果($ mycar == $汽车)回声
选择='选择'; ?> >< PHP的echo $车; ?>< /选项>
< PHP
}
呼应'< /选择>
< / DIV>< /形式为GT;';
?>< DIV ID =结果>
< PHP
回声你最喜欢的车是$ mycar
?>
< / DIV>
编辑:我曾尝试这一点,我目前一直呼应的我进入文本框,并没有别的并没有什么的评选中这款车心不是似乎实现这种
这里是code,我有
< PHP
$汽车=阵列(大众,雷诺,路虎);
?>
<形式的行动=array.php方法=后> <中心及GT; <输入类型=文本名称=车ID =车/>
<输入类型=提交/> < /中心及GT;
< PHP
如果(in_array($ _ POST,$车)){
回声你最喜欢的车是$ _ POST
}其他{
回声这车是不是选择之一;
}
?>
< /表及GT;
您必须编辑表单来接受多个值。你呼应文本之前,您可以检查用户的值。
<形式方法=邮报>
< DIV ID =下拉菜单>
< PHP
$数组1 =阵列('大众','雷诺','路虎');
$错误= FALSE;
如果(使用isset($ _ POST ['汽车']))
{
$ mycar = $ _ POST ['汽车'];
的foreach($ mycar为$车)
{
如果(!in_array($车,$数组1))
{
$错误= $车;
}
}
}
其他
{
$ mycar =阵列();
}
呼应'<选择名称=汽车[]多个=多>';
的foreach($数组1为$汽车){&GT?;
<期权价值=< PHP的echo $汽车;>中< PHP如果(in_array($汽车,$ mycar))回声\\入选='选择';?>>< PHP的echo $汽车;?>< /选项>
< PHP
}
呼应'< /选择>
<输入类型=提交名称=提交值=提交/>
< / DIV>< /形式为GT;';
?>< DIV ID =结果>
< PHP
如果($错误===假)回声你最喜欢的车是.implode('',$ mycar);
否则回声$错误。 不被包含'。爆('',$数组1);
?>
< / DIV>
I currently have a drop down box than when one of the options is selected it will echo-
"Your Favourite Car Is (option)
What I need to do now is change this so its a text box but the user can only type in one of the options within the array and if another one was chosen it would say you cannot have this as one of the choices
and it would also be able to type in more than one so theoretically it could echo-
"Your Favourite Car is Mazda, Nissan, Renault!"
Here is the code i have now for the drop box that i have working.
<form method="post">
<div id="dropdown">
<?php
if(isset($_POST['cars']))
{
$mycar=$_POST['cars'];
}
else
{
$mycar="";
}
$array1 = array('Volkswagen' , 'Renault' , 'Land Rover');
echo' <select name="cars" onchange="this.form.submit()">';
foreach($array1 as $cars)
{ ?>
<option value="<?php echo $cars; ?>" <?php if($mycar==$cars) echo "
"selected='selected'"; ?> ><?php echo $cars; ?></option>
<?php
}
echo'</select>
</div></form>';
?>
<div id="result">
<?php
echo "Your favourite car is $mycar";
?>
</div>
EDIT: I have attempted this and what i currently have always echo's "this car isnt among the selection" and nothing else and nothing i enter into the text box seems to effect this
here is the code i have
<?php
$cars = array("Volkswagen","Renault","Land Rover");
?>
<form action="array.php" method="post">
<center> <input type="text" name="cars" id="cars" />
<input type="submit" /> </center>
<?php
if (in_array($_POST, $cars)) {
echo "Your Favourite Car is $_POST";
}
else {
echo "This car is not among the selection";
}
?>
</form>
You must edit the form to accept multiple values. And you can check the user values before you echo the text.
<form method="post">
<div id="dropdown">
<?php
$array1 = array('Volkswagen' , 'Renault' , 'Land Rover');
$error = false;
if(isset($_POST['cars']))
{
$mycar=$_POST['cars'];
foreach ($mycar as $car)
{
if (!in_array($car, $array1))
{
$error = $car;
}
}
}
else
{
$mycar=Array();
}
echo' <select name="cars[]" multiple="multiple">';
foreach($array1 as $cars){ ?>
<option value="<?php echo $cars; ?>" <?php if(in_array($cars, $mycar)) echo "\"selected='selected'"; ?> ><?php echo $cars; ?></option>
<?php
}
echo'</select>
<input type="submit" name="submit" value="submit" />
</div></form>';
?>
<div id="result">
<?php
if ($error===false) echo "Your favourite car is ".implode(', ', $mycar);
else echo $error . ' is not contained by ' . implode(', ', $array1);
?>
</div>
这篇关于文本框限于数组内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!