PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
cheng412
Forum Newbie
Posts: 11 Joined: Sun Nov 17, 2013 11:18 pm
Post
by cheng412 » Tue Nov 19, 2013 10:51 pm
as the title said, i don't know where i have made something wrong, if you feel free tell me please appreciate your help
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>
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Tue Nov 19, 2013 11:57 pm
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.
cheng412
Forum Newbie
Posts: 11 Joined: Sun Nov 17, 2013 11:18 pm
Post
by cheng412 » Wed Nov 20, 2013 12:29 am
what should i do for that?? all the checking if statements should be deleted??
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Wed Nov 20, 2013 1:42 am
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.
tiger0
Forum Newbie
Posts: 16 Joined: Mon Oct 28, 2013 12:19 am
Post
by tiger0 » Wed Nov 20, 2013 4:26 am
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>
cheng412
Forum Newbie
Posts: 11 Joined: Sun Nov 17, 2013 11:18 pm
Post
by cheng412 » Wed Nov 20, 2013 6:54 am
So it means my html code has no problem? Only the php code I have to change to following code?
tiger0
Forum Newbie
Posts: 16 Joined: Mon Oct 28, 2013 12:19 am
Post
by tiger0 » Wed Nov 20, 2013 7:02 am
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.
cheng412
Forum Newbie
Posts: 11 Joined: Sun Nov 17, 2013 11:18 pm
Post
by cheng412 » Wed Nov 20, 2013 8:00 am
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??
tiger0
Forum Newbie
Posts: 16 Joined: Mon Oct 28, 2013 12:19 am
Post
by tiger0 » Wed Nov 20, 2013 11:26 am
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.