May 12, 200917 yr Hi Still being somewhat of a beginner, I have traditionally just used a basic contact page on the few sites I have designed with basic contact details and links to email address. However I am in the process of creating a site for a client who owns a holiday cottage and therefore I'm trying to create a contact page in a form-based layout (ie text area / radion buttons etc etc). At the moment I just have the form action as "mailto" and the method as "post" which obviously means the form submission creates a very basic raw email. I've read a little and understand that I need some kind of database / CGI but I really haven't a clue where to begin as I know nothing, absolutely nothing about this. My web hoster looks like they have facilities for PHP/MySql and I've tried looking through various tutorials but to be honest it all could be Russian to me! What I'm looking for is a recommendation for some kind on online tutorial for the basic novice which explains it like !. Do this, 2. Do that ..... (think you all get my drift). I want to learn but in a simple language, simply explained. Any pointers would be greatly appreciated. Thanks
May 12, 200917 yr Hi Still being somewhat of a beginner, I have traditionally just used a basic contact page on the few sites I have designed with basic contact details and links to email address. However I am in the process of creating a site for a client who owns a holiday cottage and therefore I'm trying to create a contact page in a form-based layout (ie text area / radion buttons etc etc). At the moment I just have the form action as "mailto" and the method as "post" which obviously means the form submission creates a very basic raw email. I've read a little and understand that I need some kind of database / CGI but I really haven't a clue where to begin as I know nothing, absolutely nothing about this. My web hoster looks like they have facilities for PHP/MySql and I've tried looking through various tutorials but to be honest it all could be Russian to me! What I'm looking for is a recommendation for some kind on online tutorial for the basic novice which explains it like !. Do this, 2. Do that ..... (think you all get my drift). I want to learn but in a simple language, simply explained. Any pointers would be greatly appreciated. Thanks What exactly do you need the form to do? If you only need to email the form data, then you only need PHP or a CGI script, not MySQL. When I first started working on websites, I used a free CGI script to send forms for me, which was very easy to use. You could create the email template in a .txt file and your messages would be sent using it. Now I only use PHP having become so familiar with it. In order to create your form in PHP you'll need to learn some basic scripting that you can use for validation. A quick example of such as form would be: <?php include('header.php'); //file containing your HTML page header if($_POST['submit']) { $errors = array(); //get your form values (name, email etc are the name values that you set in the HTML of your form elements) $name = trim($_POST['name']); $email = trim($_POST['email']); $message = trim($_POST['msg']); if(empty($name)) { errors[] = 'Please enter your name'; } //perform additional validation as needed //if no errors occur then send the message if(count($errors) == 0) { $to = 'email@domain.com'; //recipient email address // subject $subject = 'New Contact Message'; // message $message = ' <b>From</b>: '.$name.'<br />'. '<b>Email</b>: '.$email.'<br />'. '<b>Message</b><br />: '.$message.'</br>'; //send as HTML to allow better customisation of layout $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; //set the 'from' email address $headers .= 'From: sender@domain.com' . "\r\n" . // Mail it mail($to, $subject, $message, $headers); echo "Message sent successfully!"; } } //show the form along with error messages if needed else { if(isset($errors)) { foreach($errors as $error) { echo $error.'<br />'; } } echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post"> <label for="name">Name</label> <input type="text" name="name" /> <label for="email">Email</label> <input type="text" name="email" /> <label for="message">Message</label> <textarea name="name" row="5" cols="20"></textarea> <input type="submit" name="submit" value="Send Message" /> </form>'; } include('footer.php'); //file containing your HTML page footer The code assumes that you have header.php and footer.php files saved in the same directory which contain your HTML header and footer respectively. The form obviously can be manipulated as you like, as long as you remember to retrieve the correct values when extract the $_POST data, and make sure everything is sent in the email.
May 14, 200917 yr Author What exactly do you need the form to do?If you only need to email the form data, then you only need PHP or a CGI script, not MySQL. I just need it to email form data. I've created the form and also a .php page and loaded them onto my hosts server. Code as follows (a simple script I found on the web): Web page ======= <html> <head><title>Mail sender</title></head> <body> <table width="400" border="0" align="center" cellpadding="3" cellspacing="1"> <tr> <td><strong>Contact Form </strong></td> </tr> </table> <table width="400" border="0" align="center" cellpadding="0" cellspacing="1"> <tr> <td><form name="form1" method="post" action="send_contact.php"> <table width="100%" border="0" cellspacing="1" cellpadding="3"> <tr> <td width="16%">Subject</td> <td width="2%">:</td> <td width="82%"><input name="subject" type="text" id="subject" size="50"></td> </tr> <tr> <td>Detail</td> <td>:</td> <td><textarea name="detail" cols="50" rows="4" id="detail"></textarea></td> </tr> <tr> <td>Name</td> <td>:</td> <td><input name="name" type="text" id="name" size="50"></td> </tr> <tr> <td>Email</td> <td>:</td> <td><input name="customer_mail" type="text" id="customer_mail" size="50"></td> </tr> <tr> <td> </td> <td> </td> <td><input type="submit" name="Submit" value="Submit"> <input type="reset" name="Submit2" value="Reset"></td> </tr> </table> </form> </td> </tr> </table></body> </html> PHP doc ====== <html> <head><title>PHP Mail Sender</title></head> <body> <?php // Contact subject $subject ="$subject"; // Details $message="$detail"; // Mail of sender $mail_from="$customer_mail"; // From $header="from: $name <$mail_from>"; $to ='(myemail address etc)'; $send_contact=mail($to,$subject,$message,$header); // Check, if message sent to your email // display message "We've recived your information" if($send_contact){ echo "We've recived your contact information"; } else { echo "ERROR"; } ?></body> </html> Everything works ok in that an email is sent AND received in my test email address but the email itself is completely blank and doesn't display any of the content I've typed into the form as a test. I'm guessing that it's something I haven't set up or configured somewhere (maybe with my hosting company) ??
May 14, 200917 yr <html> <head><title>PHP Mail Sender</title></head> <body> <?php // Contact subject $subject =$_POST['subject']; // Details $message= $_POST['detail']; // Mail of sender $mail_from=$_POST['customer_mail']; $name = $_POST['name']; // From $header="from: $name <$mail_from>"; $to ='(myemail address etc)'; $send_contact=mail($to,$subject,$message,$header); // Check, if message sent to your email // display message "We've recived your information" if($send_contact){ echo "We've recived your contact information"; } else { echo "ERROR"; } ?></body> </html> Change the PHP code to this. You also need to modify the following line to match your email address $to ='(myemail address etc)'; eg $to = 'mail@mysite.com';
May 14, 200917 yr Author Change the PHP code to this. You also need to modify the following line to match your email address$to ='(myemail address etc)'; eg $to = 'mail@mysite.com'; I had just typed in the "myemail address etc" part to hide my live mail address on here. Changed the php code as you suggested and it workes a treat. Many thanks for the help!
Create an account or sign in to comment