PHP post throws 404 error after submit - php

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 u r 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>

Related

Check if the form is submited does not work

I want to echo something once a form is submitted. But when I click the submit button, it seems that the page is just refreshing itself and I do not see the word that I have written in the echo section. Here is my code:
<?php
if (isset($_POST['submit'])) {
echo "submitted";
}
?>
<h3>Post your form here</h3>
<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<label>Insert a title here</label><br>
<input name="title" type="text" placeholder="add a title"><br><br>
<label>Insert the body here</label><br>
<textarea name="body" placeholder="insert the body here "></textarea><br><br>
<input type="submit" value="submit" name="submit"><br>
</form>
I also tried the code by removing the isset function, but that did not work, either.
In form action, you have missed an echo.
action="<?php echo $_SERVER['PHP_SELF']; ?>"
This is working code. I test it on my Machine.
Why you don't use else statement to test it better.
here is the code.
Note (Check your localhost server settings)
<?php
if (isset($_POST['submit'])) {
echo "submitted";
}
else
{
echo "Not working";
}
?>
<h3>Post your form here</h3>
<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<label>Insert a title here</label><br>
<input name="title" type="text" placeholder="add a title"><br><br>
<label>Insert the body here</label><br>
<textarea name="body" placeholder="insert the body here "></textarea><br>
<br>
<input type="submit" value="submit" name="submit"><br>
</form>
Actually the credit goes to #ashok. He was right that I needed to check localhost server settings.
I am using Phpstorm to write PHP codes. Whenever I click on the Chrome browser to see the results, it takes me to
http://localhost:63342/ name of the file page.
The port 63342 is the default port used by Phpstorm. Since I am using Xampp and it runs on port 8080. I changed port number 63342 to 8080, and it worked.
If you are submitting to the same page, you really dont need to define an action.
Instead of isset, use !empty as such
if(!empty($_POST['submit'])){
echo "success";
}
I must bring to your attention that if the suggestions don't work, you should start with debugging what you are actually receiving from the form POST, using:
<pre>
<?php var_dump($_POST); ?>
</pre>
Turning on error reporting in your php settings is also a good start for debugging.

Conflict On Two Post on Single Page Form(HTML) and PHP

I need to keep two forms for login and Logout in one single page including PHP and HTML like
<html>
<body>
<form action="" method="post">
<input type="text" name="username">
<input type="submit" name="SubmitButton" value="Get in"/>
</form>
<form action="" method="post">
<input type="submit" name="logoutButton" value="Logout"/>
</form>
</body>
</html>
on PHP part I have
<?php
if(isset($_POST['SubmitButton'])){
$inputuser = $_POST['username'];
$_SESSION['user'] = 'A';
}
if(isset($_POST['logoutButton'])){
unset($_SESSION['user']);
header('Location: http://somewhere.com/');
}
As I said I have to keep everything on one single page but this looks like causing conflict between the POST(s) can you please let me know how to stop this and target each Post properly?

404 error - The requested URL/< was not found on this server. why?

I've just finished setting up AMP on a new MacOS El Capitan installation. I don't know why but every time I try to run a simple HTML form with some PHP processing I get a 404 error. I've tried it with the default MacOS document root, I've also tried with the user-level document root, and I've even installed MAMP as a last-resort move. In all these setups the result is always the same when I hit the submit button: 404.
<?php if(isset($_POST['submit'])) {
$name = $_POST['name'];
$post = $_POST['post'];
$link = $_POST['link'];
echo $name;
echo $post;
echo $link;
echo "all good";
}
?>
<form name="upload" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
Name:<br>
<input type="text" name="name"><br>
Post:<br>
<input type="text" name="post"><br>
Link:<br>
<input type="text" name="link"><br>
<input type="submit" name="submit" value="Submit Form"><br>
</form>
The error is "The requested URL/< was not found on this server."
What am I missing??
Many thanks in advance for your help!
Looks like the action for your form isn't parsing correctly. Your code runs fine on my server as it is - however, every server is different.
You could try moving the double quotes inside the php section to see if that's tripping up the parser somehow:
Change
<form name="upload" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
to
<form name="upload" action=<?php echo "\"".htmlspecialchars($_SERVER['PHP_SELF'])."\""; ?> method="post">
or alternatively
<form name="upload" action=<?php echo "'".htmlspecialchars($_SERVER['PHP_SELF'])."'"; ?> method="post">
In theory all of these should result in the same thing - but it's worth a try.

