ifnotX
ifnotX

Reputation: 73

Jquery - add value before submit

The user is submitting a single value using a GET method. I want to add a value from the template to pass to the server. In this example the value is "budweiser"

I have tried a number of variations on the below, but the only thing that gets passed to the server is the parameter in the form.

<script>                                                                                                                                                                     
$('#form1').submit(function(){                                                                                                                                             
$(this).append('<input type="hidden" name="filter" value="budweiser">');                                                                                                                               
  return true;                                                                                                                                                            
</script> 

<form id="form1" action="/dashboard_single/" method="get">                                                                                                                 
<input type="integer" name ="dash_days">                                                                                                      
<input type="submit" value="Go">                                                                                                                                         
 </form> 

Upvotes: 0

Views: 296

Answers (1)

S&#233;bastien
S&#233;bastien

Reputation: 12139

There is not much more to do from your current code. The following should work:

$('#form1').submit(function(event) {
    $('#form1').append('<input type="hidden" name="filter" value="budweiser">');
});

You will see that the filter param is passed with the value "budweiser".

$('#form1').submit(function(event) {
    $('#form1').append('<input type="hidden" name="filter" value="budweiser">');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="form1" action="/dashboard_single/" method="get">
  <input type="integer" name="dash_days">
  <input type="submit" value="Go">
</form>

Upvotes: 1

Related Questions