0

SOLVED: apparently the only thing going wrong was that i forgot to add quotation marks around the parameters in the query. Thanks for the help everyone!

i'm having the problem that for some reason the variables that should be filled in my php file are empty. I am doing a $.ajax post. Code below. i already made sure that the data is actually filled when sending by console.log() on the model that data is being filled with.

The logged model:

addedTime: Fri Jul 26 2019 11:01:07 GMT+0200{}
customer: "Test"
message: "test"
message_end_datetime: "2019-07-29"
message_start_datetime: "2019-07-27"

Controller that is doing the post:

$scope.InsertNew = function(){
    var model = {
        "addedTime" : new Date(),
        "customer" : document.getElementById("customer").value,
        "message" : document.getElementById("message").value,
        "message_start_datetime" : document.getElementById("startTime").value,
        "message_end_datetime" : document.getElementById("endTime").value
    };
    console.log(model);
    var dump = $.ajax({
        type: "POST",
        url: "http/NewMessage.php",
        data: JSON.stringify(model),
        contentType: "application/json; charset=utf-8",
        success: function () {
        console.log("succes");
}
      });
}

Then in NewMessage.php :

<?php
include "include.php"; // has the query php file. 
// $addedTime = $input['addedTime'];
$addedTime = $_POST["addedTime"];
$customer = $_POST["customer"];
$message = $_POST["message"];
$message_start_datetime = $_POST["message_start_datetime"];
$message_end_datetime = $_POST["message_end_datetime"];
newMessageQuery($addedTime,$customer,$message,
$message_start_datetime,$message_end_datetime);

Finally in the query file:

// doenst work
    function  newMessageQuery($addedTime,$customer,$message,$message_start_datetime,$message_end_datetime){
    $recordsQuery = " INSERT into customernews (added_datetime,customer,message,message_start_datetime,message_end_datetime) 
                        values ('2019-07-26',$customer,$message,'2019-07-26','2019-07-26') "; 
        return executeObjectQuery($recordsQuery);  // executes the query  
    }

//works
    function newMessageQuery($addedTime,$customer,$message,$message_start_datetime,$message_end_datetime){
        $recordsQuery = " INSERT into customernews (added_datetime,customer,message,message_start_datetime,message_end_datetime) 
                            values ('2019-07-26','test','testklant','2019-07-26','2019-07-26') "; 
            return executeObjectQuery($recordsQuery);    
        }

As seen above i already tested the query by only using default hard coded values and then it works.

However when using one of the parameter values that should be passed on it doesn't work, im not getting any php errors because it does return succes in javascript which leads me to believe the variables are not getting filled in NewMessage.

Am i doing something wrong when i pass the values with post or is it something else?

EDIT: im getting string(0) "" as the response, response below.

Request URL: ../http/NewMessage.php
Request Method: GET
Status Code: 200 OK
Remote Address: 10.0.28.167:80
Referrer Policy: no-referrer-when-downgrade
Connection: Keep-Alive
Content-Length: 13
Content-Type: text/html; charset=UTF-8
Date: Fri, 26 Jul 2019 09:43:15 GMT
Keep-Alive: timeout=5, max=99
Server: Apache/2.4.23 (Win32) OpenSSL/1.0.2h PHP/5.6.28
X-Powered-By: PHP/5.6.28
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
Accept-Encoding: gzip, deflate
Accept-Language: nl-NL,nl;q=0.9,en-US;q=0.8,en;q=0.7
Cache-Control: no-cache
Connection: keep-alive
Host: retail-web-03
Pragma: no-cache
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36

SOLVED: apparently the only thing going wrong was that i forgot to add quotation marks around the parameters in the query. Thanks for the help everyone!

TerranGaming
  • 365
  • 1
  • 2
  • 10

1 Answers1

0

You are send data in Body of the request. $_POST method is only for form-data, Here the format is application/json.

So you need to use file_get_contents() instead of $_POST. Your code will be like,

$inputJSON = file_get_contents('php://input');
$input = json_decode($inputJSON, TRUE);

$addedTime = $input["addedTime"];
$customer = $input["customer"];
$message = $input["message"];
$message_start_datetime = $input["message_start_datetime"];
$message_end_datetime = $input["message_end_datetime"];
BadPiggie
  • 5,471
  • 1
  • 14
  • 28