简体   繁体   English

jQuery序列化并传入其他自定义帖子数据

[英]Jquery serialize and passing in other custom post data

function DoAjax( args )
{
    var formData = "?" + $("form[name=" + args.formName + "]").serialize();

    $.ajax({
        type: "POST",
        url: args.url + formData,
        data: {
            request: args.request
        },
        success: function (data) {
            alert(data);
            args.callback.html(data);
        },
        error: function (a, b, c) {
            alert("error: " + a + ", " + b + ", " + c + ".");
        }
    });

    return false;       
}   

I need to pass in a custom bit of data to my php script called "request" which will denote which process to run on the php page... but I also want to serialize any other form fields on my form. 我需要将自定义数据传递到名为“ request”的php脚本中,该脚本将指示要在php页面上运行的进程...但是我还想序列化表单上的任何其他表单字段。 Is this the correct way of doing it (add the serialize to the end of the URL) because I just cannot seem to get anything from my PHP $_POST["whatever"] 这是正确的方法吗(将序列化添加到URL的末尾)是因为我似乎无法从PHP $ _POST [“ whatever”]中获得任何东西

EDIT:::/// 编辑:::///

<?php                                 
    $request = $_POST[ "request" ];   

    if( isset( $request ) )
    {
        require_once( "includes.php" );

        function LogInWithClientCred()
        {
            $username = CheckIsset( "username" );
            $password = CheckIsset( "password" );

            echo $_REQUEST["username"];
            echo $_GET["username"]; // echos correctly the username.. but I want it to be post data instead!

Please ignore the security awesomeness, it is pseudo for your purpose. 请忽略安全性,因为它是伪造的。

Combine formData and your additional data into one string. formData和其他数据合并为一个字符串。

var formData =  $("form[name=" + args.formName + "]").serialize() + "&request=" + args.request;

$.ajax({
    url: args.url,
    data: formData,
    type: "POST",
    ...
})

I am not too sure If i got ur question well, But I think you are appending the form data to URL and using POST in ajax. 我不太确定如果我的问题很好,但是我认为您是将表单数据附加到URL并在ajax中使用POST。

Can you try 你能试一下吗

$_REQUEST["whatever"] to see what you are getting $ _REQUEST [“ whatever”]以查看您得到了什么

EDIT After seeing the commnets I think I now got it 编辑看到通讯之后,我想我现在明白了

 function DoAjax( args )
{
    var formData =  $("form[name=" + args.formName + "]").serialize();

    $.ajax({
        type: "POST",
        url: args.url,
        data: {
            request: args.request,
        formData:formData
        },
        success: function (data) {
            alert(data);
            args.callback.html(data);
        },
        error: function (a, b, c) {
            alert("error: " + a + ", " + b + ", " + c + ".");
        }
    });

    return false;       
}   

Now $_POST[formData] should work 现在$ _POST [formData]应该可以工作了

Edit see this as well 编辑也看到这个

github.com/maxatwork/form2js github.com/maxatwork/form2js

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

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