Note that there are some explanatory texts on larger screens.

plurals
  1. POText box limited to array contents
    primarykey
    data
    text
    <p>I currently have a drop down box than when one of the options is selected it will echo-</p> <p><code>"Your Favourite Car Is (option)</code></p> <p>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 <code>you cannot have this as one of the choices</code> and it would also be able to type in more than one so theoretically it could echo-</p> <p><code>"Your Favourite Car is Mazda, Nissan, Renault!"</code></p> <p>Here is the code i have now for the drop box that i have working.</p> <pre><code>&lt;form method="post"&gt; &lt;div id="dropdown"&gt; &lt;?php if(isset($_POST['cars'])) { $mycar=$_POST['cars']; } else { $mycar=""; } $array1 = array('Volkswagen' , 'Renault' , 'Land Rover'); echo' &lt;select name="cars" onchange="this.form.submit()"&gt;'; foreach($array1 as $cars) { ?&gt; &lt;option value="&lt;?php echo $cars; ?&gt;" &lt;?php if($mycar==$cars) echo " "selected='selected'"; ?&gt; &gt;&lt;?php echo $cars; ?&gt;&lt;/option&gt; &lt;?php } echo'&lt;/select&gt; &lt;/div&gt;&lt;/form&gt;'; ?&gt; &lt;div id="result"&gt; &lt;?php echo "Your favourite car is $mycar"; ?&gt; &lt;/div&gt; </code></pre> <p>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</p> <p>here is the code i have </p> <pre><code>&lt;?php $cars = array("Volkswagen","Renault","Land Rover"); ?&gt; &lt;form action="array.php" method="post"&gt; &lt;center&gt; &lt;input type="text" name="cars" id="cars" /&gt; &lt;input type="submit" /&gt; &lt;/center&gt; &lt;?php if (in_array($_POST, $cars)) { echo "Your Favourite Car is $_POST"; } else { echo "This car is not among the selection"; } ?&gt; &lt;/form&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload