Jump to content

[SOLVED] How is this done??


Scooby08

Recommended Posts

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..

 

Link to comment
https://forums.phpfreaks.com/topic/121828-solved-how-is-this-done/
Share on other sites

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
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.