jQuery validate does not return submit button name
I need help here. My jQuery code does not return the submit button name in the $_POST Array...
This is my code:
<script type="text/javascript">
$(document).ready(function(){
$('#submForm').validate({
submitHandler: function(form) {
// Do cleanup first
form.submit();
}
})
});
</script>
<input type="submit" class="paypal_bttn" value="premium" name="bttn">
In the $_POST array I can see everything except the button name,开发者_StackOverflow中文版 that I am really in need to know...
Please help. Thanks
That's normal. No button was clicked by the user. You forced the submission of the form using javascript. Why do you expect that a button name would be sent when no button was clicked at all. What if you had multiple submit buttons? Which one would you like to send in this case? If your server side script expects some button name you could inject a hidden field into the form with the same name and set its value just before forcing its submission:
$(form).append(
$('<input/>', {
type: 'hidden',
name: 'bttn',
value: 'premium' // TODO: you might want to adjust the value
})
);
form.submit();
you are not submitting or validating the form on button click
, since you have wrapped your code inside the ready
handler as soon as the DOM is ready you form is validated and then submitted
<input type="submit" class="paypal_bttn" value="premium" name="bttn">
what you can do is
$(document).ready(function(){
$("input[name='bttn']").click(function(e){
e.preventDefault(); //your button type is submit so prevent the default form submission
$('#submForm').validate({
submitHandler: function(form) {
// Do cleanup first
form.submit();
}
})
});
});
The jQuery is not running when you are clicking the button. What you need to do is add the "click(function()" instead. You can then use attr() or other items to select what you want to from there.
You should also use return false;
if you don't want the page to submit and you want to use AJAX.
$(".paypal_bttn").click(function(){
// add stuff to do here.
});
You might want to capture the click of the button and do your validation/submit from there.
$('.paypal_bttn').click( function() {
// Do cleanup first
// validate form and submit
}
精彩评论