0

I recently began to learn TypeScript and PHP, and I'm came across some weird result when using AJAX. I have the typical HTML form to send data to a PHP script, but instead of using the submit type in the form, I call a function built in TypeScript (and transpiled to JS) to check if the data in the form is correct (validate for empty strings etc etc) and then send all that info to the PHP script. For testing purposes I sent to the PHP script, via POST, only one value, to check if everything was working as it should. THe result I was hoping for was an alert in the browser with the value of the data I passed using AJAX, but, instead of that, I got an alert with all the contents of the PHP script, and I have no idea why.

Here is the code so far:

This is the function who handles the AJAX request

function sendData(sueldo:number){

let xhttp : XMLHttpRequest =  new XMLHttpRequest();
xhttp.open("POST", "./administracion.php", true);
xhttp.setRequestHeader("content-type","application/x-www-form-urlencoded");

xhttp.send("sueldo="+sueldo);

xhttp.onreadystatechange = () => {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
        alert(xhttp.responseText);
    }
}

And this is the PHP script that receive that request

<?php
if(isset($POST['sueldo']))
{
    echo "Valor recuperador por POST: " . $POST['sueldo']; 
}

?>

And this is the result I get when I submit the data in the form:

enter image description here

Any help is greatly appreciated

Thanks!

El.Psy.Kongroo
  • 117
  • 1
  • 7
  • PHP is not running on your server – tkausl Sep 25 '18 at 20:01
  • Check if PHP is installed in your server, open a terminal and execute `php -v` – rescobar Sep 25 '18 at 20:03
  • Apparently is on: PHP 7.2.10-0ubuntu0.18.04.1 (cli) (built: Sep 13 2018 13:45:02) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies with Zend OPcache v7.2.10-0ubuntu0.18.04.1, Copyright (c) 1999-2018, by Zend Technologies – El.Psy.Kongroo Sep 25 '18 at 20:10

0 Answers0