Page 1 of 1

why the form show twice?

Posted: Tue Nov 19, 2013 10:51 pm
by cheng412
as the title said, i don't know where i have made something wrong, if you feel free tell me please appreciate your help :D

Code: Select all

<html>
<body>
<form action = 'test2.php' method = 'post'>
<p>Name:<input type = "text" name = "name">
<?php if (isset($_POST["submit"])) {
	if (empty($_POST["name"]))
	{
		echo "Frist name is required";
	}     
	else
	{
		$_POST["name"] = $_POST["name"];

			if (!preg_match("/^[a-zA-Z ]*$/",$_POST["name"]))
  			{
  				echo "Only letters and white space allowed";
  			}
	}
}?></p>
<p>Email:<input type = "text" name = "email">
<?php if (isset($_POST["submit"])) {
	if (empty($_POST["email"]))
	{
		echo "Email is required";
	}
	else
	{
		$_POST["email"] = $_POST["email"];
			
			if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$_POST["email"]))
			{
				echo "Invalid email";
			}
	}
}
?></p>
<p><input type = "submit" name = "submit"></p>
</form>
</body>
</html>

Code: Select all

<html>
<body>
<?php
$name=$_POST["name"];
$email=$_POST["email"];
if (isset($_POST["submit"])) {
	if (empty($_POST["name"]))
	{
		include("test2.html");
	}     
	else
	{
		$name = $name;

			if (!preg_match("/^[a-zA-Z ]*$/",$name))
  			{
  				include("test2.html");
  			}
	}
	if (empty($_POST["email"]))
	{
		include("test2.html");
	}
	else
	{
		$email=$email;
			
			if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
			{
				include("test2.html");
			}
	}
}
?>
</body>
</html>

Re: why the form show twice?

Posted: Tue Nov 19, 2013 11:57 pm
by requinix
Because you show the form every time there's an error with one of the inputs. Two inputs, two forms. If you added a third field and then copy/pasted your code a third time, the form would show up a third time.

If one of the inputs is invalid then don't show the form immediately. Simply keep track that there was an error. At the very end, after you've processed the inputs and checked that everything is valid, then you can show the form again.

Re: why the form show twice?

Posted: Wed Nov 20, 2013 12:29 am
by cheng412
:?: :?: what should i do for that?? all the checking if statements should be deleted??

Re: why the form show twice?

Posted: Wed Nov 20, 2013 1:42 am
by requinix
requinix wrote:If one of the inputs is invalid then don't show the form immediately. Simply keep track that there was an error. At the very end, after you've processed the inputs and checked that everything is valid, then you can show the form again.

Re: why the form show twice?

Posted: Wed Nov 20, 2013 4:26 am
by tiger0
As requinix said,

so I think this code will do what you want in your thread here.

Code: Select all


<html>
<body>
<form action="" method="post">
    <p>Name:<input type="text" name="name">
       </p>
    <p>Email:<input type="text" name="email">
       </p>
    <p><input type="submit" name="submit"></p>
<?PHP 

if (isset($_POST["submit"])) {
$name = $_POST["name"];
$email = $_POST["email"];
if (empty($_POST["name"])) 
 {
echo " Please enter your name";
}
elseif (!preg_match("/^[a-zA-Z ]*$/",$name))
{
echo "please enter a correct name";
}
else {
$name = $_POST["name"];
}
if (empty($_POST["email"]))
{
echo " Please enter your email";
}
elseif (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
{
echo "Email is not valid. Email should be like (yourname@domain.com)" ;
}
else {
$email=$_POST["email"];
}
include("test2.html");
}
?>
</form>
</body>
</html>

Re: why the form show twice?

Posted: Wed Nov 20, 2013 6:54 am
by cheng412
So it means my html code has no problem? Only the php code I have to change to following code?

Re: why the form show twice?

Posted: Wed Nov 20, 2013 7:02 am
by tiger0
if you just copy and paste this code that should work fine. But do not use .html, use .php

for example, copy and paste the code into a file and name it "test.php"

it should work.

Re: why the form show twice?

Posted: Wed Nov 20, 2013 8:00 am
by cheng412
basically i want to show the error comments beside the input box there... if i rewrite the code like below

Code: Select all

<html>
<body>
<?php
$name=$_POST["name"];
if (isset($_POST["submit"]))
{
if (empty($name))
{
	echo "Name is required";
}
else
{
	$name=$name;
		if(!preg_match("/^[a-zA-Z ]*$/",$_POST["name"]))
		{
			echo "Only letters and spaces are allowed";
		}
}
include ("form.html");
}
?>
</body>
</html>
it will show me 2 error comments... so what should i do for that??

Re: why the form show twice?

Posted: Wed Nov 20, 2013 11:26 am
by tiger0
Well I can't tell about the errors because you did not post them here..

I guess the errors are coming from the include() function. It's not finding the file in the directory.