<?php
if(isset($_POST['submit']))
{
$name = $_POST['name'];
echo "User Has submitted the form and entered this name : <b> $name </b>";
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="name"><br>
<input type="submit" name="submit" value="Submit Form"><br>
</form>
i want only if part to run after submit and display form should not be seen after clicking submit form button
Write your html form code inside else part.
<?php
if(isset($_POST['submit']))
{
$name = $_POST['name'];
echo "User Has submitted the form and entered this name : <b> $name </b>";
} else {
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="name"><br>
<input type="submit" name="submit" value="Submit Form"><br>
</form>
<?php } ?>
Use exit.
if(isset($_POST['submit']) {
$name = $_POST['name'];
echo "User has submitted the form and entered this name : <b> $name </b>";
exit;
}
Related
I am new in php tried to write script that read and display data on same page.
Here is my code.
<?php
$fnameErr = $passErr = "";
$fname = $password = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["fname"])) {
$fnameErr = "Name is required";
} else {
$fname = $_POST["fname"];
}
if (empty($_POST["password"])) {
$passErr = "Password is required";
} else {
$password = $_POST["password"];
}
}
?>
<p><span class="error">Field Required</span></p>
<form method="post" action="assignment.php">
Name: <input type="text" name="fname">
<span class="error">* <?php echo $fnameErr;?></span>
<br><br>
E-mail:<input type="text" name="password">
<span class="error">* <?php echo $passErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
After running it on web browser it display an error.
Object not found!
The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.
If you think this is a server error, please contact the webmaster.
Error 404
localhost
Apache/2.4.37 (Win32) OpenSSL/1.1.1 PHP/7.2.12
You should remove your form action - by default the form action is set to the current page
You can test this by adding this into your current .php file (above everything else):
<?php
if (!empty($_POST)) {
echo '<pre>'. print_r($_POST, 1) .'</pre>';
}
?>
<form method="post">
<input type="text" name="foo" />
<input type="submit" name="submit" />
</form>
You'll notice on initial load, the $_POST array won't dump out to the screen. Fill out the form and it will "refresh", posting the input value into the $_POST data with the key foo. This is now shown on the page.
In this program you just creating a form and not specifying any script that display data.
just simply write this script where form creation ends.
<?php
echo "<h2>Your Data:</h2>";
echo $fname;
echo "<br>";
echo $password;
?>
<html>
<body>
<h1>NewsLetter Registration Time! </h1>
<form action='$_SERVER["PHP_SELF"]' method='post'>
Enter the username to be added:
<input type="text" id="nt1" name="username"/>
Enter the corresponding email-id to be added:
<input type="text" id="nt1" name="email_id"/>
<input type="submit"/>
<?php
if(isset($_POST['submit']))
{
echo "<h1 style='color:red;'> Entering Data..... </h1>";
$database=mysqli_connect('localhost','root','','userdetails')
or die("didn't work");
mysqli_query($database,"INSERT INTO userdetails (username,password)VALUES ($_POST[username],$_POST[email_id])");
mysqli_close($database);
echo "<h3 style='color:red;'> Check the database..... </h3>";
}
?>
</body>
</html>
I am not able to access Database using $_SERVER['PHP_SELF'] but the code works if I use some other php script in action. Is this a problem with xampp?
Error:
You are missing php tag in your forms and type=submit in your submit button. Also, check the manual for inserting data in database using php
<html>
<body>
<h1>NewsLetter Registration Time! </h1>
<form action='<?php $_SERVER["PHP_SELF"];?>' method='post'>
Enter the username to be added:
<input type="text" id="nt1" name="username"/>
Enter the corresponding email-id to be added:
<input type="text" id="nt1" name="email_id"/>
<input type="submit" name="submit"/>
<?php
if(isset($_POST['submit']))
{
echo "<h1 style='color:red;'> Entering Data..... </h1>";
$database=mysqli_connect('localhost','root','','userdetails')
or die("didn't work");
mysqli_query($database,"INSERT INTO userdetails (username,password)VALUES ('$_POST[username]','$_POST[email_id]')");
mysqli_close($database);
echo "<h3 style='color:red;'> Check the database..... </h3>";
}
?>
</body>
</html>
I have 2 php scripts one that takes the values from a form on an html page and just return them to the user so he can check they are correct i then want to allow him to confirm then by pressing a submit button, the values will then be written on a txt file.
Here is my code that i have so far, the script return that the values are being written on the file but there is nothing in the file>
code:
fist php:
$surname = $_POST['surname'];
$lastname = $_POST['lastname'];
$mail = $_POST['mail'];
<div id="oneConatiner">
<h1>Please confirm the details of your order:</h1>
<div align="left" id='php'>
<form name="info" method="post" action="confirmation.php">
<p>Your surname is: <?php echo $surname . "<p>"; ?>
<p>Your lastname is: <?php echo $lastname . "</p></div>"; ?>
<p>Your email is: <?php echo $mail . "<p>"; ?>
<p><input type="submit" name="Submit" value="Confirm" /></p>
</form>
</div>
Second php:
<?php
$surname = $_POST['surname'];
$lastname = $_POST['lastname'];
$mail = $_POST['mail'];
$page = ceil($quantity / 10);
$data = "$surname,$lastname,$mail\r\n";
$fh = fopen("user.txt", "a");
fwrite($fh, $data);
fclose($fh);
if (fwrite == FALSE){
echo "fail";
} else {
echo "successful";
}
?>
The values you want to submit need to be form inputs of some kind. I'll use text fields for this example:
<form name="info" method="post" action="confirmation.php">
<p>Your surname is: <input name="surname" value="<?php echo htmlentities($surname); ?>"></p>
<p>Your lastname is: <input name="lastname" value="<?php echo htmlentities($lastname); ?>"></p>
<p>Your email is: <input name="mail" value="<?php echo htmlentities($mail); ?>"></p>
<p><input type="submit" name="Submit" value="Confirm" /></p>
</form>
If you don't want these fields to be editable, you can either set the fields to readonly or use hidden inputs instead.
The only error that I see and needs to be corrected is that the word fwrite in the if-conditional should be changed to $fh otherwise the code works. It worked for me.
Make sure the $surname, $lastname and $mail are not empty and you have read and write permissions for the folder where user.txt is supposed to be stored.
I am trying to pass 2 variables to another page and 1 var is required. I don't know why my code is not working properly. Take a look pls.
Prova1.php :
<html>
<body>
<?php
echo htmlspecialchars($_SERVER["PHP_SELF"]);
$nameErr="";
$name="";
$url="Prova1.php";
if ($_SERVER["REQUEST_METHOD"] == "POST"){
if (empty($_POST["name"])){
$nameErr = "Name is Required";
}else{
$url="active.php";
}
}
?>
<form method="post" action="<?php echo $url ?>" >
Name: <input type="text" name="name"/>* <?php echo $nameErr;?>
</br>
Email: <input type="text" name="email"/></br>
<input type="submit"/>
</form>
</body>
</html>
active.php :
<html>
<body>
Welcome <?php echo $_POST["name"]; ?> </br>
Your email is <?php echo $_POST["email"]; ?>
</body>
</html>
When the user fills in the required information, you should use a redirect, not another form, to go to active.php. You can pass variables using a session.
<?php
session_start();
if (isset($_POST['name']) && !empty($_POST['name']) {
$_SESSION['name'] = $_POST['name'];
$_SESSION['email'] = $_POST['email'];
header("Location: active.php");
} else {
$nameErr = '';
if (isset($_INPUT['submit'])) {
$nameErr = 'Name is required'];
}
?>
<html>
<body>
<form method="post" action="" >
Name: <input type="text" name="name" required/>* <?php echo $nameErr;?>
</br>
Email: <input type="text" name="email"/></br>
<input type="submit" name="submit"/>
</form>
</body>
</html>
<?php
}
Following your logic, I think your code should be something like this:
prova1.php
<html>
<style>
.error {color: #FF0000;}
</style>
<body>
<?php
echo htmlspecialchars($_SERVER["PHP_SELF"]);
$nameErr = "";
$name = "";
$url = "active.php";
if(isset($_GET['error'])){
if($_GET['error'] == 1){ //code for name error
$nameErr = "name is required";
}
}
?>
<form method="post" action="<?php echo $url ?>" >
Name: <input type="text" name="name"/>* <?php echo $nameErr; ?>
</br>
Email: <input type="text" name="email"/></br>
<input type="submit"/>
</form>
</body>
</html>
active.php
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
header("Location: prova1.php?error=1");
}
}else{
header("Location: prova1.php");
}
?>
<html>
<body>
Welcome <?php echo $_POST["name"]; ?> </br>
Your email is <?php echo $_POST["email"]; ?>
</body>
</html>
proval.php
if(!isset($_POST['name']) || empty($_POST['name'])){
include 'form.php';
}else{
include 'active.php';
}
form.php
<html>
<body><?php
echo htmlspecialchars($_SERVER["PHP_SELF"]);
$nameErr="";
$name="";
if (isset($_POST["name"])){
$nameErr = "Name is Required";
}
?>
<form method="post">
Name: <input type="text" name="name"/>* <?php echo $nameErr;?>
</br>
Email: <input type="text" name="email"/></br>
<input type="submit"/>
</form>
</body>
</html>
active.php
<html>
<body>
Welcome <?php echo $_POST["name"]; ?> </br>
Your email is <?php echo $_POST["email"]; ?>
</body>
</html>
I am having the hardest time of my life for not understanding the basics of the POST REDIRECT GET pattern in forms that submit to themselves.
The main problem is that when the user goes back or refreshes the page, I get duplicate entries in the database
So basically I have a page that contains two forms, each one submits to itself.
I have some code implemented regarding the PRG pattern but it doesn't seem to work.
I'll post a brief example where I'll try to explain what I am doing.
<?php
function saveUser1($UserName_1)
{
include 'db_conn.php';
//MySQL code etc...
if($result) return 1; //registro correcto
else return -2; //error
header('Location: samepage.php' , true, 303);
exit();
}
function saveUser2($UserName_2)
{
include 'db_conn.php';
//MySQL code etc...
if($result) return 1; //registro correcto
else return -2; //error
header('Location: samepage.php' , true, 303);
exit();
}
$error1 = 0;
$error2 = 0;
if(isset($_POST['userForm1']))
{
$error1 = saveUser1(clean_form($_POST['txtUserName_1']);
}
if(isset($_POST['userForm2']))
{
$error2 = saveUser2(clean_form($_POST['txtUserName_2']);
}
?>
Now the HTML
<form action="" name="userForm1" method="POST">
<label for="data">Some Data</label>
<input type="text" value="some test data to post" name="txtUserName_1" id="txtUserName_1" /><br />
<input type="submit" name="userForm1" id="userForm1"/>
</form>
<form action="" name="userForm2" method="POST">
<label for="data">Some Data</label>
<input type="text" value="some test data to post" name="txtUserName_2" id="txtUserName_2" /><br />
<input type="submit" name="userForm2" id="userForm2"/>
</form>
I just created this code in example of what I am trying to accomplish, but I haven't had any luck with the PGR pattern.
Could you guys tell me where the error is? Or redirect me (no kidding) to some good tutorial regarding this subject?
I have been looking to a lot of questions / answers, blogs but I can't find anything really solid (from my point of view).
Thanks in advance.
Below is sample code if you want try.
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Name: <input type="text" name="name">
Email: <input type="text" name="email">
Password: <input type="password" name="password">
<input type="submit" value="submit" name="send">
</form>
PHP Code and common.php is database connection file
<?php
require_once "common.php";
if(isset($_REQUEST['send']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];
$check = "SELECT * FROM user WHERE name = '".$name."' AND email = '".$email."' AND password = '".$password."'";
$check_result = mysql_query($check) or die(mysql_error());
if(mysql_num_rows($check_result) > 0)
{
header('Location : post.php');
}
else
{
$sql = "INSERT INTO user (name,email,password) VALUES ('$name','$email','$password')";
$result = mysql_query($sql) or die(mysql_error());
}
}
?>
Instead of checking for the form name itself check for a unique field within the form. E.g. If(isset($_POST[txtUserName_1'']))
The form name itself won't exist in the post.
To see what gets posted try:
print_r($_POST);
exit;
Maybe you have to set the post action to the same page.
And your form should not have the same name as your submit buttons(not sure about that).
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<label for="data">Some Data</label>
<input type="text" value="some test data to post" name="data" id="data" /><br />
<input type="submit" name="submit1" id="userForm1"/>
</form>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" name="form2" method="POST">
<label for="data">Some Data</label>
<input type="text" value="some test data to post" name="data" id="data" /><br />
<input type="submit" name="submit2" id="userForm2"/>
</form>
For the php:
if(isset($_POST['submit1']))
{
$error1 = saveUser1(clean_form($_POST['txtUserName_1']);
}
if(isset($_POST['submit2']))
{
$error1 = saveUser1(clean_form($_POST['txtUserName_2']);
}
you can add a hidden field for checking if its executed:
<input type="hidden" name="executed" value="0"/>
then you can set it to 0 when you have executed the mysql query
function saveUser1($UserName_1)
{
if($_POST['executed'] == 0)
{
include 'db_conn.php';
//MySQL code etc...
if($result) $_POST['executed'] = 1; //registro correcto
header('Location: samepage.php' , true, 303);
exit();
}
}