IF nothing is selected from 'category' and something in filled in 'newcategory'
THEN $category = $_POST['newcategory'];
ELSE IF something is selected from 'category' and 'newcategory' is not filled out
THEN $category = $row['Category']; (the category that was selected)
ELSE $message = 'Please select a current category OR create a new one';
Can be simplified to:
$category = (isset($_POST['newcategory']))?$_POST['newcategory']:$_POST['category'];
// If newcategory (text-box) is set, use text-box; otherwise use the selected item.
I'm pretty sure that's what you're lookin for. SO modified to your code it would look something like:
if ($_POST['category'] == 'createnewcategory'){
if (!$_POST['newcategory']) {
$message = "- Please Enter a Name for your new category -";
print "<script>window.location.href='$url_this?message=$message'</script>";
exit;
}
else {
$category = $_POST['newcategory'];
}
}
else if ( ( !isset($_POST['category'] || empty($_POST['category']) ) && (!isset($_POST['newcategory'] || empty($_POST['newcategory']) ) ){
$message = "- Please Either Select a Category or Create a new one -";
print "<script>window.location.href='$url_this?message=$message'</script>";
}
else {
$category = $_POST['category'];
}
Notice I flipped it around. We check to see if the posted category or new category are there. Then, if they're both completely empty we error, otherwise we use the selected item. Although my first bit of code would be better, but there's no error catching.
Another variation would be:
<?php
if(!isset($_POST['newcategory']) && isset($_POST['category']) && $_POST['category'] != 'createnewcategory')
{
// Textbox empty, select menu has something other than default selected
$category = $_POST['category'];
}
elseif( isset($_POST['newcategory']) && ( (empty($_POST['category']) || !isset($_POST['category']) || $_POST['category'] == 'createnewcategory') ) )
{
// Textbox filled with something, empty or not posted category,
// or category == 'creatnewcategory'
$category = $_POST['newcategory'];
}
elseif( isset($_POST['newcategory']) && ( isset($_POST['category']) && $_POST['category'] != 'createnewcategory') )
{
// Uh oh, we've got a selected item AND the text-field filled in. Which to use?
// Alert user to select either the right combo:
// (createnewcategory & text-field, or empty text-field & selected item)
}
else
{
// Nothing seems right.... alert user to enter proper information
}
?>