I am having troubles on PHP form
Moderator: General Moderators
I am having troubles on PHP form
I need a PHP expert to look on my codes. It's not working. I don't know if I have writtent the code correctly. Any help is greatly appreicated. Thank you in advance.
send.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>mail Sent</title>
<style>
<!--
p{
font-family:verdana;arial;
font-size:12px;
}
-->
</style>
<?php
$firstName = $_POST['first name'];
$lastName = $_POST['last name'];
$Organization = $_POST['organization'];
$title = $_POST['title'];
$message = stripslashes($_POST['message']);
$stateProvince = $_POST['stateProvince'];
$country = $_POST['country'];
$postal= $_POST['postal code'];
$phone = $_POST['phone'];
$email = $_POST['email'];
if($Name && $Email && $Topic && $Message){
$Message .= "\r\n\r\n" . 'Message sent from ' . $Name . ' (' . $Email . ')';
$send = mail('sam_mah29@hotmail.com',$Topic,$Message);
echo '<br><br><br><center><p><strong>';
if($send)
echo 'Ur message has been sent successfully delivered';
echo "<br> Click <a href=../contact.html>here</a> to go back to the HOME page ";
}
else{
echo 'We enquired a problem delivering u message! Please try again';
echo '</strong></p></center>';
}
if (strlen($Name) == 0){
$error_msg="Please Enter ur name";
}
if(strlen($Email) == 0) {
$error_msg="Please Enter a valid email address";
}
if(strlen($Topic) == 0){
$error_msg="Please Enter the Topic";
}
if(strlen($Message) == 0){
$error_msg="Please Type in the message";
}
if(isset($error_msg)){
echo "The form cannot be sent because its not properly filled:<br><br>";
echo $error_msg;
echo "<br> Click <a href=javascript:history.back(1)>here</a> to go back and fill the form once again ";
}
?>
contact.html
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<center><form method="post" action="send.php">
<table>
<tr>
<td><b>First Name</b></td>
<td> <input name="firstName" type="text" id="firstName" size="35" maxlength="35">
*</td>
</tr>
<tr>
<td><b>Last Name</b></td>
<td><input name="lastName" type="text" id="lastName" size="35" maxlength="35">
*</td>
</tr>
<tr>
<td><b>Title</b></td>
<td><input name="title" type="text" id="title" size="35" maxlength="35"></td>
</tr>
<tr>
<td>Organization</td>
<td><input name="organization" type="text" id="organization" size="35" maxlength="35"></td>
</tr>
<tr>
<td><b>Message</b></td>
<td><textarea name ="message" cols="35" rows="5"></textarea>
</tr>
<tr>
<td>City</td>
<td><input name="city" type="text" id="city"></td>
</tr>
<tr>
<td>State or<br />
Province</td>
<td><SELECT name=stateProvince size=1 id="stateProvince"> <OPTION value=""
selected>Alabama<OPTION value="">Alaska<OPTION
value="">Arizona<OPTION value="">Arkansas<OPTION
value="">California<OPTION value="">Colorado<OPTION
value="">Connecticut<OPTION value="">Delaware<OPTION
value="">District of Columbia<OPTION value="">Florida<OPTION
value="">Georgia<OPTION value="">Hawaii<OPTION
value="">Idaho<OPTION value="">Illinois<OPTION
value="">Indiana<OPTION value="">Iowa<OPTION
value="">Kansas<OPTION value="">Kentucky<OPTION
value="">Louisiana<OPTION value="">Maine<OPTION
value="">Maryland<OPTION value="">Massachusetts<OPTION
value="">Michigan<OPTION value="">Minnesota<OPTION
value="">Mississippi<OPTION value="">Missouri<OPTION
value="">Montana<OPTION value="">Nebraska<OPTION
value="">Nevada<OPTION value="">New Hampshire<OPTION value="">New
Jersey<OPTION value="">New Mexico<OPTION value="">New York<OPTION
value="">North Carolina<OPTION value="">North Dakota<OPTION
value="">Ohio<OPTION value="">Oklahoma<OPTION
value="">Oregon<OPTION value="">Pennsylvania<OPTION value="">Rhode
Island<OPTION value="">South Carolina<OPTION value="">South
Dakota<OPTION value="">Tennessee<OPTION value="">Texas<OPTION
value="">Utah<OPTION value="">Vermont<OPTION
value="">Virginia<OPTION value="">Washington<OPTION value="">West
Virginia<OPTION value="">Wisconsin<OPTION value="">Wyoming<OPTION
value="">--------------------<OPTION value="">Alberta<OPTION
value="">British Columbia<OPTION value="">Labrador<OPTION
value="">Manitoba<OPTION value="">New Brunswick<OPTION
value="">Newfoundland<OPTION value="">Nova Scotia<OPTION
value="">Northwest Territories<OPTION value="">Nunavut
Territory<OPTION value="">Ontario<OPTION value="">Prince Edward
Island<OPTION value="">Quebec<OPTION value="">Saskatchewan<OPTION
value="">Yukon</OPTION></SELECT></TD>
</tr>
<tr>
<td>Country</td>
<td><input name="country" type="text" id="country"></td>
</tr>
<tr>
<td>Postal Code</td>
<td><input name="postal" type="text" id="postal"></td>
</tr>
<tr>
<td>Phone</td>
<td><input name="phone" type="text" id="phone" maxlength="20">
*</td>
</tr>
<tr>
<td>Email address</td>
<td><input name="email" type="text" id="email" size="35" maxlength="35">
*</td>
</tr>
<tr>
<td colspan="2">
<tr>
<td colspan="2">
<input type="submit" value="send" name="send">
<input type="reset" value="reset"></td>
</tr>
</table>
</form>
</center>
</body>
</html>
send.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>mail Sent</title>
<style>
<!--
p{
font-family:verdana;arial;
font-size:12px;
}
-->
</style>
<?php
$firstName = $_POST['first name'];
$lastName = $_POST['last name'];
$Organization = $_POST['organization'];
$title = $_POST['title'];
$message = stripslashes($_POST['message']);
$stateProvince = $_POST['stateProvince'];
$country = $_POST['country'];
$postal= $_POST['postal code'];
$phone = $_POST['phone'];
$email = $_POST['email'];
if($Name && $Email && $Topic && $Message){
$Message .= "\r\n\r\n" . 'Message sent from ' . $Name . ' (' . $Email . ')';
$send = mail('sam_mah29@hotmail.com',$Topic,$Message);
echo '<br><br><br><center><p><strong>';
if($send)
echo 'Ur message has been sent successfully delivered';
echo "<br> Click <a href=../contact.html>here</a> to go back to the HOME page ";
}
else{
echo 'We enquired a problem delivering u message! Please try again';
echo '</strong></p></center>';
}
if (strlen($Name) == 0){
$error_msg="Please Enter ur name";
}
if(strlen($Email) == 0) {
$error_msg="Please Enter a valid email address";
}
if(strlen($Topic) == 0){
$error_msg="Please Enter the Topic";
}
if(strlen($Message) == 0){
$error_msg="Please Type in the message";
}
if(isset($error_msg)){
echo "The form cannot be sent because its not properly filled:<br><br>";
echo $error_msg;
echo "<br> Click <a href=javascript:history.back(1)>here</a> to go back and fill the form once again ";
}
?>
contact.html
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<center><form method="post" action="send.php">
<table>
<tr>
<td><b>First Name</b></td>
<td> <input name="firstName" type="text" id="firstName" size="35" maxlength="35">
*</td>
</tr>
<tr>
<td><b>Last Name</b></td>
<td><input name="lastName" type="text" id="lastName" size="35" maxlength="35">
*</td>
</tr>
<tr>
<td><b>Title</b></td>
<td><input name="title" type="text" id="title" size="35" maxlength="35"></td>
</tr>
<tr>
<td>Organization</td>
<td><input name="organization" type="text" id="organization" size="35" maxlength="35"></td>
</tr>
<tr>
<td><b>Message</b></td>
<td><textarea name ="message" cols="35" rows="5"></textarea>
</tr>
<tr>
<td>City</td>
<td><input name="city" type="text" id="city"></td>
</tr>
<tr>
<td>State or<br />
Province</td>
<td><SELECT name=stateProvince size=1 id="stateProvince"> <OPTION value=""
selected>Alabama<OPTION value="">Alaska<OPTION
value="">Arizona<OPTION value="">Arkansas<OPTION
value="">California<OPTION value="">Colorado<OPTION
value="">Connecticut<OPTION value="">Delaware<OPTION
value="">District of Columbia<OPTION value="">Florida<OPTION
value="">Georgia<OPTION value="">Hawaii<OPTION
value="">Idaho<OPTION value="">Illinois<OPTION
value="">Indiana<OPTION value="">Iowa<OPTION
value="">Kansas<OPTION value="">Kentucky<OPTION
value="">Louisiana<OPTION value="">Maine<OPTION
value="">Maryland<OPTION value="">Massachusetts<OPTION
value="">Michigan<OPTION value="">Minnesota<OPTION
value="">Mississippi<OPTION value="">Missouri<OPTION
value="">Montana<OPTION value="">Nebraska<OPTION
value="">Nevada<OPTION value="">New Hampshire<OPTION value="">New
Jersey<OPTION value="">New Mexico<OPTION value="">New York<OPTION
value="">North Carolina<OPTION value="">North Dakota<OPTION
value="">Ohio<OPTION value="">Oklahoma<OPTION
value="">Oregon<OPTION value="">Pennsylvania<OPTION value="">Rhode
Island<OPTION value="">South Carolina<OPTION value="">South
Dakota<OPTION value="">Tennessee<OPTION value="">Texas<OPTION
value="">Utah<OPTION value="">Vermont<OPTION
value="">Virginia<OPTION value="">Washington<OPTION value="">West
Virginia<OPTION value="">Wisconsin<OPTION value="">Wyoming<OPTION
value="">--------------------<OPTION value="">Alberta<OPTION
value="">British Columbia<OPTION value="">Labrador<OPTION
value="">Manitoba<OPTION value="">New Brunswick<OPTION
value="">Newfoundland<OPTION value="">Nova Scotia<OPTION
value="">Northwest Territories<OPTION value="">Nunavut
Territory<OPTION value="">Ontario<OPTION value="">Prince Edward
Island<OPTION value="">Quebec<OPTION value="">Saskatchewan<OPTION
value="">Yukon</OPTION></SELECT></TD>
</tr>
<tr>
<td>Country</td>
<td><input name="country" type="text" id="country"></td>
</tr>
<tr>
<td>Postal Code</td>
<td><input name="postal" type="text" id="postal"></td>
</tr>
<tr>
<td>Phone</td>
<td><input name="phone" type="text" id="phone" maxlength="20">
*</td>
</tr>
<tr>
<td>Email address</td>
<td><input name="email" type="text" id="email" size="35" maxlength="35">
*</td>
</tr>
<tr>
<td colspan="2">
<tr>
<td colspan="2">
<input type="submit" value="send" name="send">
<input type="reset" value="reset"></td>
</tr>
</table>
</form>
</center>
</body>
</html>
-
AbelaJohnB
- Forum Newbie
- Posts: 9
- Joined: Sat Feb 28, 2004 12:14 am
Sam,
First, None of your states will show because your <options> do not have values set.
not
Is probably what you'll want to do 
Secondly, within your form page:
That's just not proper HTML
Missing some </TD>'s in there... or something.
Now, the rest of it is "ok". Not "great" or "good"... but "Ok". Add some JavaScript code to detect null values on submit and a bit of cleaing up and it would be close.
Now onto the messy file....
May be untrue
What if it fails

