通过JQuery AJAX/javascript发送表单数据


Send Form Data Through JQuery AJAX / javascript

我有一个这样的表单;

<form action="out.php" method="post">
    <input type="hidden" name="a" value="a" />
    <input type="hidden" name="b" value="b" />
    <input type="hidden" name="c" value="c" />
    <input type="hidden" name="d" value="d" />
    <input type="hidden" name="e"  maxlength="60" value="e" />
    <input type="hidden" name="f" value="f" />
    <input type="submit" value="Create & Send">
</form>

用户看不到此表单。他们只看到一个提交按钮,比如"创建标签并发送给客户"。但他们需要输入客户的电子邮件地址。所以我有一个js代码,提交按钮触发它。它询问电子邮件地址。JS代码:

$('#dialog_prompt').click(function(){
    $.prompt("What is customer's email?","",
    function(value){
        $.msg("So, you like '"+value+"'?");
    },
    function(){
        $.msg("You clicked cancel!");
    });
});

所以我的问题是;当用户提交按钮并输入客户的电子邮件并点击ok时,JS必须从表单&"out.php".的电子邮件地址

那么我如何通过JS发送表单数据呢?

HTML:

<form action="out.php" method="post">
<input type="hidden" name="em" value="" class="customeremail" />
<input type="hidden" name="a" value="a" />
<input type="hidden" name="b" value="b" />
<input type="hidden" name="c" value="c" />
<input type="hidden" name="d" value="d" />
<input type="hidden" name="e"  maxlength="60" value="e" />
<input type="hidden" name="f" value="f" />
<input type="submit" value="Create & Send">
</form>

JS:

$('#dialog_prompt').click(function(){
    $.prompt("What is customer's email?","",
    function(value){
        $('form .customeremail').val(value);
        $('form').ajaxSubmit();
    },
    function(){
        $.msg("You clicked cancel!");
    });
});

您可以使用AJAX发送详细信息。

$('#dialog_prompt').click(function(){
                $.prompt("What is customer's email?","",
                function(value){        
                    $.msg("So, you like '"+value+"'?");
                    $.ajax({
                    url: "out.php",   // url to which details are send
                    type: 'POST',
                    data: $('form').serialize(),   // pass form details
                    } ).done (function(data) {      // on success
                  });
                },
                function(){
                    $.msg("You clicked cancel!");
                });
            });

现在,您可以使用$_POST访问out.php页面中通过AJAX传递的值。

注意:serialize()方法通过序列化表单值来创建URL编码的文本字符串。