|
-
Mar 16th, 2008, 11:34 PM
#1
Thread Starter
Hyperactive Member
Form Submission with Validation
Hi,
I have a problem with form submission.
Situation
I have 3 .php files.
one.php -> Main file to run. If textbox filled with data then it is redirected to three.php.
two.php -> This file is to refer a friend. If email textbox is filled with data then it is redirected to three.php. It is included in the one.php.
three.php -> Result page. Either Submission from one.php or two.php redirect to this page on successful submission.
Problem
one.php -> On clicking the submit button without properly checking the php and javascript validation the page is redirected to three.php.
two.php -> On clicking the submit button without checking the php validation the page is redirected to three.php.
Code: one.php
PHP Code:
<?php
if(isset($_POST["t"]))
{
header("Location: three.php");
}
?>
<html>
<head>
<title>Test Script</title>
<script language="javascript">
function validate(thisform)
{
if(document.f.t.value=="")
{
alert("Please enter name");
document.f.t.focus();
return false;
}
}
</script>
</head>
<body>
<form name="f" method="POST" action="<?php echo $_SERVER["PHP_SELF"]; ?>" onsubmit="return validate(this)">
Name<input type="text" name="t" size="20"><br>
<input type="submit" name="b" value="Submit">
</form>
</body>
</html>
<?php
include("two.php");
?>
Code: two.php
PHP Code:
<?php
if(isset($_POST["email"]))
{
$email = $_POST["email"];
mail($email, "Test", "Test Mail", "From: sender <[email protected]>");
header("Location: three.php");
}
?>
<form name="f" method="POST" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
<table border="0">
<tr>
<td>
Write Email ID Here <input type="text" name="email" size="20">
</td>
<td>
<input type="submit" name="b" value="Submit">
</td>
</tr>
</table>
</form>
Code: three.php
PHP Code:
<?php
echo "Mail Sent Successfully";
?>
I need your assistance to understand the problem and help to solve the situation. Thank you so much.
-
Mar 22nd, 2008, 10:58 PM
#2
Fanatic Member
Re: Form Submission with Validation
You should take a look at this function to determine if they typed in something:
http://www.php.net/manual/en/function.empty.php
-
Mar 23rd, 2008, 03:45 AM
#3
Re: Form Submission with Validation
You are including two.php inside one.php. So you have two forms and the same page; both are which are going to be sent to one.php.
After inclusion your entire page will look like this:
PHP Code:
<?php
if(isset($_POST["t"])) { header("Location: three.php"); }
?>
<html> <head> <title>Test Script</title> <script language="javascript"> function validate(thisform) { if(document.f.t.value=="") { alert("Please enter name"); document.f.t.focus(); return false; } } </script> </head> <body> <form name="f" method="POST" action="<?php echo $_SERVER["PHP_SELF"]; ?>" onsubmit="return validate(this)"> Name<input type="text" name="t" size="20"><br> <input type="submit" name="b" value="Submit"> </form> </body> </html> <?php
if(isset($_POST["email"])) { $email = $_POST["email"]; mail($email, "Test", "Test Mail", "From: sender <[email protected]>"); header("Location: three.php"); }
?>
<form name="f" method="POST" action="<?php echo $_SERVER["PHP_SELF"]; ?>"> <table border="0"> <tr> <td> Write Email ID Here <input type="text" name="email" size="20"> </td> <td> <input type="submit" name="b" value="Submit"> </td> </tr> </table> </form>
So to start with your code is producing invalid HTML markup. Second, you are outputing two Location heades, the second of which is after you have output HTML; so form two will never actually redirect to three.php. Second; you cannot simply use isset() to check your variables and rely upon Javascript to validate them you need to check for the name and check that it has data in in the PHP code too. You also need to validate the email address.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|