1

Who can help me?

I have a problem with getting the parameters (out of an array) into PHP $_POST. The parameters which has been send by $ajax are well filled. When I try to get grab the whole array into the PHP script, there is no $_POST or $_GET available. What do I wrong. I have tried it also with the single parameters, which fail also. I tried all possible manners to grap the parameters ($_GET as well as $_POST). What do I wrong?

for example the input has been defined as below and work well

-- for example the input has been defined as below and work well

<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>

-- The post looks like this below

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);
            }
        }); 
};

-- The php looks like this

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;
        }

I have checked all posted answers on stackoverflow and have read a lot of messages regarding this subject and I have followed up a few answers but the sender ($_POST) is not present. What do I wrong? Thanks for your answers.

J. Deloers
  • 13
  • 4
  • I would advice you to start on a simpler case while you are still learning. Check the answer of this question for instance http://stackoverflow.com/a/5004276/204610 – Michael Jan 17 '16 at 10:08
  • Michael, I have tried which was also not working. – J. Deloers Jan 18 '16 at 15:01

2 Answers2

0

You send action in url as GET parameter, but check as POST parameter. Try to change check to if ((isset($_GET['action']) && $_GET['action'] == "postData")... or add action to data postData.action = 'postData';

Also correct data to data: postData, postData is allready object;

Max P.
  • 5,579
  • 2
  • 13
  • 32
  • Also there is mistake in action check, `isset` returns boolean - false/true, so you can't compare it with "postData". Change check to `if ((isset($_GET['action']) && $_GET['action']== "postData")..` or `if ((isset($_POST['action']) && $_POST['action']== "postData")..` regarding of how you will send action parameter – Max P. Jan 17 '16 at 10:27
  • I don't see changes in your php code, so I can't tell why nothing changed. – Max P. Jan 17 '16 at 15:05
  • 1
    Unmark my answer as accepted if problem still exist. Accepted answer means that problem is solved. – Max P. Jan 18 '16 at 09:32
  • I've tested your code on my system. Post works. I modified code a little - http://pastebin.com/NNu4283v, http://pastebin.com/m8cqr9Un. As result I get `{"action":"postData","createVerzuimServiceReport1":"createVerzuimServiceReport1","fromdate_submit":"2016-01-02","enddate_submit":"NaN-aN-aN","studentsearchfield":null}` – Max P. Jan 18 '16 at 20:40
  • How can I contact you -Max P.? – J. Deloers Jan 18 '16 at 21:09
  • Regarding this question you can write here. Or you have other questions? – Max P. Jan 18 '16 at 22:02
  • Yes I have questions, I appreciate your effort tremendously. But I replaced the code and it does work in my php script. I do not receive the params, I do not know what is going wrong. The parameters are not present in my PHP. I really tried everything and searched the whole net for a solution with respect to the $ ajax post. While it is quite straightforward. And I tried out very much manners to get it work. But I want to fix this. I am willing to pay for the solution. – J. Deloers Jan 18 '16 at 22:31
  • You can find in my profile http://stackexchange.com/users/6896160/max-p link to my profile on upwork. – Max P. Jan 18 '16 at 22:36
  • Sorry, I do not understand this message. What is your username on upwork. thanks. – J. Deloers Jan 18 '16 at 22:55
0

postData is already an object. So, don't put it into another object by using {....} around. Change this line

data: { postData },

To

data: postData,