Forum Moderators: coopster

Message Too Old, No Replies

Simple PHP

         

macmuso

12:37 pm on Apr 20, 2003 (gmt 0)

10+ Year Member



Hello.

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 :-)

Birdman

12:51 pm on Apr 20, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You probably need to change to single pipes in this line:

if(!$firstname ¦!$lastname ¦!$email)

eaden

1:01 pm on Apr 20, 2003 (gmt 0)

10+ Year Member



You don't have your submit button in your code above, but check that the name is "submit" and not "Submit"

macmuso

1:08 pm on Apr 20, 2003 (gmt 0)

10+ Year Member



I changed them to single pipes - still nothing, and yes, the submit button is submit, not Submit.

hpche

1:12 pm on Apr 20, 2003 (gmt 0)

10+ Year Member



If you've got register_globals set to off, then you'd have to change

if(!$submit)

to

if(!$_POST['submit'])

for it to work. And also do the same with the other variables or do extract($_POST);

Birdman

1:12 pm on Apr 20, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Maybe this way:

if(!isset($firstname,$lastname,$email)){

}

macmuso

1:24 pm on Apr 20, 2003 (gmt 0)

10+ Year Member



If you've got register_globals set to off, then you'd have to change

if(!$submit)

to

if(!$_POST['submit'])

Do I have to do this for these variables as well?
if(!isset($firstname,$lastname,$email)){

What would be the code?
if(!isset($_POST['firstname,$lastname,$email'})){?

Birdman

1:30 pm on Apr 20, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, you will have to use $_POST for all the variables passed in the form if register_globals are off. So, for each variable, use $_POST['varname']

if(!isset($_POST['firstname'],$_POST['lastname'],$_POST['email'])){

Added: I like the method hpche mentioned, using extract() [php.net]

macmuso

2:28 pm on Apr 20, 2003 (gmt 0)

10+ Year Member



Nup, no luck :-(

Is there a different way to do what I want to do?

Birdman

2:54 pm on Apr 20, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually, after thinking, I realize that empty fields still set the variable with even if it is empty. What I do is test to see if does not equal "".

$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;
}

hpche

5:19 pm on Apr 20, 2003 (gmt 0)

10+ Year Member



Try this which is a simplified version of your form, I just tested it and worked ok for me.


<?
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" );
}
?>

dreamcatcher

5:30 pm on Apr 20, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



heres another simple mail form which checks for a valid e-mail addy:

<?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.

:)

macmuso

5:43 am on Apr 21, 2003 (gmt 0)

10+ Year Member



Thanks heaps