Ghost
Ghost

Reputation: 149

How to disable the reset upon submitting a form

How can I maintain the value of the form when I submit it?

<select name="Year">
    <option value="2013">2013</option>
    <option value="2014">2014</option>
    <option value="2015">2015</option>
</select>

<input type="submit" value="Filter"/>

Upvotes: 0

Views: 171

Answers (2)

emha
emha

Reputation: 300

If you use jquery (you tagged it): Submit your form via $.post to your php script. Something like

$("form").submit(function() {
    $.post("URL/TO/FORM.PHP", $(this).serializeArray(), function(data) {
         console.log(data);
    });
    return false;
});

So you submit your form to your script but the site don't refresh because you set the return to false. You can get the url to your script from your action="" like this

$("form").attr("action")

Upvotes: 2

Vicky Gonsalves
Vicky Gonsalves

Reputation: 11717

if you are using php then:

<select name="Year">
    <option value="2013" <?php if($_REQUEST['Year']=='2013'){echo "selected"}?>>2013</option>
    <option value="2014" <?php if($_REQUEST['Year']=='2014'){echo "selected"}?>>2014</option>
    <option value="2015" <?php if($_REQUEST['Year']=='2015'){echo "selected"}?>>2015</option>
</select>

<input type="submit" value="Filter"/>

Upvotes: 0

Related Questions