Cannot submit HTML form using PHP on iPad

Recently I got this problem that I cannot solve. My HTML form cannot be submitted by using iPad, but the same form CAN be submitted from my laptop (windows machine). Please check this out:
http://www.gomap.ch/admin/pages/test.php - this is submitted correctly
http://ipadpreview.com/previewer?url=www.gomap.ch/admin/pages/test.php - this is not submitted.
This is a very simple form, and code goes like this:
<?php
error_reporting(0);
if($_POST['mirko']){
echo 'hello, Mirko';
}
?>
<form action="" method="POST">
<input type="submit" value="Submit" name="mirko"/>
</form>
Any help, please??
Thanks a lot!
You are not using valid html.Replace your code with this and have u tried on real ipad.
<html>
<head></head>
<body>
<?php
error_reporting(0);
if($_POST['mirko']){
echo 'hello, Mirko';
}
?>
<form action="" method="POST">
<input type="submit" value="Submit" name="mirko"/>
</form>
</body>
</html>
You need to add the name of the form which holds the php code into the action. In this case it would be the same form you're on. Follow what I did below with nameOfFile.php being the name of the file you posted.
<?php
error_reporting(0);
if($_POST['mirko']){
echo 'hello, Mirko';
}
?>
<form action="nameOfFile.php" method="POST">
<input type="submit" value="Submit" name="mirko"/>
</form>

PHP login script 404 error

I used a login script tutorial here: http://www.astahost.com/info/tilltf-simple-login-script-simple-secure-login-script.html and when I clocked login, there was a 404. When i went back, I was logged in. I followed the the tutorial exactly as it said. Any idea why it is going to this weird page?
<?php
session_start();
require_once 'database.php';
if (isset($_SESSION['user'])){
echo "Welcome ".$_SESSION['user'];
?>
<form name="logout" method="post" action="logout.php">
<input type="submit" name="logout" id="logout" value="Logout">
</form>
<br /><form name="news" method="post" action="news.php">
<input type="submit" name="news" id="news" value="News">
</form>
<?php
}
elseif(isset($_SESSION['admin'])){
echo"Welcome ".$_SESSION['admin'];
echo"<br><br>You are logged in as an Admin";
?>
<form name="logout" method="post" action="logout.php">
<input type="submit" name="logout" id="logout" value="Logout">
</form>
</form>
<?php
}else{
?>
<form name="login_form" method="post" action="login2.php">
<label>
<input name="user" type="text" id="user">ID<br />
<input name="pass" type="password" id="pass">Password<br />
</label>
<input type="submit" name="login" id="login" action="index.php" value="Login">
</label>
</p>
</form>
<form name="Register" method="post" action="reg.php">
<input type="submit" name="register" id="register" value="Register">
</form><br />
<form name="news" method="post" action="news.php">
<input type="submit" name="news" id="news" value="News">
</form>
<?php
}
?>
Glancing at the code you linked to, I noticed login2.php tries to redirect to Index.php in some cases, but to index.php (lowercase i) in other cases. It should be lowercase in both places. That might be the cause of your 404.
That tutorial has a couple header redirects:
header("location:Index.php");
and
header("location:index.php");
Make sure they're not supposed to be pointing to the same file, and make sure that file exists. It's logging you in, and then trying to redirect you to a file it can't find.
Depending on your type of server, this might cause problems:
if(mysql_num_rows($queryN) == 1) {
$resultN = mysql_fetch_assoc($queryN);
$_SESSION['user'] = $_POST['user'];
header("location:Index.php");
}
Specifically because the I in Index.php is capitalized, while all other references to the file utilize a lowercase i.
Also, this code is TERRIBLY formatted... but alas that's a different problem altogether.
Looking over the code at the link you provided, I see a likely cause. At the end of the login processing script, there's a call to header() which redirects back to the index page. One of them has different casing, and many web servers are case-sensitive by default:
header("location:index.php");
header("location:Index.php");
I'm guessing the lower case index.php is the correct one, so update your code accordingly.
just an added warning: the linked example has a serious security hole along these lines
$escaped_username = mysql_real_escape_string($username);
# make a md5 password.
$md5_password = md5($_POST['pass']);
$queryN = mysql_query("select * from user where username = '".$username."' and password = '".$md5_password."' AND level='1'");
$escaped_username is generated correctly, but the sql query still uses $username. this opens up the possibility to inject sql statements. be sure to change it.

Categories