通过$ _POST将$ .ajax发布的参数发送到PHP数组

时间:2016-01-17 09:53:23

标签: php ajax post

谁能帮帮我?

我在将参数(数组外)放入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;
        }

我已经检查了stackoverflow上的所有发布的答案,并且已经阅读了很多关于这个主题的消息,我已经跟进了几个答案,但发送者($ _POST)不存在。我错了什么?谢谢你的回答。

2 个答案:

答案 0 :(得分:0)

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

同样将数据更正为data: postData,postData为allready object;

答案 1 :(得分:0)

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

data: { postData },

data: postData,