404 Posted March 14, 2020 Share Posted March 14, 2020 I'm working on a Contact Form for my website. It works, except the "Phone" field doesn't show a phone number in the sent message. It's just blank where the number should be displayed. What am I doing wrong? - Thanks HTML: <div class="contact-form"> <h2 class="style">Contact Us</h2> <form action="contact.php" method="post" id="form"> <div> <span><label>NAME</label></span> <span><input name="cf_name" type="text" class="textbox"></span> </div> <div> <span><label>EMAIL</label></span> <span><input name="cf_email" type="text" class="textbox"></span> </div> <div> <span><label>PHONE</label></span> <span><input name="cf_phone" type="text" class="textbox"></span> </div> <div> <span><label>MESSAGE</label></span> <span><textarea name="cf_message"> </textarea></span> </div> <div> <span><input type="submit" value="Send"></span> </div> </form> </div> PHP: <?php $field_name = $_POST['cf_name']; $field_email = $_POST['cf_email']; $field_phone = $_POST['cf_phone']; $field_message = $_POST['cf_message']; $mail_to = 'somebody@invalid.com'; $subject = 'Message from a website visitor '.$field_name; $body_message = 'From: '.$field_name."\n"; $body_message .= 'E-mail: '.$field_email."\n"; $body_message .= 'Phone: '.$field_phone."\n"; $body_message .= 'Message: '.$field_message; $headers = 'From: '.$field_email."\r\n"; $headers .= 'Reply-To: '.$field_email."\r\n"; $mail_status = mail($mail_to, $subject, $body_message, $headers); if ($mail_status) { ?> <script language="javascript" type="text/javascript"> alert('Thank you for the message. We will contact you shortly.'); window.location = 'index.html'; </script> <?php Link to comment Share on other sites More sharing options...
dsonesuk Posted March 15, 2020 Share Posted March 15, 2020 As far as I can make it should work? But it could be a cache problem in that it's still using the old form from cache, suggest you clear history especially form in browser. Then run again. Link to comment Share on other sites More sharing options...
404 Posted March 19, 2020 Author Share Posted March 19, 2020 (edited) Thanks for responding. I made a typo in the code ...it's working now . The only thing that doesn't seem to work is the error section at the bottom of the PHP code (which I left out in my previous post - oops!). If the sender doesn't fill out any field correctly or even leaves it blank, the form sends the message anyway. Here's the missing bit of PHP: } else { ?> <script language="javascript" type="text/javascript"> alert('Message failed. Please try again or contact us directly by phone.'); window.location = 'index.html'; </script> <?php } ?> Edited March 19, 2020 by 404 Link to comment Share on other sites More sharing options...
dsonesuk Posted March 19, 2020 Share Posted March 19, 2020 All the required parameters in mail() are filled (To, Subject, Message). It does not matter if any field form input is empty or wrong, it does not know that! If these have anything the mail is sent. You have to validate all the data before its allowed to get to mail() function. Link to comment Share on other sites More sharing options...
404 Posted March 19, 2020 Author Share Posted March 19, 2020 (edited) Thanks ...I guess that should have been obvious 🙂. Do you know of a tutorial somewhere online that would help me with adding the proper validation? Edited March 19, 2020 by 404 Link to comment Share on other sites More sharing options...
dsonesuk Posted March 19, 2020 Share Posted March 19, 2020 https://www.w3schools.com/php/php_form_url_email.asp Link to comment Share on other sites More sharing options...
404 Posted March 21, 2020 Author Share Posted March 21, 2020 Thanks! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now