2

I have a select with some options. But this select section is not always shown. It is only shown if a user has clicked another element. If the user submits the form than the default values (first entries) of the select are also submitted. How can I clear the select value?

I tried it with $('#ExpM').val('') and also with this:

    $('#Form').submit(function() {
        if (!$('#radio_dump').is(':checked')){
            $('#ExpM').selectedIndex=-1;
            $('#ExpY').selectedIndex=-1;
        }
    });

But the values are also sent in the post. These values are not required. What can I do?

testing
  • 19,681
  • 50
  • 236
  • 417

3 Answers3

6

Try this:

$('#Form').submit(function() {
    if (!$('#radio_dump').is(':checked')){
        $('#ExpM').attr('name','');
        $('#ExpY').attr('name','');
    }
});

Basically, name it blank so nothing for it is sent.

Indranil
  • 2,451
  • 1
  • 23
  • 31
1
foreach($_POST as $key => $value)
if ( empty($value) ) unset($_POST[$key]);
Dejan Marjanović
  • 19,244
  • 7
  • 52
  • 66
  • Where should this code be placed? With `unset` the variable should be cleared. Does it mean that I can't see them in Firebug anymore? – testing Dec 23 '11 at 16:10
  • This is PHP code, firebug can't see server side values... this will remove empty values from $_POST array in PHP, you should set first option in select like this ``... – Dejan Marjanović Dec 23 '11 at 16:13
  • Currently I send the values of the form to the same script which generates the values. If I use `print_r ($_POST);` I can see the parameters in the array. Also in Firebug I see the POST parameter. I have placed the PHP code at the end of my script. Mustn't it be in the submit event or something like this? I already added the empty option field as Galen proposed. – testing Dec 23 '11 at 16:19
  • 1
    Well you should add it on top of PHP script, adding to bottom is pointless. – Dejan Marjanović Dec 23 '11 at 16:20
0

This is some idea only, I have not tested if it solves your problem:

Disabled form controls are not submitted. You could disable those form controls which should not be submitted, so you would not need to worry any longer which item is selected at all.

See Disabled and read-only controlsHTML4.

I'm pretty sure jQuery has something to disable a form field, see jQuery - Disable Form Fields.

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836