如何通过AJAX发送外部表单POST数据 [英] How to send external form POST data through AJAX

查看:69
本文介绍了如何通过AJAX发送外部表单POST数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web表单,简单的HTML/PHP本身可以工作,但是当它通过下面的AJAX调用传递到模板页面上时-提交时缺少发布数据.这一定是我在下面缺少的一个参数.

I have a web form, simple HTML / PHP that works by itself but when it is passed onto the template page via the below AJAX call -- the post data is missing on submission. This HAS got to be a param I'm missing below.

$(document).ready(function() {

$('#toggle3').click(function(){
    var tog = $('.toggle');
    $.ajax({
        type: 'POST',
        url: '/mysimplewebform.php',
        success: function (fields){
            tog.html(fields);
            tog.slideToggle(1000);
        }
    });
  });
});

请求已发送.提交后,我会收到电子邮件,通过表格发送除选定帖子数据以外的所有内容.下面是PHP.

The request is sent. And upon submission I receive email, everything but the selected post data via form is sent. Below is the PHP.

<?php
    $backwheel = $_POST['backwheel'];
    $frontwheel = $_POST['frontwheel'];
    $form_message = "backwheel:".$backwheel." frontwheel:".$frontwheel." \nMessage: ". " You just recieved a new custom order via your Customizer!"."\nFullCustomURL: ".$_SERVER['HTTP_REFERER'];

  mail("email@gmail.com", "Your Website Something", $form_message, "From: Capn Ron (New Order!)" );

  if (isset($_POST['submit']))

    {   
echo "<script>
alert('Thanks for your Order!');
window.location.href='http://www.website.com';
</script>";       
    }
?> 

推荐答案

您不会缺少参数.在您的代码中,#toggle3似乎是按钮,因为click事件已绑定到按钮.因此,如果您尝试对其进行序列化,则它肯定不会返回任何内容.您必须序列化周围的表单,这可以通过使用jQuery的 closest()函数轻松实现;即:

You're not missing a param. From your code, #toggle3 appears to be button since the click event is bound to it. So, if you try to serialize it, it will certainly return nothing. You have to serialize the surrounding form which is easily achieved by using jQuery's closest() function; i.e.:

$(document).ready(function() {

$('#toggle3').click(function(){
    var tog = $('.toggle');
    $.ajax({
        type: 'POST',
        url: '/mysimplewebform.php',
        data: $(this).closest('form').serialize(),
        success: function (fields){
            tog.html(fields);
            tog.slideToggle(1000);
        }
    });
  });
});

这篇关于如何通过AJAX发送外部表单POST数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