submit error PHP web form self submit?
07-08-2011, 10:22 AM
|
submit error PHP web form self submit?
|
Posts: 17
Name: Ali Shan
Location: Pakistan, Lahore
|
Hey i want to make a web form for my web i am trying this code but it do nothing. i cant get where is error in this script please help me.
PHP Code:
<html> <body bgcolor="silver" text="blue" link="black">
<form action="" method="post"> <table align="center"> <tbody align="left" valign="top"> <tr> <td height="100"></td> </tr> <tr> <td> Name:* </td> <td> <input type="text" name="Name" value="" maxlength="100" /> </td> </tr> <tr> <td> E-Mail:* </td> <td> <input type="text" name="E_Mail" value="" maxlength="100" /> </td> </tr> <tr> <td> Phone no:* </td> <td> <input type="text" name="Ph" value="" maxlength="100" /> </td> </tr> <tr> <td> Ga: </td> <td> <input type="text" name="Ga" value="" maxlength="100" /> </td> </tr> <tr> <td> Comments:* </td> <td> <textarea rows="3" cols="20" name="Comments"></textarea> </td> </tr> <tr> <td> * Required </td> </tr> </table> <table align="center"> <tr> <td> <input type="submit" value="Submit" alt="submit" name="submit" /> </td> <td> <input type="reset" /> </td> </tr> </table> </form>
</body> </html> <?php if (isset($POST["submit"])) { include("data.php"); $Name=$_POST['Name']; $E_Mail=$_POST['E_Mail']; $Ph=$_POST['Ph']; $Ga=$_POST['Ga']; $Comments=$_POST['Comments']; if ($Name == '' || $E_Mail == '' || $Ph == '' || $Comments == '') { $error = 'true'; echo "ERROR: Please fill required fields!"; } else { $error = 'false'; } if(preg_match("/^[+]{1}[0-9]{12}$/", $Ph)) { $error = 'true'; echo "The Phone number is not valid."; } else { $error = 'false'; } if(preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $E_Mail)) { $error = 'true'; echo "Your Email is not Valid."; } else { $error = 'false'; } if($error == "true") { echo "There is an error"; } else { mysql_query("INSERT INTO `gmifamil_form`.`gmi` (Name, EMail, Ph, Ga, Comments) VALUES ('$Name', '$E_Mail', '$Ph', '$Ga', '$Comments')") or die(mysql_error()); header("Location: th.htm"); } }
?>
Last edited by DezineGenerators; 07-08-2011 at 10:36 AM..
|
|
|
|
07-08-2011, 10:27 AM
|
Re: submit error PHP web form self submit?
|
Posts: 2,883
Name: Matt
Location: Irvine, CA
|
When posting code make sure to use the php tags. Otherwise all of the formatting is lost. See the 'how to post code' link in my sig.
At first glance there are two things I noticed that are wrong. First:
PHP Code:
if (isset($POST["submit"]))
should be
PHP Code:
if (isset($_POST["submit"]))
notice the underscore ( _ ).
Second, you're calling header after you've already sent output. You should do your form processing before you output your form. This will resolve the header issue and also allow you to display error messages in the form if needed.
Edit
Forgot to mention, you're also wide open for a SQL injection.
Other than that you'll have to clarify and post any error message you're getting.
Last edited by NullPointer; 07-08-2011 at 10:37 AM..
|
|
|
|
07-08-2011, 10:32 AM
|
Re: submit error PHP web form self submit?
|
Posts: 17
Name: Ali Shan
Location: Pakistan, Lahore
|
now when i click on submit with out any entry i redirect to th.htm without any error. i dnt want empty entries
|
|
|
|
07-08-2011, 10:41 AM
|
Re: submit error PHP web form self submit?
|
Posts: 17
Name: Ali Shan
Location: Pakistan, Lahore
|
As you can see that i have generated errors for empty variables but when i submit this form without any text i don't get any error.
|
|
|
|
07-08-2011, 10:42 AM
|
Re: submit error PHP web form self submit?
|
Posts: 2,883
Name: Matt
Location: Irvine, CA
|
Quote:
Originally Posted by DezineGenerators
now when i click on submit with out any entry i redirect to th.htm without any error. i dnt want empty entries
|
I'm guessing it's because you're setting $error to false in multiple cases. Set $error to false initially and then set it to true if there is a problem. By the way, you don't need to quote booleans.
PHP Code:
$error = "true"; //wrong
$error = true; //right
|
|
|
|
07-08-2011, 10:43 AM
|
Re: submit error PHP web form self submit?
|
Posts: 17
Name: Ali Shan
Location: Pakistan, Lahore
|
sorry i cant under stand you please can you give me any example of processin.
|
|
|
|
07-08-2011, 10:46 AM
|
Re: submit error PHP web form self submit?
|
Posts: 2,883
Name: Matt
Location: Irvine, CA
|
For example:
PHP Code:
if(isset($_POST['submit']))
{
$error = false;
if ($Name == '' || $E_Mail == '' || $Ph == '' || $Comments == '')
{
$error = true;
echo "ERROR: Please fill required fields!";
}
//leave off the else case
}
Once $error is set to false you shouldn't set it to false again.
|
|
|
|
07-08-2011, 10:54 AM
|
Re: submit error PHP web form self submit?
|
Posts: 17
Name: Ali Shan
Location: Pakistan, Lahore
|
I am getting same problem empty entries on submit 
|
|
|
|
07-08-2011, 10:57 AM
|
Re: submit error PHP web form self submit?
|
Posts: 2,883
Name: Matt
Location: Irvine, CA
|
Can you post the code you're using now?
|
|
|
|
07-08-2011, 11:17 AM
|
Re: submit error PHP web form self submit?
|
Posts: 17
Name: Ali Shan
Location: Pakistan, Lahore
|
Here it is
PHP Code:
<html> <body bgcolor="silver" text="blue" link="black">
<form action="" method="post"> <table align="center"> <tbody align="left" valign="top"> <tr> <td height="100"></td> </tr> <tr> <td> Name:* </td> <td> <input type="text" name="Name" value="" maxlength="100" /> </td> </tr> <tr> <td> E-Mail:* </td> <td> <input type="text" name="E_Mail" value="" maxlength="100" /> </td> </tr> <tr> <td> Phone no:* </td> <td> <input type="text" name="Ph" value="" maxlength="100" /> </td> </tr> <tr> <td> Ga: </td> <td> <input type="text" name="Ga" value="" maxlength="100" /> </td> </tr> <tr> <td> Comments:* </td> <td> <textarea rows="3" cols="20" name="Comments"></textarea> </td> </tr> <tr> <td> * Required </td> </tr> </table> <table align="center"> <tr> <td> <input type="submit" value="Submit" alt="submit" name="submit" /> </td> <td> <input type="reset" /> </td> </tr> </table> </form>
</body> </html> <?php if (isset($_POST["submit"])) { include("data.php"); $Name=$_POST['Name']; $E_Mail=$_POST['E_Mail']; $Ph=$_POST['Ph']; $Ga=$_POST['Ga']; $Comments=$_POST['Comments']; $error = flase; if ($Name == '' || $E_Mail == '' || $Ph == '' || $Comments == '') { $error = true; echo "ERROR: Please fill required fields!"; } else { $error = false; } if(preg_match("/^[+]{1}[0-9]{12}$/", $Ph)) { $error = true; echo "The Phone number is not valid."; } else { $error = false; } if(preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $E_Mail)) { $error = true; echo "Your Email is not Valid."; } else { $error = false; } if($error == true) { echo "There is an error"; } else { mysql_query("INSERT INTO `gmifamil_form`.`gmi` (Name, EMail, Ph, Ga, Comments) VALUES ('$Name', '$E_Mail', '$Ph', '$Ga', '$Comments')") or die(mysql_error()); header("Location: th.htm"); } }
?>
|
|
|
|
07-08-2011, 11:20 AM
|
Re: submit error PHP web form self submit?
|
Posts: 2,883
Name: Matt
Location: Irvine, CA
|
You only need to set $error to false once. Once it is true, you should never set it to false again.
Remove all of the else conditions:
PHP Code:
else
{
$error = false;
}
Also there is an error here:
PHP Code:
$error = flase;
"flase" should be false.
|
|
|
|
07-08-2011, 11:26 AM
|
Re: submit error PHP web form self submit?
|
Posts: 17
Name: Ali Shan
Location: Pakistan, Lahore
|
Ok thans for this now it shows empty error but mail validation and ph no validation is not working.
|
|
|
|
07-08-2011, 03:22 PM
|
Re: submit error PHP web form self submit?
|
Posts: 17
Name: Ali Shan
Location: Pakistan, Lahore
|
I am still having problem with this code.
Warning: Cannot modify header information - headers already sent by (output started at /home/contact.php) in /home/contact.php on line 43
PHP Code:
<?php if (isset($_POST["submit"])) { include("data.php"); $Name=$_POST['Name']; $E_Mail=$_POST['E_Mail']; $Ph=$_POST['Ph']; $Ga=$_POST['Ga']; $Comments=$_POST['Comments']; $head = false; $error = false; if ($Name == '' || $E_Mail == '' || $Ph == '' || $Comments == '') { echo '<div id="container"><div style="position:absolute;left:500px;top:237px;width:768px;height:23px;z-index:10" align="left">Write your name.</div><div style="position:absolute;left:540px;top:335px;width:768px;height:23px;z-index:10" align="left">Your comments Please!</div></div>'; } if(preg_match("/^[+]{1}[0-9]{12}$/", $Ph)) { $error = false; } else{ $error = true; echo '<div id="container"><div style="position:absolute;left:500px;top:287px;width:768px;height:23px;z-index:10" align="left">Please Write your no in this formet (+011231231234)</div></div>'; } if(preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $E_Mail)) { $error = false; } else{ $error = true; echo '<div id="container"><div style="position:absolute;left:500px;top:263px;width:768px;height:23px;z-index:10" align="left">Email adress is not Valid.</div></div>'; } if($error == false){ $head = true; mysql_query("INSERT INTO `gmifamil_form`.`gmi` (Name, EMail, Ph, Ga, Comments) VALUES ('$Name', '$E_Mail', '$Ph', '$Ga', '$Comments')") or die(mysql_error()); } } if($head == true){ header("Location: Thank_you.htm"); } ?>
|
|
|
|
07-09-2011, 04:05 AM
|
Re: submit error PHP web form self submit?
|
Posts: 44,055
Name: Chris Hirst
Location: Blackpool. UK
|
You cannot redirect once output has been sent to the user agent.
__________________
Chris. ->> Please login or register to view this content. Registration is FREE <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
|
|
|
|
|
« Reply to submit error PHP web form self submit?
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|