HTML Form Cannot POST

2019-08-04 16:22发布

问题:

I am making a user system on a website and I cannot get the form to post to the file.

Here is the form in the HTML file:

<form method="post" action="php/userlogin.php">
          <p><input type="text" name="usernameL" value=""></p>
          <p><input type="password" name="passwordL" value=""></p>
          <p><input type="submit" name="submit" value="Login"></p>
</form>

And the userlogin.php in the php directory:

<?php

$username = $password = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  $username = test_input($_POST["usernameL"]);
  $password = test_input($_POST["passwordL"]);
}

function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data; 
}

?>

I'm new to forms and can't find an answer anywhere or even a question like this. How would I fix this?

回答1:

You code is working fine.

The problem might be with the file structure. Please check that.

Ex: If your html file in the root folder of your project, Then the userlogin.php files should be there in project_root_folder/test/

So the file structure should be...

Root/
    index.html
    Test/
        userlogin.php


回答2:

Code is fine. Just output the values of the variables.

echo $username.'  '.$password;

You can see that the data is being posted.



回答3:

Well your code seems to work perfectly fine, maybe the problem is with your testing enviroment, you need solution like XAMPP https://www.apachefriends.org/ to run php scripts on your computer. Other way is to run scripts remotely on some free webhosting that supports php.

If this is not the case then to check if actually data was sent, modify your code this way:

...
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  $username = test_input($_POST["usernameL"]);
  $password = test_input($_POST["passwordL"]);
  echo $username."<br/>";
  echo $password."<br/>";
}
...


标签: php html forms