Now that I've posted the content to my PHP script, how do I get it?


Irasona

I posted the content with the following code:

function checkphp() {
    var xml = new XMLHttpRequest();
    xml.open("POST", 'testphp.php');
    xml.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xml.send('alame=Henry');
    xml.onreadystatechange = function() {
        if (xml.readyState == 4 && xml.status == 200) {
            document.body.innerHTML = xml.responseText;
        }
    }
}

I tried this:

echo($_POST['alame']) or die("Nothing was posted");

But it doesn't work...it spits out "1". Any suggestions?

Pascat

You will die 1because PHP is echoing the value of ($_POST['alname'])( true);

$someVar = 'test';
$someBool = false;

$a = ($someVar) || die();
echo $a; // 1
var_dump($a); //bool(true)

$a = ($someBool) || die(); // dies

In a language like JavaScript, you might expect different behavior:

// In JS
var someVal = false;
var somethingElse = someVal || "fallback";
console.log(somethingElse); // "fallback"

However, in PHP, the entire expression is evaluated, so

// In PHP
$someVal = false;
$somethingElse = $someVal || "fallback";
echo $somethingElse; // 1

$someVal || "fallback"evaluates to true because PHP boils it down to(false || true) => true

@jacouh is wrong because it's unpredictable 1, but completely true that you should check the variable some other way, like a ternary or similar, e.g.

if(!isset($_POST['alame']))
    die("Noting was posted");

// Continue and do something with $_POST['alame'];

Related


How do I get the content of a Slurm script?

Jingnan Jia I submitted a job a few days ago and it's still running. But I forgot the content of script.shthat day . and script.shhas been removed. Do you know how to restore the contents of this script? Damien Francois In the latest version, you can retrieve

How do I get the content of a Slurm script?

Jingnan Jia I submitted a job a few days ago and it's still running. But I forgot the content of script.shthat day . and script.shhas been removed. Do you know how to restore the contents of this script? Damien Francois In the latest version, you can retrieve

How do I get the content of a Slurm script?

Jingnan Jia I submitted a job a few days ago and it's still running. But I forgot the content of script.shthat day . and script.shhas been removed. Do you know how to restore the contents of this script? Damien Francois In the latest version, you can retrieve

How do I get the content of a Slurm script?

Jingnan Jia I submitted a job a few days ago and it's still running. But I forgot the content of script.shthat day . and script.shhas been removed. Do you know how to restore the contents of this script? Damien Francois In the latest version, you can retrieve

How do I get the content of a Slurm script?

Jingnan Jia I submitted a job a few days ago and it's still running. But I forgot the content of script.shthat day . and script.shhas been removed. Do you know how to restore the contents of this script? Damien Francois In the latest version, you can retrieve

How do I view the history of what I've posted in Google Chrome?

Tomas Aschan I just submitted a form with a textbox in which I wrote a long text. In the other textbox, I filled in a date in the wrong format - instead of getting an error, the site acted as if my form submission was valid, it just didn't save anything. Is th

How do I get media posted by friends I follow on Instagram?

DG2903 I'm trying to use the Instagram API to retrieve all the recent pictures and videos my friends have posted on their Instagram. I looked at all the endpoints in the Instagram documentation and couldn't find any that would allow me to do this. Am I missing

JavaScript: How do I get the json.file posted to my code?

Susie Imagine if I wanted to wait to get JSON files if they were posted to me. I can get it. If not I wait for it. I want to get and store it and parse it for processing. (I need to receive requests) How do I have a JSON file that I can have it get what I need

How do I moderate Facebook comments posted on my website?

Russ 4U In meta, I added <meta property="fb:admins" content="myID"/> <meta property="fb:moderator" content="myID" /> I tried and personally code block <div id="fb-root"></div> <script>(function(d){ var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {ret

How do I get my bash script to push changes to Githhub?

Atif Imam So, I'm just a beginner in git. So, I want to make a bash script file that just contains all the commands I need to push to git. I just need to execute that script. #!/bin/sh ########################### # add all added/modified files git add * # com

How do I update the content in my database?

Dulwich I'm trying to update a value I get from the database, but it doesn't seem to go to the routing function I tried changing the routing method and even stopping the request but it doesn't seem to work this is the route Route::put("/users/bonus/update/{id}

How do I update the content in my database?

Dulwich I'm trying to update a value I get from the database, but it doesn't seem to go to the routing function I tried changing the routing method and even stopping the request but it doesn't seem to work this is the route Route::put("/users/bonus/update/{id}

How do I get my PHP source code length?

贾诺斯 · 提吉 (JánosTigyi) I have a very large PHP script and I want to know how many characters it has. Is there any software that can figure out how many files and characters are in my project? dirty bits As mentioned by Mark Baker, the desired solution: phploc T

In PHP, how do I get to my class/public function?

William John I'm working on some php code where I'm trying to access a class/public function so I can do an if/else function with a database connection. I've only used another php file before (dirname( FILE ), but I'm not sure how to adjust that code to get to

How do I get my PHP to send mail?

JM at work I installed Ubuntu 11.04, LAMP using tasksel. I found that PHP mail()is not working properly. I guess I need to enable it? How do I do this? bothersome From php's mail manual: Require In order for the mail functionality to be available, PHP requires