The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
Contact Form
Discuss Contact Form in the PHP Development forum on Dev Shed. Contact Form PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

July 17th, 2012, 02:09 AM
|
|
Registered User
|
|
Join Date: Jul 2012
Posts: 2
Time spent in forums: 37 m 5 sec
Reputation Power: 0
|
|
|
Contact Form
Hi there,
I am building a website at the moment using Dreamweaver and need to do a form on the contact page. Can anyone help me produce a working form with Php code and a thankyou for contacting us page ?? The form fields are Name:, Email:, Tel:, Message: and the Submit button.
Any help would be greatly appreciated, as I am pulling out the hair I have left grrrrrrrrr!!
This post has been edited by Rayb: Today, 01:08 AM
|

July 17th, 2012, 01:30 PM
|
|
Registered User
|
|
Join Date: Jul 2012
Posts: 7
Time spent in forums: 1 h 24 m 33 sec
Reputation Power: 0
|
|
With out knowing exactly what you want the PHP to do here is a basic example:
Code:
<form action="url to the php script .php" method = "POST" >
<label for="name">Name:</label><input type="text" name="name" />
<label for="email">Email:</label><input type="email" name="email" />
<label for="tel">Telephone:</label><input type="text" name="tel" />
<label for="message">Message:</label><textarea name="message"></textarea>
</form>
And here is the php:
PHP Code:
<?php
$name = mysql_real_escape_string($_POST['name']);
$email= mysql_real_escape_string($_POST['email']);
$tel= mysql_real_escape_string($_POST['tel']);
$message= mysql_real_escape_string($_POST['message']);
// do what you wish with the values.
?>
This is a very basic example but hopefully this gives you an idea of what to do.
|

July 17th, 2012, 02:42 PM
|
 |
CSS & JS/DOM Adept
|
|
Join Date: Jul 2004
Location: USA
|
|
Welcome to DevShed Forums, raybrown.
"A little knowledge is a dangerous thing." You will probably want to do some error checking in the PHP code as well.
@tomVance The "for" attribute of <label> elements should reference the ID of a form field. Your form fields lack IDs.
|

July 17th, 2012, 02:56 PM
|
|
Registered User
|
|
Join Date: Jul 2012
Posts: 7
Time spent in forums: 1 h 24 m 33 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by Kravvitz Welcome to DevShed Forums, raybrown.
"A little knowledge is a dangerous thing." You will probably want to do some error checking in the PHP code as well.
@tomVance The "for" attribute of <label> elements should reference the ID of a form field. Your form fields lack IDs. |
Kravviz is right the labels need to relates to the ID attribute for the input elements i missed them out, sorry.
|

July 18th, 2012, 08:38 AM
|
|
Registered User
|
|
Join Date: Jul 2012
Posts: 2
Time spent in forums: 37 m 5 sec
Reputation Power: 0
|
|
|
Contact form
Thanks TomVance
I want the PHP to send the contents from the input fields in the form and display a thankyou message to say we will be in contact soon, see my coding below, hope you can help or make it simple for me :-)
Code:
<form name="contactform" method="post" action="send_form_email.php">
<table width="450px" align="center">
<tr>
<td valign="top">
<label for="first_name">First Name *</label>
</td>
<td valign="top">
<input type="text" name="first_name" maxlength="50" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="last_name">Last Name *</label>
</td>
<td valign="top">
<input type="text" name="last_name" maxlength="50" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="email">Email Address *</label>
</td>
<td valign="top">
<input type="text" name="email" maxlength="80" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="telephone">Telephone Number</label>
</td>
<td valign="top">
<input type="text" name="telephone" maxlength="30" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="comments">Comments *</label>
</td>
<td valign="top">
<textarea name="comments" maxlength="1000" cols="25" rows="6">
</textarea>
</td>
</tr>
<tr> <td colspan="2" style="text-align:center">
<input type="submit" value="Submit">
</td>
</tr>
</table> </form>
And here is the php:
PHP Code:
<?php
if(isset($_POST['email'])) {
$email_to = "";
$email_subject = "Your email subject line";
function died($error) {
// error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comments']))
{ died('We are sorry, but there appears to be a problem with the form you submitted.'); }
$first_name = $_POST['first_name'];
// required
$last_name = $_POST['last_name'];
// required
$email_from = $_POST['email'];
// required
$telephone = $_POST['telephone'];
// not required
$comments = $_POST['comments'];
// required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) { $error_message .= 'The Email Address you entered does not appear to be valid.<br />';}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) { $error_message .= 'The First Name you entered does not appear to be valid.<br />';}
if(!preg_match($string_exp,$last_name)) { $error_message .= 'The Last Name you entered does not appear to be valid.<br />';}
if(strlen($comments) < 2) { $error_message .= 'The Comments you entered do not appear to be valid.<br />';}
if(strlen($error_message) > 0) { died($error_message); }
$email_message = "Form details below.\n\n";
function clean_string($string) { $bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string); }
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n"; $email_message .= "Comments: ".clean_string($comments)."\n"; }
?>
|

July 20th, 2012, 03:52 PM
|
 |
Likely to be eaten by a grue.
|
|
Join Date: Oct 2006
Location: Pennsylvania, USA
|
|
Given that the OP contains an edit message from another forum, I'm sure nobody is surprised that he's been cross-posting to at least 2 other forums.
This thread is a spam magnet, so I'm closing it.
__________________
HEY! YOU! Read the New User Guide and Forum Rules
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin
"The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002
Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|