Should be:
and
should be:
You should always seriously consider using [php_man]stripslashes()[/php_man] on all of your input fields.
Next is your method for [php_man]mail()[/php_man]
All I'm going to say is... this method is not good.
I'm not going to rewrite your entire script... but I will suggest you try "Example 4." at: this page as it is a solid and easy to understand method/example.
Lastly, I do not personally agree with throwing error messages on the 'send.php' page. Rather, if there is an error, redirect them back to the form (use [php_man]header()[/php_man]) and throw your message therein. It's much more user-friendly.
Anyway, hope this helps get you started in your quest for one of the easiest language on the internet!
First, None of your states will show because your <options> do not have values set.
Code: Select all
<OPTION VALUE="California">California</OPTION>Code: Select all
<OPTION VALUE="">California</OPTION>Secondly, within your form page:
Code: Select all
<tr>
<td colspan="2">
<tr>
<td colspan="2">
<input type="submit" value="send" name="send">
<input type="reset" value="reset"></td>
</tr>That's just not proper HTML
Now, the rest of it is "ok". Not "great" or "good"... but "Ok". Add some JavaScript code to detect null values on submit and a bit of cleaing up and it would be close.
Now onto the messy file....
Code: Select all
<title>mail Sent</title>Code: Select all
$firstName = $_POSTї'first name'];Code: Select all
$firstName = $_POSTї'firstName'];Code: Select all
$lastName = $_POSTї'last name'];Code: Select all
$lastName = $_POSTї'lastName];Next is your method for [php_man]mail()[/php_man]
All I'm going to say is... this method is not good.
Lastly, I do not personally agree with throwing error messages on the 'send.php' page. Rather, if there is an error, redirect them back to the form (use [php_man]header()[/php_man]) and throw your message therein. It's much more user-friendly.
Anyway, hope this helps get you started in your quest for one of the easiest language on the internet!
Hi Anson,AnsonM wrote:Can you explain what error is showing? thanx
The problem lies on this line.
echo "<br> Click <a href=../contact.html>here</a> to go back to the HOME page ";
Hi AbelaJohnB,
I really want to make this more userfriedly but I don't know how to write PHP codes for mail() on send.php. I have fixed all the corrections you suggested.
send.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<style>
<!--
p{
font-family:verdana;arial;
font-size:12px;
}
-->
</style>
<?php
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$Organization = $_POST['organization'];
$title = $_POST['title'];
$message = stripslashes($_POST['message']);
$stateProvince = $_POST['stateProvince'];
$country = $_POST['country'];
$postalCode= $_POST['postalCode'];
$phone = $_POST['phone'];
$email = $_POST['email'];
if($Name && $Email && $Topic && $Message){
$Message .= "\r\n\r\n" . 'Message sent from ' . $Name . ' (' . $Email . ')';
$send = mail('sammah@telus.net',$Title,$Message);
echo '<br><br><br><center><p><strong>';
if($send)
echo 'Your message has been sent successfully delivered';
echo "<br> Click <a href=../contact.html>here</a> to go back to the HOME page ";
}
else{
echo 'We enquired a problem delivering your message! Please try again';
echo '</strong></p></center>';
}
if (strlen($firstName) == 0){
$error_msg="Please Enter Your First Name";
}
if (strlen($lastName) == 0){
$error_msg="Please Enter Your Last Name";
}
if(strlen($Email) == 0) {
$error_msg="Please Enter a Valid Email Address";
}
if(strlen($Title) == 0){
$error_msg="Please Enter the Title";
}
if(strlen($phone) == 0){
$error_msg="Please Enter Your phone number";
}
if(isset($error_msg)){
echo "The form cannot be sent because its not properly filled:<br><br>";
echo $error_msg;
echo "<br> Click <a href=javascript:history.back(1)>here</a> to go back and fill the form once again ";
}
?>
contact.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<center><form method="post" action="send.php">
<table>
<tr>
<td><b>First Name</b></td>
<td> <input name="firstName" type="text" id="firstName" size="35" maxlength="35">
*</td>
</tr>
<tr>
<td><b>Last Name</b></td>
<td><input name="lastName" type="text" id="lastName" size="35" maxlength="35">
*</td>
</tr>
<tr>
<td><b>Title</b></td>
<td><input name="title" type="text" id="title" size="35" maxlength="35"></td>
</tr>
<tr>
<td>Organization</td>
<td><input name="organization" type="text" id="organization" size="35" maxlength="35"></td>
</tr>
<tr>
<td><b>Message</b></td>
<td><textarea name ="message" cols="35" rows="5"></textarea>
</tr>
<tr>
<td>City</td>
<td><input name="city" type="text" id="city"></td>
</tr>
<tr>
<td>State or<br />
Province</td>
<td><SELECT name=stateProvince size=1 id="stateProvince">
<OPTION value="Alabama" selected>Alabama<OPTION value="Alaska">Alaska<OPTION
value="Arizona">Arizona<OPTION value="Arkansas">Arkansas<OPTION
value="California">California<OPTION value="Colorado">Colorado<OPTION
value="Connecticut">Connecticut<OPTION value="Delaware">Delaware<OPTION
value="District of Columbia">District of Columbia<OPTION value="Florida">Florida<OPTION
value="Georgia">Georgia<OPTION value="Hawaii">Hawaii<OPTION
value="Idaho">Idaho<OPTION value="Illinois">Illinois<OPTION
value="Indiana">Indiana<OPTION value="Iowa">Iowa<OPTION
value="Kansas">Kansas<OPTION value="Kentucky">Kentucky<OPTION
value="Louisiana">Louisiana<OPTION value="Maine">Maine<OPTION
value="Maryland">Maryland<OPTION value="Massachusetts">Massachusetts<OPTION
value="Michigan">Michigan<OPTION value="Minnesota">Minnesota<OPTION
value="Mississippi">Mississippi<OPTION value="Missouri">Missouri<OPTION
value="Montana">Montana<OPTION value="Nebraska">Nebraska<OPTION
value="Nevada">Nevada<OPTION value="New Hampshire">New Hampshire<OPTION value="New
Jersey">New Jersey<OPTION value="New Mexico">New Mexico<OPTION value="New York">New York<OPTION
value="North Carolina">North Carolina<OPTION value="North Dakota">North Dakota<OPTION
value="Ohio">Ohio<OPTION value="Oklahoma">Oklahoma<OPTION
value="Oregon">Oregon<OPTION value="Pennsylvania">Pennsylvania<OPTION value="Rhode
Island">Rhode Island<OPTION value="South Carolina">South Carolina<OPTION value="South
Dakota">South Dakota<OPTION value="Tennessee">Tennessee<OPTION value="Texas">Texas<OPTION
value="Utah">Utah<OPTION value="Vermont">Vermont<OPTION
value="Virginia">Virginia<OPTION value="Washington">Washington<OPTION value="West
Virginia">West Virginia<OPTION value="Wisconsin">Wisconsin<OPTION value="Wyoming">Wyoming<OPTION
value="">--------------------<OPTION value="Alberta">Alberta<OPTION
value="British Columbia">British Columbia<OPTION value="Labrador">Labrador<OPTION
value="Manitoba">Manitoba<OPTION value="New Brunswick">New Brunswick<OPTION
value="Newfoundland">Newfoundland<OPTION value="Nova Scotia">Nova Scotia<OPTION
value="Northwest Territories">Northwest Territories<OPTION value="Nunavut
Territory">Nunavut Territory<OPTION value="Ontario">Ontario<OPTION value="Prince Edward
Island">Prince Edward Island<OPTION value="Quebec">Quebec<OPTION value="Saskatchewan">Saskatchewan<OPTION
value="Yukon">Yukon</OPTION></SELECT></td>
</tr>
<tr>
<td>Country</td>
<td><input name="country" type="text" id="country"></td>
</tr>
<tr>
<td>Postal Code</td>
<td><input name="postal" type="text" id="postal"></td>
</tr>
<tr>
<td>Phone</td>
<td><input name="phone" type="text" id="phone" maxlength="20">
*</td>
</tr>
<tr>
<td>Email address</td>
<td><input name="email" type="text" id="email" size="35" maxlength="35">
*</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="send" name="send">
<input type="reset" value="reset"></td>
</tr>
</table>
</form>
</center>
</body>
</html>
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Now now on used the
Code: Select all
and/orCode: Select all
functions when posting code.Phenom wrote:Now now on used theCode: Select all
and/orCode: Select all
functions when posting code.[/quote] Sorry
Code: Select all
if($Name && $Email && $Topic && $Message){in your code you have :
Code: Select all
$firstName = $_POSTї'firstName'];
$lastName = $_POSTї'lastName'];
$Organization = $_POSTї'organization'];
$title = $_POSTї'title'];Apparently I still make some more errors. It suppose to be if ($firstName&&lastName&&email&&title&&message&&phone)Dr Evil wrote:you never set $Name and $TopicCode: Select all
if($Name && $Email && $Topic && $Message){
in your code you have :Is this intentional ?Code: Select all
$firstName = $_POSTї'firstName']; $lastName = $_POSTї'lastName']; $Organization = $_POSTї'organization']; $title = $_POSTї'title'];