In a html page I have this input:
<input type="text" name="my_input" id="my_input">
Using PHP, if the user enters in the input the words "sun", "moon", "stars", I want the script to do stuff.
Something like this, just don't know how to write it correctly:
if (my_input.value == "sun,moon,stars") {
do stuff
}
Thank you very much!
Use in_array()
if( in_array($_POST['my_input'], array("sun", "moon", "stars")) ) {
//Do stuff
}
There are a few ways you can achieve this. Some better than others, personally I prefer in_array
in_array
if (in_array($value, array("sun", "moon", "stars"))) {
// Do something
}
If statement
if ($value == "sun" || $value == "moon" || $value == "stars") {
// Do something
}
Switch
switch ($value) {
case "sun":
case "moon":
case "stars":
// Do something
break;
}
Note there are more ways you can achieve this. The above are just a few.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With