Php:如果输入值是x、y或z,则执行一些操作
在html页面中,我有以下输入:Php:如果输入值是x、y或z,则执行一些操作,php,Php,在html页面中,我有以下输入: <input type="text" name="my_input" id="my_input"> 多谢各位 使用 这很容易 您可以通过post方法获取文本字段的值: <input type="text" name="my_input" id="my_input"> if ($_POST['my_input'] == "sun" || $_POST['my_input'] == "moon" || $_POST['my_input']
<input type="text" name="my_input" id="my_input">
多谢各位 使用
这很容易
您可以通过post方法获取文本字段的值:
<input type="text" name="my_input" id="my_input">
if ($_POST['my_input'] == "sun" || $_POST['my_input'] == "moon" || $_POST['my_input'] == "stars" ) {
do stuff
}
如果($\u POST['my_input']==“sun”|$\u POST['my_input']==“moon”|$\u POST['my_input']==“stars”){
做事
}
或者您可以使用in_数组函数。或者您可以使用php的:
if(preg_match("/^(sun|moon|stars)$/i", $_POST['my_input']) === 1) {
// success
}
有几种方法可以实现这一点。有些比其他好,我个人更喜欢数组中的
in_数组
if (in_array($value, array("sun", "moon", "stars"))) {
// Do something
}
If语句
if ($value == "sun" || $value == "moon" || $value == "stars") {
// Do something
}
开关
switch ($value) {
case "sun":
case "moon":
case "stars":
// Do something
break;
}
注意,有更多的方法可以实现这一点。以上只是其中的几个。虽然这是用php标记的,但您的示例代码看起来像javascript,因此这里有一个javascript解决方案:
<input type="text" name="my_input" id="my_input" onblur="doStuff(this.value);">
<script>
function doStuff(val){
if(["sun", "moon", "stars"].indexOf(val)!= -1){
alert("found");
}
}
</script>
函数doStuff(val){
如果([“太阳”、“月亮”、“星星”]。索引(val)!=-1){
警惕(“发现”);
}
}
在if
语句中使用in_array()
函数。请提供完整的代码好吗?javascript非常好,但这里是完全的php初学者…幸运的家伙,有人已经做了你的家庭作业;-)您的问题被标记为php
,但您的示例看起来像javascript,您需要什么?这并不像_array()解决方案中的那么优雅。另外,相等性测试操作符是=
,而不是=
。谢谢Steve。它看起来像javascript,因为它是我所知道的唯一编程语言。。。只是不知道如何用php编写它:)
switch ($value) {
case "sun":
case "moon":
case "stars":
// Do something
break;
}
<input type="text" name="my_input" id="my_input" onblur="doStuff(this.value);">
<script>
function doStuff(val){
if(["sun", "moon", "stars"].indexOf(val)!= -1){
alert("found");
}
}
</script>