WesleyA Posted May 18, 2015 Share Posted May 18, 2015 I want to create a condition for my script. It starts with reading an array, this array can be unlimited, at least 4 up to 40 elements. These elements contain 2 variables. I figured out foreach is a better solution than switch (which I use now) In the next example (simple version of my above purpose) I show what I indicate. <?php $elements = array('Volvo', 'Honda', 'Ford'); ?> <form method="post"> <select name="menudrop"> <?php foreach ($elements as $value) : ?> <option value="<?php echo $value ?>"><?php echo $value ?></option> <?php endforeach; ?> </select> <input type="submit" value="submit" /> </form> <?php if (in_array($_POST['menudrop'], $elements)){ echo 'Clients enjoy eating a ' . $_POST['menudrop']; } ?> The problem is an error. The line with the error is: if (in_array($_POST['menudrop'], $elements)) what exactly does this error mean? The script is running and all things are executed like I intended, but I still get an error message at the screen. Notice: Undefined index: Link to comment Share on other sites More sharing options...
dsonesuk Posted May 18, 2015 Share Posted May 18, 2015 Is this on loading of page before submitting? because $_POST['menudrop'] would not exist to search array. try if(isset($_POST['menudrop']) && !empty($_POST['menudrop'])) { run code... } Link to comment Share on other sites More sharing options...
WesleyA Posted May 18, 2015 Author Share Posted May 18, 2015 OK this works thanks a lot dsonesuk. What is the explanation of this code? Link to comment Share on other sites More sharing options...
dsonesuk Posted May 18, 2015 Share Posted May 18, 2015 There is no index of 'menudrop' for $_POST, if fact there are no $_POST indexes at all UNTIL a actual submission has taken place. The if condition makes sure $_POST['menudrop'] has been set and exists and that it is not empty. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now