0

我正在尝试将 AJAX 函数从 jQuery 发送到 PHP。我可以将它转到正确的页面,但是当我到达那里时,如何从 AJAX 调用中访问数据?这可能是我想念的愚蠢的东西。

jQuery:

$(document).ready(function() {

    $("#assessform_page").on("submit", "#assessform", function(e) {
        e.preventDefault();
        var assessrows = {};
        var url = $(this).attr("action");

        $("div[data-test-num]").attr("data-test-num", function(index, value) {
            assessrows[value] = {};
            $(this).children("input[data-tab-row], select[data-tab-row]").attr("data-tab-row", function(i, v) {
                assessrows[value][v] = $(this).val();
            });
            $(this).children("label[data-tab-row]").attr("data-tab-row", function(i, v) {
                assessrows[value][v] = $(this).text();
            });
        });

        $.ajax({
            url: url,
            data: assessrows,
            type: "POST",
            success: function() {
                window.location.href = url;
            },
        });

    });

});

HTML:

<form id="assessform" action="assessment.php" method="post" enctype="application/x-www-form-urlencoded" name="assessform">
    <div data-test-num="1">
        <label data-tab-row="objname">First Value: </label>
        <select autofocus="autofocus" data-tab-row="status">
            <option value="not_started">Not started</option>
            <option value="in progress">In Progress</option>
            <option value="obstacles">Obstacles</option>
            <option value="excluded">Excluded</option>
            <option value="done">Done</option>
        </select>
        <input type="text" data-tab-row="numer" value="" />
        <input type="text" data-tab-row="denom" value="" />
        <br />
        <br />
    </div>
    <button type="submit" name="submit-button" id="submit-button">Submit</button><br />
    <button type="button">Save</button><br />
</form>

PHP:

<?php ?>(在我得到这个变量之前,我真的什么都做不了。)

我需要调用什么变量来从 AJAX 获取数据,以及在执行此操作之前需要调用什么函数(如果有)?

4

2 回答 2

1

EDIT:

If your assessment.php make a response as follows:

  <?php
       echo 'successfully_completed';
  ?>

The response data from PHP will be passed as follows:

$.ajax({
    url: url, // assessment.php
    data: assessrows,
    type: "POST",
    success: function(data) {
        alert("The response is [" + data + "]"); 
        // "The response is [successfully_completed]"

        // If you want to pass this data to the next page
        window.location.href = "/foo/success.php?data="+data;  
    }
});
于 2013-10-28T01:05:45.473 回答
1
$.ajax({
    url: "url",
    data: assessrows,
    type: "POST",
    success: function(data) {
        console.log(data); //do something with data
        window.location.href = url;
    },
});
于 2013-10-28T00:46:09.170 回答