0

I contacted GoDaddy about this, in case it is something on their end, but they couldn't really help. I'm trying to send a contact form via JSON in a xmlhttprequest, and I'm sending it via a POST request. Unfortunately, and this is something that wasn't happening on another hosting service, it is being received as a get request. Here is my js code, and what Chrome is displaying after sending it:

code:

function SendJson(values, type){
    var xhr = new XMLHttpRequest();
    var url = 'http://www.klemequestrianestates.com/processes/mail_form.php';
    var data = JSON.stringify(values);
    console.log(data);
    xhr.open('POST', url);
    xhr.setRequestHeader('Form-Type', type);
    xhr.setRequestHeader("Content-Type", "application/json");
    xhr.send(data);
    xhr.onreadystatechange = function(){
        if(xhr.readyState === 4){
            //success confirmation
        }   
    };
}

Where Values is a js object, and type is either 'sell' or 'buy' (the form has 2 different types of clients that might use it, and sellers and buyers have different fields.

Chrome Debugger: enter image description here

Here is the PHP file:

<?php
header('Access-Control-Allow-Headers: *');
header("Access-Control-Allow-Methods: POST, GET");      


$data_type = $_SERVER['HTTP_FORM_TYPE'];

$raw_JSON = file_get_contents('php://input');
$converted_JSON = json_decode($raw_JSON);
echo var_dump($converted_JSON);
$type = $_SERVER['REQUEST_METHOD'];
echo var_dump($type);

if($data_type === 'sell'){
    $first_name = $converted_JSON->div_00;
    $last_name = $converted_JSON->div_01;
    $phone_num = $converted_JSON->div_02;
    $email = $converted_JSON->div_03;
    $street = $converted_JSON->div_04;
    $city = $converted_JSON->div_05;
    $state = $converted_JSON->div_06;
    $zip = $converted_JSON->div_07;
    $option = $converted_JSON->div_08;
    $message = $converted_JSON->div_09;

    $to = 'contact@joshualyness.com'; 
    $subject = $first_name.' '.$last_name.', '.$option;
    $body = 'From: '.$email.PHP_EOL.PHP_EOL.'Phone Number: '.$phone_num.PHP_EOL.PHP_EOL.'Address: '. $street.PHP_EOL.$city.PHP_EOL.$state.PHP_EOL.$zip.PHP_EOL.PHP_EOL.'Message: '.$details;
    mail($to, $subject, $body);
}

Here is the var dump contents: enter image description here

Any ideas why this is happening? If you need any more details, drop a comment and I'll see what I can do. I know it's a bit of a weird question, I wouldn't be bothering the community if I didn't have any other ideas. Thanks.

Josh Lyness
  • 102
  • 1
  • 12
  • I'm receiving an email from this form, so I know it is definitely processing. The fields from the form are all empty, because the GET cant receive anything from php's input stream (from the empty NULL var dump which is supposed to contain div_00 through to div_09) – Josh Lyness Mar 02 '18 at 15:33
  • 1
    Why do you think it's not a POST? javascript can't send a GET request with a payload so it's definetely a POST request – Axnyff Mar 02 '18 at 15:34
  • if you use something like https://www.hurl.it/ and send a post request your site responds with `NULL string(4) "POST"` – Namaskar Mar 02 '18 at 15:34
  • I'm curious why you are doing this odd injection `$_SERVER['HTTP_FORM_TYPE']` pass, when that value would better be handled as a `$_POST['form_type']` variable. But I see a "Payload", which suggests POST vars are being sent ok. This Q may have some insights: https://stackoverflow.com/questions/26506168/why-is-serverrequest-method-always-get – IncredibleHat Mar 02 '18 at 15:51
  • @Axnyff, I was thinking that maybe some server config was turning it into a GET after the js sends it, I'll look into it – Josh Lyness Mar 02 '18 at 16:46
  • @SvenWritesCode, nope, im not using hurl or any other plugin/api. Just plain js and php. – Josh Lyness Mar 02 '18 at 16:47
  • 1
    @IncredibleHat, I've had issues with accessing $_POST, but I'll switch it over and check if it works. It's copied code from another project and I know it wasn't working on that. Thanks though! – Josh Lyness Mar 02 '18 at 16:48

0 Answers0