Scooby08 Posted August 29, 2008 Share Posted August 29, 2008 This seems so simple, but I'm not coming up with anything.. On a form they have a choice to either choose an existing category that is in the database or create a new category.. For the existing categories I have a select dropdown list, and for the new category I just have a input text field.. The part I can't figure out how to do is this.. I named the select dropdown of all the categories "category".. Then I named the input field for the new category "new_category".. Like so.. <select name="category"> <option value="" selected="selected">Categories...</option> <option value="All categories in database loop">All categories in database loop</option> </select> <input type="text" name="new_category" /> Then I need to put which ever one they choose to use into this query: $query = "INSERT INTO dw_items (item_category) "; $query .= "VALUES ('$_POST[the category selected or created]') "; Also, how would I make it so they can only use one or the other? (select or input) Just looking for suggestions here.. Thank you much.. Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 29, 2008 Share Posted August 29, 2008 First thing you need to decide is what to do IF both are submitted. You can use JavaScript to try and limit the user, but that is not foolproof. So you can either 1) Not accept the input and redisplay the form or 2) Consider one as the primary and eaccept that. I will assume that if the user submitted both that the user really meant to submit a new value if (isset($_POST['new_category']) && trim($_POST['new_category'])!='') { $category = mysql_real_escape_string(trim($_POST['new_category'])); } else if (isset($_POST['category']) && trim($_POST['category'])!='') { $category = mysql_real_escape_string(trim($_POST['category'])); } else { $category = false; } if (!$category) { echo "No category submitted!"; } else { $query = "INSERT INTO dw_items (item_category) VALUES ('$category')"; //Run query } Quote Link to comment Share on other sites More sharing options...
Scooby08 Posted August 29, 2008 Author Share Posted August 29, 2008 Thank you for that information.. It was exactly what I was looking for.. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.