Adam Pietrasiak
Adam Pietrasiak

Reputation: 13224

HTML dont submit select with given option

I want option ccc to be selectable, but not submitted, so when someone submit form with option ccc selected, all <select> should not be sent.

HTML only solution would be best.

<select name="some">
   <option>aaa</option> //send
   <option>bbb</option> //send
   <option special>ccc</option> //dont send
</select>

Upvotes: 0

Views: 2662

Answers (4)

Kingslayer47
Kingslayer47

Reputation: 493

Using Only HTML you can

<form action="#" method="POST">
<select name="some" required>
    <option selected value="">ccc</option> //empty value
    <option value="aaa">aaa</option> //send
    <option value="bbb">bbb</option> //send
</select>

<input type="submit" name="submit">
</form>

Upvotes: 0

Matanya
Matanya

Reputation: 6346

You cannot prevent the select submission without removing or disabling it prior to form submission using javascript.

Having said that, it makes much more sense to test for this on the server side, and this is actually done regularly when no option is selected, like so:

<select name="some">
   <option value="-1">Select an option</option> 
   <option value="aaa">aaa</option> 
   <option value="bbb">bbb</option> 
</select>

PHP:

if ($_POST['some']!=-1) {
  // do something
}

Upvotes: 1

Chris Laarman
Chris Laarman

Reputation: 1597

With HTML only, the field will always be submitted. However you can post an empty value, giving practically the same effect:

<select name="some">
   <option value="aaa">aaa</option>
   <option value="bbb">bbb</option>
   <option value="">ccc</option> //empty value
</select>

Upvotes: 1

Bardo
Bardo

Reputation: 2523

Try this

<select name="some">
   <option value="aaa">aaa</option> //send
   <option value="bbb">bbb</option> //send
   <option>ccc</option> //dont send
</select>

If I'm correct this should return nothing when you send the form with ccc selected.

Upvotes: -1

Related Questions