January 30, 200917 yr Hi All, Below is my code, this is my first attempt of php coding in more depth so be gentle.... If you test the code you will see the following come above. From the image you will see the error messages from my code how do I get rid of that, so it just displays the errors when they accur? The form and everything works great it's just that i cant figure out. Here's the code: <?php $name = trim($_POST['name']); $email = trim($_POST['email']); $phone = trim($_POST['phone']); $comments = trim($_POST['comments']); $name_check = checkInput($name, "50"); $email_check = checkEmail($email); $phone_check = checkInput($name, "20"); $comments_check = checkInput($comments); $form ="<form action=\"contact.php\" method=\"post\">"; $form.="<table><tr><td align=\"left\" valign=\"top\">Name:</td><td><input type=\"text\" name=\"name\" size=\"50\" value=\"$name\"></td></tr>"; $form.="<tr><td align=\"left\" valign=\"top\">Email:</td><td><input type=\"text\" name=\"email\" size=\"50\" value=\"$email\"></td></tr>"; $form.="<tr><td align=\"left\" valign=\"top\">Phone:</td><td><input type=\"text\" name=\"phone\" size=\"50\" value=\"$phone\"></td></tr>"; $form.="<tr><td align=\"left\" valign=\"top\">Comments:</td><td><textarea name=\"comments\" cols=\"40\" rows=\"7\">"; $form.="$comments</textarea></td></td>"; $form.="<tr><td></td><td><input type=\"submit\" name=\"sent\" value=\"Send\"></td></tr></table>"; $form.="</form>"; $output=""; function checkEmail($email){ if(eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$', $email)){ return true; } else{ return false; } } function checkInput($input,$length="5000"){ $exp="/^[a-zA-Z\s\d_\-\.\,\°\(\)\@\:]{1,".$length."}$/i"; if (preg_match($exp, $input)) { return true; } else{ return false; } } if ($name_check==false||$email_check==false||$phone_check==false||$comments_check==false) { if($name_check==false){ $output.="Your Name is invalid, Please Try again.<br/>"; } if($email_check==false){ $output.="-Your Email is invalid, Please Try again.<br/>"; } if($phone_check==false){ $output.="-Your phone number appears to have invalid characters, Please try again<br/>"; } if($comments_check==false){ $output.="-Please enter a message<br/>"; } $output.=$form; } else{ $to_send="Name: ".$name."\n\nEmail Address: ".$email."\n\nPhone Number: ".$phone."\n\nComments: ".$comments; $send=mail ("sales@xpress-media.co.uk","Website Feedback", $to_send,"From: $email"); if ($send==false) { $output.="Sorry we are unable to process your request, please check your details and try again"; } else { $user_msg="Thank you for your comments, we will be in touch very soon\n\n\nKind Regards,\n\nXpress Media"; $user_send=mail($email, "Thank you for your feedback",$user_msg,"From: Xpress Media <sales@xpress-media.co.uk>"); if($user_send==false) { $output.="Thanks".$form; } else { $output.="<h2>Thank you $name, we will be in touch within 24 hours.</h2>"; } } } if ($output==true) { echo $output; } ?>
January 30, 200917 yr Try adding all of your code within this IF Statement: if( isset( $_POST['sent'] ) ) { // Code to execute if the button was pressed } else { // Code to execute if the button wasn't pressed. In your case, just show the form } That checks if the form was sent by seeing if the input field named "sent" has been activated. Edit: Changed to $_POST, thanks for mentioning that scaz.
January 30, 200917 yr Author Try adding all of your code within this IF Statement: if( isset( $_GET['sent'] ) ) { // Code to execute if the button was pressed } else { // Code to execute if the button wasn't pressed. In your case, just show the form } That checks if the form was sent by seeing if the input field named "sent" has been activated. All he All the code below <?php ????? Thanks
January 30, 200917 yr Author All the code below <?php ????? Thanks Not sure I get you, can you use my code and show me what you mean, I'm still new to this and picking bits up as I go along much appreciated. Lee
January 30, 200917 yr The way you have structured it makes this a little complicated as you only have one echo statement which is essentially all the appropriate content concatenated into one variable. Wondering Soul's structure would be more suited but ensure you use 'if( isset( $_POST['sent'] ) )' instead of 'if( isset( $_GET['sent'] ) )' So elaborating on what Woundering Soul said: : if( isset( $_POST['sent'] ) ) { // Stick your PHP validation and sending in here. } else { // Stick your HTML form in here. }
January 30, 200917 yr Author The way you have structured it makes this a little complicated as you only have one echo statement which is essentially all the appropriate content concatenated into one variable. Wondering Soul's structure would be more suited but ensure you use 'if( isset( $_POST['sent'] ) )' instead of 'if( isset( $_GET['sent'] ) )' So elaborating on what Woundering Soul said: : if( isset( $_POST['sent'] ) ) { // Stick your PHP validation and sending in here. } else { // Stick your HTML form in here. } I still don't get what you mean ?
January 31, 200917 yr Author I still don't get what you mean ? Anyone!!! I need to sort this out can someone help me please
January 31, 200917 yr Stop spamming. The reason you are getting no more responses is because you have had 2 very helpful replies and you don't seem to get it. This is going to make people think that they can't help so they won't bother. Instead of 'I don't get it'. Try telling us what you don't get because scaz182 has made a good, straight forward reply. Did you actually write this code from scratch? If you did then you should be able to follow scaz182's advice.
January 31, 200917 yr Author I dont understand where i have to put the following if( isset( $_POST['sent'] ) ) { // Stick your PHP validation and sending in here. } else { // Stick your HTML form in here. } Has my code starts off: <?php $name = trim($_POST['name']); $email = trim($_POST['email']); $phone = trim($_POST['phone']); $comments = trim($_POST['comments']); $name_check = checkInput($name, "50"); $email_check = checkEmail($email); $phone_check = checkInput($name, "20"); $comments_check = checkInput($comments); $form ="<form action=\"contact.php\" method=\"post\">"; $form.="<table><tr><td align=\"left\" valign=\"top\">Nam then the form follows so not sure on where to put it
January 31, 200917 yr move your $form variable below <?php then put if( isset( $_POST['sent'] ) ) { above $name = trim($_POST['name']); then put } else { echo $form; } below $output.="<h2>Thank you $name, we will be in touch within 24 hours.</h2>"; } } } Looking at your code that should work but overall the code is pretty messy and very unconventional.
January 31, 200917 yr IF you still can't get it to work after Shaun's advice this should do it: <?php // Display form if 'sent' not set if (!isset($_POST['sent'])) { ?> <form action="contact.php" method="post"> <table><tr><td align="left" valign="top">Name:</td><td><input type="text" name="name" size="50" ></td></tr> <tr><td align="left" valign="top">Email:</td><td><input type="text" name="email" size="50" ></td></tr> <tr><td align="left" valign="top">Phone:</td><td><input type="text" name="phone" size="50" ></td></tr> <tr><td align="left" valign="top">Comments:</td><td><textarea name="comments" cols="40" rows="7"></textarea></td></td> <tr><td></td><td><input type="submit" name="sent" value="Send"></td></tr></table> </form> <?php } else { function checkEmail($email){ if(eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$', $email)){ return true; } else{ return false; } } function checkInput($input,$length="5000"){ $exp="/^[a-zA-Z\s\d_\-\.\,\°\(\)\@\:]{1,".$length."}$/i"; if (preg_match($exp, $input)) { return true; } else{ return false; } } $name = trim($_POST['name']); $email = trim($_POST['email']); $phone = trim($_POST['phone']); $comments = trim($_POST['comments']); $name_check = checkInput($name, "50"); $email_check = checkEmail($email); $phone_check = checkInput($name, "20"); $comments_check = checkInput($comments); if ($name_check==false||$email_check==false||$phone_check==false||$comments_check==false) { if($name_check==false){ $output.="Your Name is invalid, Please Try again.<br/>"; } if($email_check==false){ $output.="-Your Email is invalid, Please Try again.<br/>"; } if($phone_check==false){ $output.="-Your phone number appears to have invalid characters, Please try again<br/>"; } if($comments_check==false){ $output.="-Please enter a message<br/>"; } } else{ $to_send="Name: ".$name."\n\nEmail Address: ".$email."\n\nPhone Number: ".$phone."\n\nComments: ".$comments; $send=mail ("sales@xpress-media.co.uk","Website Feedback", $to_send,"From: $email"); if ($send==false) { $output.="Sorry we are unable to process your request, please check your details and try again"; } else { $user_msg="Thank you for your comments, we will be in touch very soon\n\n\nKind Regards,\n\nXpress Media"; $user_send=mail($email, "Thank you for your feedback",$user_msg,"From: Xpress Media <sales@xpress-media.co.uk>"); if($user_send==false) { $output.="Thanks"; } else { $output.="<h2>Thank you $name, we will be in touch within 24 hours.</h2>"; } } } if ($output==true) { echo $output; } } ?>
January 31, 200917 yr Author Done problem solved, Thanks to everyone involed in helping me I now understand where I went wrong..... Thanks guys Lee
Create an account or sign in to comment