Php: If the input is x, y or z,
In the html page, I have this input:
<input type="text" name="my_input" id="my_input">
Using PHP, if the user types in the input the words "sun", "moon", "stars", I want the script to do things.
Something like this, just don't know how to write it correctly:
if (my_input.value == "sun,moon,stars") {
do stuff
}
Many thanks!
+3
source to share
5 answers
Or you can use php : preg_match()
if(preg_match("/^(sun|moon|stars)$/i", $_POST['my_input']) === 1) {
// success
}
+1
source to share
There are several ways to accomplish this. Some are better than others, I personally preferin_array
in_array
if (in_array($value, array("sun", "moon", "stars"))) {
// Do something
}
If the statement
if ($value == "sun" || $value == "moon" || $value == "stars") {
// Do something
}
Switch
switch ($value) {
case "sun":
case "moon":
case "stars":
// Do something
break;
}
Note that you can achieve more. The above are just a few.
+4
source to share
Although tagged with php, your example code looks like javascript, so here is a javascript solution:
<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>
+1
source to share