0

Please be patient. This is my first post as far as I remember.

This is a part of my calendar.js script. I'm trying to POST data that I fetch from modal window in index.php to sql.php.

function saveModal() { /*JQuery*/
    var current_date_range = $(".modal-select#daterange").val();
    var current_room_number = $("#mod-current-room-number").val();
    var current_room_state = $("#mod-current-room-state").val();
    var myData = {"post_date_range": current_date_range, "post_room_number": current_room_number, "post_room_state": current_room_state};
    var myJSON = JSON.stringify(myData);

    $.ajax({
        type: "POST",
        url: "sql.php",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: true,
        data: myJSON,
        beforeSend: function() {
            $("#ajax").html("<div class='loading'><img src='/images/loader.gif' alt='Loading...' /></div>");
        },
        success: function(result){
            $("#ajax").empty();
            $("#ajax").html(result);
            $("#ajax").fadeIn("slow");
            window.location.reload(true);
        },
        error: function(){
            alert(myJSON);
            $("#ajax").html("<p class='error'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>Oops!</strong> Try that again in a few moments.</p>");
        }
    })    
}

I get the data just fine (as you can see I have checked in the error: function() with alert(myJSON);). It looks like this: {"post_date_range":"12/19/2018 - 12/28/2018","post_room_number":"118","post_room_state":"3"}. Nevermind that the daterangepicker.js returns dates in the hideous MM/DD/YYYY format, which I would very much like to change to YYYY-MM-DD. The real problem is, the code never gets to success: function().

Now my sql.php is in the same folder as calendar.js and index.php.

In sql.php I try to retrieve those values with:

$currentDateRange = $_REQUEST['post_date_range'];
$currentRoomNumber = intval($_REQUEST['post_room_number']);
$currentRoomState = intval($_REQUEST['post_room_state']);

I have checked many other SO Q&As and none have helped me solve my problem. I don't see any spelling errors. It's not disobeying same origin policy rule. I don't want to use jQuery $.post function. Anyone sees the obvious solution?

s3c
  • 1,481
  • 19
  • 28
  • Are you able to get your post data array in your PHP file ? – Harsh Madaiyar Dec 05 '18 at 07:21
  • remove this `dataType: "json",` line and check – Vel Dec 05 '18 at 07:21
  • removing `dataType: "json",` didn't help. I don't believe I get any data to `sql.php` since it never reaches the content of `success: function ()`. Also changing `$_REQUEST` to `$_POST` (answer below) makes no difference, which is to be expected with the same reasoning in the previous sentence. IMO. – s3c Dec 05 '18 at 07:35
  • 1
    Did you debug your `sql.php`? Could you post `var_dump($_POST)` from your `sql.php` ? Also try to catch the error: `error:function (xhr, ajaxOptions, thrownError) { alert(xhr.status); alert(thrownError); }` – AnTrakS Dec 05 '18 at 07:35
  • Possible duplicate of https://stackoverflow.com/questions/18866571/receive-json-post-with-php – misorude Dec 05 '18 at 07:44
  • If you are sending JSON, PHP will _not_ provide the data in $_POST/$_REQUEST; you need to specifically read it yourself, see duplicate. – misorude Dec 05 '18 at 07:44
  • You should `json_decode` the request body from sql.php. – Sumesh TG Dec 05 '18 at 08:08

3 Answers3

1

You want to send array in post rather than the string so directly send myData to get array value in your PHP file rather converting to JSON string It would work with your current PHP file as you require.

Harsh Madaiyar
  • 166
  • 1
  • 4
  • Just changing `myJSON` to `myData` in the AJAX doesn't work. – s3c Dec 05 '18 at 07:40
  • by changing this you can get your array in $_Request.... you can check that by print the data using echo in your PHP file. what kind of result you expect in AJAX response then I can check and help you. – Harsh Madaiyar Dec 05 '18 at 08:57
1

You should specify a POST key for the JSON data string you are sending:

var myJSON = JSON.stringify(myData);
(...)
$.ajax({
    (...)
    data: 'data=' + myJSON,

You need to parse (decode) this string in your PHP file to be able to use it as an array/object again:

$data = json_decode($_REQUEST['data']);
$currentDateRange = $data['post_date_range'];
$currentRoomNumber = intval($data['post_room_number']);
$currentRoomState = intval($data['post_room_state']);

Also, dataType in jQuery.ajax function is specified as "The type of data that you're expecting back from the server." according to jQuery documentation. As far as I can tell from your code, you might rather expect something else as your response, so try excluding this line.

Peter
  • 153
  • 1
  • 7
  • This is also true as I have found out, although for the time being I solved it with non-JSON form `var myData = $("#modal-form").serializeArray();`. But it is my understanding that it is better practice to use JSON form, since it can easily be changed and used from different set-ups. TY, @Peter – s3c Dec 05 '18 at 16:31
0

I am sorry to have burdened you all. It's my third week programming so my lack of knowledge is at fault. I did not know how dataflow works between AJAX and PHP in the URL...

While I was searching for other errors, I printed or echoed out many different things. The PHP file should echo only 1 thing, although it can be an array with multiple values.

Like this for example:

$return_arr = array("sql"=>$sql1, "result"=>$result, "out"=>$out);
echo json_encode($return_arr);

I appologize again.

s3c
  • 1,481
  • 19
  • 28