将 $.ajax 通过 $_POST 发布的参数放入 PHP 数组中


Getting $.ajax posted parameters via $_POST into a PHP array

谁能帮我?

我在将参数(从数组中)输入 PHP $_POST 时遇到问题。$ajax发送的参数填写得很好。当我尝试将整个数组抓取到 PHP 脚本中时,没有 _POST 美元或 _GET 美元可用。我做错了什么。我也尝试过使用单个参数,这些参数也失败了。我尝试了所有可能的方式来掌握参数($_GET和$ _POST)。我做错了什么?

例如,输入已定义如下,并且运行良好

-- 例如,输入已定义如下,并且工作良好

<div class="site input"> <i class="icon-pend icon calendar"></i>
<input name="fromdate" type="text" data-datepicker="true" value="<?php if($_POST['fromdate'] <> ""){ echo $_POST['fromdate'];} else{echo $_GET['fromdate'];} ?>" placeholder="<?php echo Lang::$word->FROM;?>" id="fromdate" data-bind="<?php if($_POST['fromdate'] <> ""){ echo $_POST['fromdate'];} else{echo $_GET['fromdate'];} ?>"></div>

-- 帖子如下所示

function createVerzuimServiceReport1()
{
        var pg='<?php echo $_GET['pg'] ?>';
        var ipp='<?php echo $_GET['ipp'] ?>';
        var fromdate_submit=$('input[name="fromdate_submit"]').val();
        if(fromdate_submit=='') {
            fromdate_submit='<?php echo $_GET['fromdate_submit'] ?>'; }
        var enddate_submit=$('input[name="enddate_submit"]').val();
        if(enddate_submit=='') {
            enddate_submit='<?php echo $_GET['enddate_submit'] ?>'; }
        var date = new Date(Date.parse($('#fromdate').val())),
                d = ("0" + date.getDate()).slice(-2),  
                m = ("0" + (date.getMonth() + 1)).slice(-2),              
                y =  date.getFullYear(),
                fDate = y + '-' + m + '-' + d;      
        var date = new Date(Date.parse($('#enddate').val())),
                d = ("0" + date.getDate()).slice(-2),  
                m = ("0" + (date.getMonth() + 1)).slice(-2),              
                y =  date.getFullYear(),
                lDate = y + '-' + m + '-' + d;  
        var postData = { };
        postData.action = "postData";
        postData.createVerzuimServiceReport1 = "createVerzuimServiceReport1";
        postData.fromdate_submit = fDate;
        postData.enddate_submit = lDate;
        postData.studentsearchfield = $('#filter').val();
        $.ajax({
            type: "post",
            url: "../plugins/registereddayvr/controller_verzuim_tot.php", 
            data: postData,
            dataType: "json", 
                success: function(data) { 
                    alert(postData.action + ' ' + postData.createVerzuimServiceReport1 + ' ' + postData.fromdate_submit + ' ' + postData.enddate_submit + ' ' + postData.studentsearchfield); 
                    alert(postData.action + ' ' + postData.createVerzuimServiceReport1 + ' ' + postData.fromdate_submit + ' ' + postData.enddate_submit + ' ' + postData.studentsearchfield);
                    window.open("../plugins/registereddayvr/controller_verzuim_tot.php", "MsgWindow", "width=800, height=900");                 
                    if(data.fout) 
                         alert('FOUT BERICHT: ' + data.bericht); 
                },          
                error: function (xhr, ajaxOptions, thrownError) {
                alert(xhr.status);
                alert(thrownError);
            }
        }); 
};

-- php 看起来像这样

if(isset($_POST['action']) && $_POST['action'] == "postData") { //if(isset($_GET['action']) && $_GET['action']== "postData")
   $vals = array(
        'action' => $action,
        'createVerzuimServiceReport1' => $createVerzuimServiceReport1,
        'fromdate_submit' => $fromdate_submit,
        'enddate_submit' => $enddate_submit,
        'studentsearchfield' => $studentsearchfield
    );
    // JSON encode er zijn params gevonden verstuur naar $.ajax success.
    echo 'resultaat is: '.json_encode($vals);
    exit; // zeker weten dat er niets anders is
}
        else { // zo is er toegang tot de fout bericht in jQuery
        echo json_encode(array('fout' => TRUE, 'bericht' => 'Een probleem is ontstaan! Fout is: Er zijn geen parameters ontvangen in controller_verzuim_tot.php...', 'get' => $_GET, 'post' => $_POST,));
            exit;
        }

我已经检查了堆栈溢出上所有发布的答案,并阅读了很多有关此主题的消息,并且我跟进了一些答案,但发件人($_POST)不存在。我做错了什么?感谢您的回答。

postData已经是一个对象。因此,不要使用周围{....}将其放入另一个对象中。更改此行

data: { postData },

data: postData,

您将 url 中的操作作为 GET 参数发送,但检查为 POST 参数。尝试将检查更改为if ((isset($_GET['action']) && $_GET['action'] == "postData")...或向数据添加操作postData.action = 'postData';

还要更正数据以data: postData,postData 是 allready 对象;