I have some input text and a dropdown. When you hit the submit, if my text field is empty I add that to my $missing array, but because there is always something “selected” in my drop down, it’s not seen as empty. How can I handle the drop down that has the first option selected (‘choose one’) and have it seen as empty?
I tried to directly assign something to the $missing array if the ‘choose one’ is still selected in the drop down, but no luck… even though I can see it show up in my $missing array, my entry still gets posted to my db.
I’d appreciate any advice on this problem. I’m very new to php/mysql and i’ve been putting this together based on other things i’ve learned. here’s what my code is looking like.
<?php
// process if the add item button is pressed
if (array_key_exists('insert', $_POST)) {
include('../includes/corefuncs.php');
// remove backslashes
nukeMagicQuotes();
// prepare an array of expected items
$expected = array('category', 'description', 'price', 'additional');
// set required field
$required = array('category', 'description', 'price');
// create empty array for missing fields
$missing = array();
// create database connection
$conn = dbConnect('admin');
// make $_POST data safe for insertion into database
foreach ($_POST as $key => $value) {
// assign to temporary variable and strip whitespace if not an array
$temp = is_array($value) ? $value : trim($value);
// if empty and required, add to $missing array
if (empty($temp) && in_array($key, $required)) {
array_push($missing, $key);
}
elseif (in_array($key, $expected)) {
${$key} = mysql_real_escape_string($value);
}
}
// prepare the SQL query if $missing is empty
if (empty($missing)) {
$sql = "INSERT INTO lunch (category, description, price, additional)
VALUES('$category', '$description', '$price', '$additional')";
// process the query
$result = mysql_query($sql) or die(mysql_error());
}
// if successful, redirect to list of existing records
if ($result) {
//header('');
//exit;
}
}
?>
<body>
<h1>New Lunch Item</h1>
<?php
if ($_POST && isset($missing)) { ?>
<p class="warning">Please complete the missing item(s) indicated.</p>
<?php } ?>
<form id="form1" name="form1" method="post" action="">
<p>
<label for="category">Choose a menu category</label>
<select name="category" id="category">
<option value="choose one"
<?php
if ($_POST['category'] == 'choose one') { ?>
selected="selected"
<?php
$missing[] = 'choose one'; ?>
<?php } ?>
>Choose one</option>
<option value="starters"
<?php
if ($_POST['category'] == 'starters') { ?>
selected="selected"
<?php } ?>
>Starters</option>
<option value="salads"
<?php
if ($_POST['category'] == 'salads') { ?>
selected="selected"
<?php } ?>
>Salads</option>
<option value="entrees"
<?php
if ($_POST['category'] == 'entrees') { ?>
selected="selected"
<?php } ?>
>Entrees</option>
<option value="sweets"
<?php
if ($_POST['category'] == 'sweets') { ?>
selected="selected"
<?php } ?>
>Sweets</option>
</select>
</p>
<p>
<label for="description">Description:</label>
<textarea name="description" cols="60" rows="8" class="widebox" id="description"></textarea>
</p>
<p>
<label for="price">Price:</label>
<input name="price" type="text" class="widebox" id="price"></textarea>
</p>
<p>
<label for="additional">Additional:</label>
<input name="additional" type="text" class="widebox" id="additional"></textarea>
</p>
<p>
<input type="submit" name="insert" value="Add new item" />
</p>
</form>
<pre>
<?php
if (array_key_exists('insert', $_POST)) {
print_r($missing);
}
?>
</body>