How can I post the following form using Ajax
. When submitting the form that page refreshes and I want to avoid that.
<form method="post" action="" >
<div class="rp-donation-block checkout_donation" >
<p class="message"><strong><?php echo $this->get_setting('message'); ?></strong></p>
<div class="input text">
<input type="text" value="<?php echo $amount; ?>" class="input-text text-donation" name="donation-amount">
<input type="submit" value="<?php echo $this->get_setting('btn_lable'); ?>" class="button" name="donate-btn">
</div>
</div>
</form>
I tried both of these with no luck.
<form method="post" action="" onSubmit="return false">
<form method="post" action="" action= "<?php echo $_SERVER['PHP_SELF']; ?>">
I suppose your form element has id of
form
as<form id="form"...
A form is not mandatory to post using
Ajax
. You can write anAjax
request using just a button click as well.If you want to invoke only on form submission you can write your
Ajax
like so.On the other side use :
To Get
post1
value.The button you are using to trigger the event that causes the submit of the form via ajax post should not be of type submit! Else this will always fail.