Page 1 of 1
newbie question regarding simple form submission
Posted: Sun Jun 22, 2003 12:35 pm
by fransgaard
I recently took up learning php and I have started with a simple form.
I have made a form that sends an email, and a form that checks whether required fields have been filled out. Both works but when combinded it doesn't. I was hoping somebody here could tell me what I am doing wrong here.
this one checks for required fields
http://www.fransgaard.net/php/form01.php
this one sends email
http://www.fransgaard.net/php/form02.php
This one is both combined, but doesn't work
http://www.fransgaard.net/php/form03.php
This is the code for the last form:
Code: Select all
<?php
if (! isset ($your_message))
$my_message = "";
elseif ($your_message <> "")
if ($tlf <> "" || $email <> "")
$my_message = "Thanks for your email";
// bit that is sent off to my mail
$to = "robert@fransgaard.com";
$msg .= "$your_message\n\n";
mail($to, "To me", $msg, "Fra $name\n Reply-To: $email \n");
//
else
$my_message = "I need your phone or email";
elseif ($your_message == "")
$my_message ="Don't you want to say something ?";
?>
<html>
<head>
...
Posted: Sun Jun 22, 2003 12:42 pm
by Zeceer
I don't see why you want to combine them, but try instead making a page with the form, and send the form to another page that is the script. On top of the mail script you can use this:
Code: Select all
if( $_POSTї'name']=="" or $_POSTї'phone']=="" or $_POSTї'email']=="" or $_POSTї'message']=="")
{
echo "Fill in the required fields";
exit;
}
Or you can put a header in there instead.
javascript
Posted: Sun Jun 22, 2003 12:58 pm
by phpScott
try adding javascript yo your exitment of learning php.
You could javascript to check to see if the required fields have been filled out and would save a trip to the server.
phpScott
Posted: Sun Jun 22, 2003 1:02 pm
by twigletmac
Big bonus of server-side validation over client-side validation is it doesn't rely on JS being available. Always good to add some server-side checking in case your client-side stuff is circumvented (never trust users).
Mac
Posted: Sun Jun 22, 2003 1:12 pm
by fransgaard
wow, thanks for all the quick answers
Zeceer, I want to keep the same file after form has been submitted, but your code looks very much like flash actionscript code so it was suddenly a whole lot easier to figure out what to do, so I rewrote the codes to this
http://www.fransgaard.net/php/form05.php which works:
Code: Select all
<?php
if ( isset ($your_message))
{
if( $_POSTї'name']=="" or $_POSTї'email']=="" or $_POSTї'your_message']=="")
{
$my_message = "OY! fill in the fields";
} else {
$to = "robert@fransgaard.com";
$msg .= "$your_message\n\n";
mail($to, "To me", $msg, "Fra $name\n Reply-To: $email \n");
}
}
?>
phpScott, I'll take one programming language at the time since I am rather new at this

Posted: Sun Jun 22, 2003 2:52 pm
by m3rajk
IMHO: php and javascript are crosses between programming languages and something else. javascript is much much less of a programming language, php is much closer to a programming language.
a better way to check variables being set is to use the php function
isset()
since you're using a form, i suggest passing a hidden variable. use that to determine what to do. the following i didn't test, but since i've already gone through this stuff with what i'm doing and i'm pretty confident that it'll work. don't just take it, read the link i gave you and then go through this line by line, if you don't understand everything, then post questions here, and then read the answers, otherwise you won't learn it and you will make the mistakes again. i just can't think of any other way to set you on a good track to complete it aside from giving you most of what you need layed out for you
Code: Select all
<?php
/*get variables*/
if(isset($stage)){
if(isset($name)&&isset($email)&&isset($tlf)&&isset($your_message)){
$date-time();
$message="on $date, $name contactable via $phone and $email sent message...
$your_message
cc: $name"
$other="cc: $email"
mail('your_e-mail_address', 'user comments', $message, $other);
}else{
/* spit out your error messages */
}
}else{
?><html>
<head>
<title>PHP form</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<strong></strong>
<form action="<?php echo $_SERVER[PHP_SELF]; ?>">
<input type="hidden" name="stage" value="process">
Your name<br>
<input name="name" type="text" size="30" maxlength="50" value=""><br><br>
Your email<br>
<input name="email" type="text" size="30" maxlength="50" value=""><br><br>
Your phone<br>
<input name="tlf" type="text" size="30" maxlength="20" value=""><br><br>
Message<br>
<textarea name="your_message" rows="8" cols="23"></textarea><br><br>
<input type="submit" value="Send">
</form>
</body>
</html>
<?php
}?>
javascript
Posted: Sun Jun 22, 2003 3:38 pm
by phpScott
I never just rely on javascript and revalidate the info server side but it does mean less trips to the server if js is used to do some basic form validation before the request is sent.
php's isset() and isempty functions are really handy server side tools.
phpScott
Posted: Mon Jun 23, 2003 3:54 am
by fransgaard
m3rajk, thanks I'll have a look through it, but as I said I am completely new to PHP and what I have done now I understand, I prefer to only use things I know how works.
ALso as I said it has simularities to action script which I know so it is a good place to start.
What I don't understand about yours is this bit:
<?php echo $_SERVER[PHP_SELF]; ?>
Also I don't understand why:
!isset($name)
is better than
$_POST['name']==""
They seem to do the same to me, but yes I'll have a read through your link, thanks

Posted: Mon Jun 23, 2003 4:13 am
by twigletmac
fransgaard wrote:What I don't understand about yours is this bit:
That's to set the action attribute of the form to the current page (i.e. make it a self submitting form).
fransgaard wrote:Also I don't understand why:
is better than
They seem to do the same to me, but yes I'll have a read through your link, thanks

If the $_POST['name'] variable is not set then the second method will cause an undefined index notice to appear (if you have error reporting at it's highest level) because you are trying to compare an empty string against a variable which does not exist. However, using isset() allows you to check that the variable exists before attempting to do anything with it. Using
empty() instead of !isset() would also allow you to check that the variable was not null, equal to an empty string or 0 (zero).
Mac
Posted: Mon Jun 23, 2003 6:30 am
by fransgaard
So isset() does not check whether there is a value to the variable but whether there is a variable at all, is that correctly understood?
Posted: Mon Jun 23, 2003 7:08 am
by twigletmac
fransgaard wrote:So isset() does not check whether there is a value to the variable but whether there is a variable at all, is that correctly understood?
Yes.
Mac
Posted: Mon Jun 23, 2003 9:02 am
by fransgaard
ok thanks. in that case I don't think it matters much since I know the variables are there since I built the form, but that was nice to know for future reference