简体   繁体   中英

PHP post throws 404 error after submit

I am trying to make simple login system. But I encountered a problem with post method. Simple register.php file is not working:

<?php
if (isset($_POST['username'])) {
    print('text');
}
?>

<html>
<body>
    <form method="post">
        <input type="text" name="username" required/>
        <button type="submit" name="submit">Register</button>
    </form>
</body>
</html>

After pressing a button it opens 404 error page.

I searched through dozens of similar questions but still got no solution. I'm working on localhost, PhpStorm 2016.3.2 and PHP7.0 and Ubuntu 16.04

SOLUTION: It was built-in PhpStorm server's fault. It's not working when post method is used.

I'm using now Lampp and post works fine.

It's work. Are you start Xampp Server or Lampp Severr on your Ubuntu ? I think you forgot to start Server

Try:

<?php
if (isset($_POST['submit'])) {
$username = $_POST['username'];
echo $username;
}
?>

<html>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <input type="text" name="username" required />
    <button type="submit" name="submit">Register</button>
</form>
</body>
</html>

Give the action attribute to the form tag and see.. Here I have added index.php.. U can change it to the same file ur working on.. This might solve the problem.

 <form action="index.php" method="post"> <input type="text" name="username" required/> <button type="submit" name="submit">Register</button> </form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM