简体   繁体   English

如何使用AJAX和POST方法提交数据

[英]How to submit data using AJAX and POST Method

How can I post the following form using Ajax . 如何使用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']; ?>">

A form is not mandatory to post using Ajax . 使用Ajax发布表单不是强制性的。 You can write an Ajax request using just a button click as well. 您也可以仅单击按钮即可编写Ajax请求。

$(function () {
    $('#buttonId').on('click', function (e) {
        $.ajax({
            type: "POST",
            cache: false,
            url: <url_name>, //some php file where you may hit the database and want to modify content
            data:{'post1': <post1value>,'post2': <post2value>},
            datatype: "JSON", // can be html or text as well
            success: function (data) {
                var actualData = $.parseJSON(data); // if in case of JSON
            }
        });
    });
});

If you want to invoke only on form submission you can write your Ajax like so. 如果只想在表单提交时调用,则可以这样编写Ajax

$('#formId').on('submit', function (e) {
});

On the other side use : 另一方面使用:

$post_1 = $_POST['post1'];

To Get post1 value. 获取post1值。

I suppose your form element has id of form as <form id="form"... 我想你的表单元素具有的ID form<form id="form"...

<script>
// Attach a submit handler to the form
$( "#form" ).submit(function( event ) {

// Stop form from submitting normally
event.preventDefault();

// Get some values from elements on the page:
var $form = $( this ),
donate_amount = $form.find( "input[name='donation-amount']" ).val(),
url = $form.attr( "action" );

// Send the data using post
var posting = $.post( url, { amount: donate_amount } );

// Put the results in message div
  posting.done(function( data ) {
    var message_returned = $( data ).find( "#message" ); //hope your are returning message, also you can return other things like message type and create message on frontend as accordingly
    $( ".message" ).empty().append( message_returned ); //adding returned message to your message div
  });
});
</script>

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 ! 用于触发通过ajax post提交表单的事件的按钮的类型不应为Submit Else this will always fail. 否则,这将始终失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM