0

I'm setting up a simple form on my local server, but the POST and GET methods both return nothing.

I discovered this problem when making my own form. Then I tried using online tutorial codes to see if they have the same problem, they do. I'm almost sure that it is not the codes problem because of this. When I 'fix' these codes by using isset() or ??'' the resulting page is blank.

Im using Windows 10 x86, PHP 7, node.js local server, code copied from https://www.w3schools.com/php7/php7_forms.asp and https://www.w3resource.com/php/super-variables/$_REQUEST.php .

Does anybody know if there's anything wrong with PHP or is it something else?

<html>
<body>

<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>

</body>
</html>
<html>
<body>

Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>

</body>
</html>

webserver.js

var express = require('express');
var app = express();

var execPHP = require('./execphp.js')();

execPHP.phpFolder = 'C:\\Users\\Dshop\\Desktop\\php-7.3.3\\Server1';

app.use('*.php',function(request,response,next) {
    execPHP.parseFile(request.originalUrl,function(phpResult) {
        response.write(phpResult);
        response.end();
    });
});

app.listen(3000, function () {
    console.log('Node server listening on port 3000!');
});

execphp.js

/**
*
*/
class ExecPHP {
    /**
    *
    */
    constructor() {
        this.phpPath = 'C:\\Users\\Dshop\\Desktop\\php-7.3.3\\php.exe';
        this.phpFolder = '';
    }   
    /**
    *
    */
    parseFile(fileName,callback) {
        var realFileName = this.phpFolder + fileName;

        console.log('parsing file: ' + realFileName);

        var exec = require('child_process').exec;
        var cmd = this.phpPath + ' ' + realFileName;

        exec(cmd, function(error, stdout, stderr) {
            callback(stdout);
        });
    }
}
module.exports = function() {
    return new ExecPHP();
};

what shows up in server

what results in

12
  • 1
    isset() is not a fix it or something similiar is a pre-requisite Commented Mar 26, 2019 at 12:32
  • Remember there are a number of ways to launch a web page. ONLY when you launch it from the SUBMIT of a form will the $_POST or $_GET arrays actually get filled Commented Mar 26, 2019 at 12:34
  • However I do not see a reason why these 2 scripts would not work, if you submit the form to the second script Commented Mar 26, 2019 at 12:36
  • @alex are you sure that PHP is actually configured properly? What happens if you do <?php echo "hello world"; ?> do you get the expected string? I cant see anything fundamentally wrong with your code. Commented Mar 26, 2019 at 12:36
  • 1
    "PHP 7, node.js local server" — How are you mixing Node.js and PHP? Commented Mar 26, 2019 at 12:53

1 Answer 1

1

The code you are using to run the PHP is very basic. The key thing missing that is relevant to your interests is that you do nothing to populate $_POST. You're simply executing the PHP program with no input.

To run PHP from inside a Node.js program, you should probably use a module like node-php.

You might be better off using something like Apache HTTPD to run the PHP (with mod_php) and forward requests to a separate Node.js server for code running there (using mod_proxy).

Sign up to request clarification or add additional context in comments.

5 Comments

i was just about to say this in a comment.
Ill try this. Also, could I get some explanation on how $_POST is not populated? Since I did add some input in before I submitted the form. Im new, explain please.
"could I get some explanation on how $_POST is not populated" — You don't put data in a place where PHP will take it and put it in $_POST therefore $_POST has no data in it.
@Quentin Shouldn't the text boxes be a place to put data in? I think I did that. (two imgur links)
The data is in the HTTP request you send to Node.js. The Node.js code then ignores it and runs the PHP without doing anything to send the data to the PHP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.