简体   繁体   中英

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. 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"]

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.

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.

Can you try

$_REQUEST["whatever"] to see what you are getting

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

Edit see this as well

github.com/maxatwork/form2js

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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