Forum Moderators: coopster
I am trying to have an account signup form on my site, and want to check if certain fields are filled in. If they are I want the data to be mailed to me, if not, then taken to an error page. I think I have made a stupid mistake somewhere, but can't figure it out - am I doing this the best way or should it be done a different way?
Heres the basic code:
<?
# prevent caching
header("Expires: Mon, 31 Mar 2003 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
if(!$submit)
{
?>
<!-- Display form page -->
... <input type="text" name="firstname" length="30" maxlength="60"> ... etc...
<!-- End Form page -->
<?
}
else
{
if(!$firstname ¦¦!$lastname ¦¦!$email)
{
?>
<!-- Display the error page -->
<html><head><title>error</title></head>
<!-- End error page -->
<?
}
else
{
$message = "First Name: $firstname\n Last Name: $lastname";
mail( "accounts@mydomain", "Account Interest", $message, "From: $email" );
header( "Location: [google.com"...] );
}
}
?>
All that happens is when I click submit without filling in any of the fields, I'm just shown the blank form page all over again. Thanks in advance for your help :-)
if(!isset($_POST['firstname'],$_POST['lastname'],$_POST['email'])){
Added: I like the method hpche mentioned, using extract() [php.net]
$err = "<!-- Display the error page -->
<html><head><title>error</title></head>
<!-- End error page -->";
if($firstname!= ""){
if($lastname!= ""){
if($email!= ""){
$message = "First Name: $firstname\n Last Name: $lastname";
mail( "accounts@mydomain", "Account Interest", $message, "From: $email" );
header( "Location: [google.com"...] );
} else {
echo $err;
} else {
echo $err;
} else {
echo $err;
}
<?
if(!$_POST['submit'])
{
?>
<form method="post">
<input type="text" name="firstname" length="30" maxlength="60">
<input type="text" name="lastname" length="30" maxlength="60">
<input type="text" name="email" length="30" maxlength="60">
<input type=submit name=submit>
</form>
<?
}
else
{
extract($_POST);
if(!$firstname OR !$lastname OR !$email)
echo "error";
else
header( "Location: http://www.google.com" );
}
?>
<?php
if ($_POST['submit'])
{
if ($firstname=="") { echo "error message"; exit; }
if ($lastname=="") { echo "error message"; exit; }
if ($email=="") { echo "error message"; exit; }
if (eregi("^([a-z]¦[0-9]¦\.¦-¦_)+@([a-z]¦[0-9]¦\.¦-¦_)+\.([a-z]¦[0-9]){2,3}$", $email, $arr_vars) &&
!eregi("(@.*@)¦(\.\.)¦(@\.)¦(\.@)¦(^\.)", $email, $arr_vars)) //e-mail address check
{
$message = "First Name: $firstname\n Last Name: $lastname";
mail( "accounts@mydomain", "Account Interest", $message, "From: $email" );
header( "Location: [google.com"...] );
}
else
{
echo "error message,invalid e-mail address";
exit;
}}
?>
<form method="POST" action="<? PHP_SELF;?>">
First Name:<input type="text" name="firstname"><br>
Last Name:<input type="text" name="lastname"><br>
E-Mail:<input type="text" name="email"><br>
<input type="submit" name="submit" value="Submit">
</form>>
Obviously set the form output to how you want it. This was for quickness.
:)