Sunday 15 July 2012

jquery - Submit a form with A tag and value -



jquery - Submit a form with A tag and value -

i know using:

<a href="#" onclick="document.formname.submit();">

i can submit form, if need submit value?

say have form

<form name="f1" id="f1" role="form"> <input type="radio" name="myesno" value="yes" id="myesno1"> yes <input type="radio" name="myesno" value="no" id="myesno2"> no </form>

so neither selected , neither required, form show @ top of page, , @ bottom has form same purpose, so, can see kind of redundant have 2 forms same thing specially if in same page... thought utilize < > tags submit form, thing how submit value?

<a href="#" > yes </a> | <a href="#" > no </a>

so how do that?

html:

<form name="f1" id="f1" role="form"> <input type="hidden" name="selected_option" id="selected_option" value="" /> <a class="opt_button" data-value="yes" href="#" > yes </a> | <a data-value="no" class="opt_button" href="#" > no </a> </form>

javascript:

$(document).ready(function() { $('.opt_button').on('click', function() { $('#selected_option').val($(this).attr('data-value')); $('#f1').submit(); }); });

php:

if ($_post['selected_option'] == 'yes') { echo "yes selected"; } elseif ($_post['selected_option'] == 'no') { echo "no selected"; } else { echo "neither alternative selected"; }

jquery html forms

No comments:

Post a Comment