最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Simulate html form POST using ajaxjquery - Stack Overflow

matteradmin5PV0评论

I want to read all the post variables and their content from a form and post them using jquery's "$.post()". First of all, this won't do the job: $.post("myServer/test2.php", $('#myform').serialize()) because it would only send one variable which I'd have to parse on the php side.

Here is how I'd start:

function doIndirectPost() {
    variableWithTheFormPOSTData = {};
    $("#IdOfMyForm :input").each(function() {
        variableWithTheFormPOSTData[$(this).attr("name")] = $(this).attr("value");
    }
    document.getElementById("IdOfMyForm").submit();
    $.post("myServer/test2.php", variableWithTheFormPOSTData);
}

and then I'd like to use $.post() to post the data seperated in multiple variables (just like a normal form submit would do it... I read somewhere, that you could do that like this:

$.post('myserver/test2.php.php',{
    var1: content1,
    var2: content2
}

But I want it to be dynamic. This part:

    var1: content1,
    var2: content2

should autmatically contain all variable names and values of the form.

In the end I'd like to be able to get all POST variables like this:

foreach ($_POST as $key => $value)
{
    echo $key . "= " . $value;
}

I want to read all the post variables and their content from a form and post them using jquery's "$.post()". First of all, this won't do the job: $.post("myServer./test2.php", $('#myform').serialize()) because it would only send one variable which I'd have to parse on the php side.

Here is how I'd start:

function doIndirectPost() {
    variableWithTheFormPOSTData = {};
    $("#IdOfMyForm :input").each(function() {
        variableWithTheFormPOSTData[$(this).attr("name")] = $(this).attr("value");
    }
    document.getElementById("IdOfMyForm").submit();
    $.post("myServer./test2.php", variableWithTheFormPOSTData);
}

and then I'd like to use $.post() to post the data seperated in multiple variables (just like a normal form submit would do it... I read somewhere, that you could do that like this:

$.post('myserver./test2.php.php',{
    var1: content1,
    var2: content2
}

But I want it to be dynamic. This part:

    var1: content1,
    var2: content2

should autmatically contain all variable names and values of the form.

In the end I'd like to be able to get all POST variables like this:

foreach ($_POST as $key => $value)
{
    echo $key . "= " . $value;
}
Share Improve this question asked Sep 4, 2013 at 11:15 user2743803user2743803 511 silver badge3 bronze badges 4
  • Have you tried serializeArray()? – Ben Fortune Commented Sep 4, 2013 at 11:16
  • "First of all, this won't do the job: $.post("myServer./test2.php", $('#myform').serialize()) because it would only send one variable" — That simply isn't true. It would send all the successful inputs in the form, using the standard form encoding method, and PHP would decode it automatically and populate $_POST. – Quentin Commented Sep 4, 2013 at 11:28
  • really?? I was told that I'd have to use parse_str($_POST['serialize'], $data); on it. idk – user2743803 Commented Sep 4, 2013 at 12:16
  • That's only if you do something stupid like: $.post('foo.php', { serialize: $('#myform').serialize() }); – Quentin Commented Sep 4, 2013 at 12:23
Add a ment  | 

2 Answers 2

Reset to default 5

Serialize doesn't send only one variable, it sends name value pairs of all input elements in the form. So

    $("#IdOfMyForm").on("submit", function () {
        $.post("myServer./test2.php", $("#IdOfMyForm").serialize(), 
                function(dataFromServer){
                   //server sent a response now do whatever you want to do after form has been submitted 
                   //or submit form regular way $("#IdOfMyForm").submit();
                 }
        );
        return false;
    });

Should work. Just remember to set name attribute of every input/select element in form.

Have you tried it using JQuery's Ajax instead?

Like this answer here: jQuery Ajax POST example with PHP

Post a comment

comment list (0)

  1. No comments so far