Accurax Posted October 31, 2006 Share Posted October 31, 2006 Hi guys,At themmoment i have a very simple mail form on my "contact us" page, it's built around 2 pages [b]contactus.php[/b] & [b]contatcsend.php[/b]Contactus.php simply contains a form as follows[code] <form action="contactsend.php" method="POST"> <div align="center"> <table width="0%" height="0" border="0" align="center" cellpadding="0" cellspacing="0" class="contacttable"> <tr> <td colspan="2"><p align="center"><strong>Fill in the form below to make an Enquiry</strong></p></td> </tr> <tr> <td><p align="left"><b>Company name:</b></p></td> <td><input name="company" type ="text" size="50" maxlength="30" /> <div align="center"></div></td> </tr> <tr> <td><p align="left"><b>Your Name:</b></p></td> <td><input name="name" type ="text" size="50" maxlength="30" /> <div align="center"></div></td> </tr> <tr> <td><p align="left"><b>Your Email:</b></p></td> <td><input name="email" type ="text" size="50" maxlength="50" /></td> </tr> <tr> <td><p align="left"><b>Contact Number:</b></p></td> <td><input name="contact" type ="text" size="50" maxlength="25" /></td> </tr> <tr> <td width="150"><p align="left"><b>Your Messsage:</b></p></td> <td><textarea name="message" cols="50" rows="8"></textarea></td> </tr> <tr> <td class="bottomleft"> </td> <td><input name="submit" type="submit" value="Send Message" /> </td> </tr> </table> </p></div> </form>[/code]as you can see this is just an HTML form and its "action" is contactsend.phpWhich containts the following php code[code] <? //contact form$company=$_POST['company'];$name=$_POST['name'];$email=$_POST['email'];$contact=$_POST['contact'];$message=$_POST['message'];$ip = $_SERVER['REMOTE_ADDR'];$time = time();$date_message = date("r", $time);$subject="New Enquiry";$email="deleted@mydescretion.co.uk";//format the message$msg .= "IP: $ip\n";$msg .= "$date: $date_message\n";$msg .= "$subject\n";$msg .= "Company: $_POST[company]\n";$msg .= "Name: $_POST[name]\n";$msg .= "Email: $_POST[email]\n";$msg .= "Message: $_POST[message]\n";mail($email,$subject,$msg);echo("Your message has been sent, We will contact you with a reply as soon as possible.");?>[/code]As you can see this script simply formats the message and pulls the IP address / generates the time and date.I would like to develop this form to do 2 things in the short term.1) check all the fields have content2) check the email address is of the form x@x.xIve been playing with some ifelse statements that i found..... but im not really sure where to put them or how to make them work.I'm feeling a bit lost on this to be honest, and have spent too long on it now, i need to get this understood and ready to go. Can anyone help me with this please? Quote Link to comment https://forums.phpfreaks.com/topic/25712-taking-my-mail-form-to-the-next-level/ Share on other sites More sharing options...
KevinM1 Posted October 31, 2006 Share Posted October 31, 2006 Well, you could combine your pages into one file (say contact.php):[code]<?phpif(isset($_POST['submit'])){ //has the form been submitted? //check all of the inputs if(!empty($_POST['company'])){ //if the company field of the form is not empty //process the info } else{ //company field hasn't been filled -- error }...} //end of if(isset(...else{ //if it hasn't been submitted, display the form?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">...<?php} //end of the else block?>[/code]A script can refer to itself (which is what the echo statement in the form does), so you can have a script both display a form and process it upon submission, with the basic outline I showed above. I've also shown how to check if an input field is empty or not (only works in textfields, I believe).For your e-mail structure, you'll probably have to use regular expressions. I'm not very familiar with PHP's regex functionality, so I'll leave that for someone else to handle. ;) Quote Link to comment https://forums.phpfreaks.com/topic/25712-taking-my-mail-form-to-the-next-level/#findComment-117357 Share on other sites More sharing options...
Accurax Posted October 31, 2006 Author Share Posted October 31, 2006 I think your gonna have to dumb this down a little for me please.I now have the following[code] <form action=\"".$_SERVER['PHP_SELF']."\" method=\"post\"> <div align="center"> <table width="0%" height="0" border="0" align="center" cellpadding="0" cellspacing="0" class="contacttable"> <tr> <td colspan="2"><p align="center"><strong>Fill in the form below to make an Enquiry</strong></p></td> </tr> <tr> <td><p align="left"><b>Company name:</b></p></td> <td><input name="company" type ="text" size="50" maxlength="30" /> <div align="center"></div></td> </tr> <tr> <td><p align="left"><b>Your Name:</b></p></td> <td><input name="name" type ="text" size="50" maxlength="30" /> <div align="center"></div></td> </tr> <tr> <td><p align="left"><b>Your Email:</b></p></td> <td><input name="email" type ="text" size="50" maxlength="50" /></td> </tr> <tr> <td><p align="left"><b>Contact Number:</b></p></td> <td><input name="contact" type ="text" size="50" maxlength="25" /></td> </tr> <tr> <td width="150"><p align="left"><b>Your Messsage:</b></p></td> <td><textarea name="message" cols="50" rows="8"></textarea></td> </tr> <tr> <td class="bottomleft"> </td> <td><input name="submit" type="submit" value="Send Message" /> </td> </tr> </table> </p></div> </form> <? //contact form$company=$_POST['company'];$name=$_POST['name'];$email=$_POST['email'];$contact=$_POST['contact'];$message=$_POST['message'];$ip = $_SERVER['REMOTE_ADDR'];$time = time();$date_message = date("r", $time);$subject="New Enquiry";$email="me@mine.com";//format the message$msg .= "IP: $ip\n";$msg .= "$date: $date_message\n";$msg .= "$subject\n";$msg .= "Company: $_POST[company]\n";$msg .= "Name: $_POST[name]\n";$msg .= "Email: $_POST[email]\n";$msg .= "Message: $_POST[message]\n";mail($email,$subject,$msg);echo("Your message has been sent, We will contact you with a reply as soon as possible.");?>[/code]All on the same page.......... the form still submits when the button is pressed ... but, on by virtue of the page refreshing.The form auto submits itself as soon as the page is loaded ......... I'm afraid i still dont understand how to check a field is filled in or not.Sorry if im being stupid ??? :'( Quote Link to comment https://forums.phpfreaks.com/topic/25712-taking-my-mail-form-to-the-next-level/#findComment-117373 Share on other sites More sharing options...
KevinM1 Posted October 31, 2006 Share Posted October 31, 2006 Well, right now you have the order backwards. The most logical thing to do is process the form if it's been submitted. If it [b]hasn't[/b] been submitted, then you output the form. I've rewritten it into a form that should work below:[code]<?php$errorMessage = ''; //initialize our error messageif(isset($_POST['submit'])){ //if the form has been submitted... if(!empty($_POST['company'])){ //if company isn't empty $company = $_POST['company']; //set the local variable to what's been posted $comp = TRUE; //set a boolean for future use } else{ //company IS empty $errorMessage .= "Please input your company name!<br />"; //add to our error message $comp = FALSE; //set a boolean for future use } if(!empty($_POST['name'])){ $name = $_POST['name']; $n = TRUE; } else{ $errorMessage .= "Please input your name!<br />"; $n = FALSE; } if(!empty($_POST['email'])){ $email = $_POST['email']; $e = TRUE; } else{ $errorMessage .= "Please input your e-mail address!<br />"; $e = FALSE; } if(!empty($_POST['contact'])){ $contact = $_POST['contact']; $cont = TRUE; } else{ $errorMessage .= "Please input your contact number!<br />"; $cont = FALSE; } if(!empty($_POST['message'])){ $message = $_POST['message']; $mess = TRUE; } else{ $errorMessage .= "Please input your message!"; $mess = FALSE; } if($comp && $n && $e && $cont && $mess){ //if everything's been filled out correctly, send the mail $ip = $_SERVER['REMOTE_ADDR']; $time = time(); $date_message = date("r", $time); $subject="New Enquiry"; $email="me@mine.com"; //format the message $msg .= "IP: $ip\n"; $msg .= "$date: $date_message\n"; $msg .= "$subject\n"; $msg .= "Company: $company\n"; $msg .= "Name: $name\n"; $msg .= "Email: $email\n"; $msg .= "Message: $message\n"; mail($email,$subject,$msg); echo "Your message has been sent. We will contact you with a reply as soon as possible."; } else{ //something is wrong, so print our error message echo "<span style="color: red;">Could not send e-mail for the following reasons:<br />$errorMessage"; }else{ //the form HASN'T been submitted yet ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <div align="center"> <table width="0%" height="0" border="0" align="center" cellpadding="0" cellspacing="0" class="contacttable"> <tr> <td colspan="2"><p align="center"><strong>Fill in the form below to make an Enquiry</strong></p></td> </tr> <tr> <td><p align="left"><b>Company name:</b></p></td> <td><input name="company" type ="text" size="50" maxlength="30" /> <div align="center"></div></td> </tr> <tr> <td><p align="left"><b>Your Name:</b></p></td> <td><input name="name" type ="text" size="50" maxlength="30" /> <div align="center"></div></td> </tr> <tr> <td><p align="left"><b>Your Email:</b></p></td> <td><input name="email" type ="text" size="50" maxlength="50" /></td> </tr> <tr> <td><p align="left"><b>Contact Number:</b></p></td> <td><input name="contact" type ="text" size="50" maxlength="25" /></td> </tr> <tr> <td width="150"><p align="left"><b>Your Messsage:</b></p></td> <td><textarea name="message" cols="50" rows="8"></textarea></td> </tr> <tr> <td class="bottomleft"> </td> <td><input name="submit" type="submit" value="Send Message" /></td> </tr> </table> </p> </div> </form> <?php} //ending bracket for our else-conditional?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/25712-taking-my-mail-form-to-the-next-level/#findComment-117403 Share on other sites More sharing options...
Accurax Posted November 1, 2006 Author Share Posted November 1, 2006 WowJust wow ... thanks your amazing .Ive spent all morning going over it, and im pretty down with the logic behind how it works now....However i am getting a persistant error that i cant figure out..... can anyone shed some light on what it means[code]Parse error: parse error, unexpected T_STRING, expecting ',' or ';' in path/path/path/www/contactus.php on line 133[/code]Line 133 happens to be:[code] else{ //something is wrong, so print our error message [b]132[/b] echo "<span style="color: red;">Could not send e-mail for the following reasons:<br />$errorMessage"; [b]133[/b] } [b]134[/b][/code]Any idea's? Quote Link to comment https://forums.phpfreaks.com/topic/25712-taking-my-mail-form-to-the-next-level/#findComment-117754 Share on other sites More sharing options...
redarrow Posted November 1, 2006 Share Posted November 1, 2006 [code]<?php$errorMessage = ''; //initialize our error messageif(isset($_POST['submit'])){ //if the form has been submitted... if(!empty($_POST['company'])){ //if company isn't empty $company = $_POST['company']; //set the local variable to what's been posted $comp = TRUE; //set a boolean for future use } else{ //company IS empty $errorMessage .= "Please input your company name!<br />"; //add to our error message $comp = FALSE; //set a boolean for future use } if(!empty($_POST['name'])){ $name = $_POST['name']; $n = TRUE; } else{ $errorMessage .= "Please input your name!<br />"; $n = FALSE; } if(!empty($_POST['email'])){ $email = $_POST['email']; $e = TRUE; } else{ $errorMessage .= "Please input your e-mail address!<br />"; $e = FALSE; } if(!empty($_POST['contact'])){ $contact = $_POST['contact']; $cont = TRUE; } else{ $errorMessage .= "Please input your contact number!<br />"; $cont = FALSE; } if(!empty($_POST['message'])){ $message = $_POST['message']; $mess = TRUE; } else{ $errorMessage .= "Please input your message!"; $mess = FALSE; } if($comp && $n && $e && $cont && $mess){ //if everything's been filled out correctly, send the mail $ip = $_SERVER['REMOTE_ADDR']; $time = time(); $date_message = date("r", $time); $subject="New Enquiry"; $email="me@mine.com"; //format the message $msg .= "IP: $ip\n"; $msg .= "$date: $date_message\n"; $msg .= "$subject\n"; $msg .= "Company: $company\n"; $msg .= "Name: $name\n"; $msg .= "Email: $email\n"; $msg .= "Message: $message\n"; mail($email,$subject,$msg); echo "Your message has been sent. We will contact you with a reply as soon as possible."; } else{ //something is wrong, so print our error message echo "<span style='color: red;'>Could not send e-mail for the following reasons:<br />$errorMessage"; }else{ //the form HASN'T been submitted yet ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <div align="center"> <table width="0%" height="0" border="0" align="center" cellpadding="0" cellspacing="0" class="contacttable"> <tr> <td colspan="2"><p align="center"><strong>Fill in the form below to make an Enquiry</strong></p></td> </tr> <tr> <td><p align="left"><b>Company name:</b></p></td> <td><input name="company" type ="text" size="50" maxlength="30" /> <div align="center"></div></td> </tr> <tr> <td><p align="left"><b>Your Name:</b></p></td> <td><input name="name" type ="text" size="50" maxlength="30" /> <div align="center"></div></td> </tr> <tr> <td><p align="left"><b>Your Email:</b></p></td> <td><input name="email" type ="text" size="50" maxlength="50" /></td> </tr> <tr> <td><p align="left"><b>Contact Number:</b></p></td> <td><input name="contact" type ="text" size="50" maxlength="25" /></td> </tr> <tr> <td width="150"><p align="left"><b>Your Messsage:</b></p></td> <td><textarea name="message" cols="50" rows="8"></textarea></td> </tr> <tr> <td class="bottomleft"> </td> <td><input name="submit" type="submit" value="Send Message" /></td> </tr> </table> </p> </div> </form> <?php} //ending bracket for our else-conditional?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/25712-taking-my-mail-form-to-the-next-level/#findComment-117758 Share on other sites More sharing options...
Accurax Posted November 1, 2006 Author Share Posted November 1, 2006 Thanks, i should have noticed those "..." tbh....... I'm now getting this error:[code]Parse error: parse error, unexpected T_ELSE in /path/path/path/path/www/contactus.php on line 137[/code]I dont really want someone to just fix it for me....... but id be really please if someone could explain what the error means, and help me figure it out for myself.Ive checked all the Brace's and they all appear to close ... ive also checked that there are now else statements without If staements and vis a vis .....Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/25712-taking-my-mail-form-to-the-next-level/#findComment-117769 Share on other sites More sharing options...
Yesideez Posted November 1, 2006 Share Posted November 1, 2006 Try looking in the previous line as the actual error is often found there. Quote Link to comment https://forums.phpfreaks.com/topic/25712-taking-my-mail-form-to-the-next-level/#findComment-117787 Share on other sites More sharing options...
Accurax Posted November 1, 2006 Author Share Posted November 1, 2006 [me=Accurax]feels stupid[/me]Was missing a bracesorted now, thanks for the help guys Quote Link to comment https://forums.phpfreaks.com/topic/25712-taking-my-mail-form-to-the-next-level/#findComment-117796 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.