skip to Main Content

Thanks in advance for your help.

On a HTML/CSS/JS site I’m making the contact form provided on the theme works fine however I can’t seem to get the phone number field to work. I’ve tried it without using sanitize, changing the id’s, nothing seems to work.

HTML:

 <section class="contact-page">
            <div class="container">
                <div class="section-title text-center">
                    <div class="section-title__icon">
                        <img src="assets/images/icon/section-title-icon.png" alt="">
                    </div>
                    <span class="section-title__tagline">Contact with us now</span>
                    <h2 class="section-title__title">Feel free to write gardon <br> anytime</h2>
                </div>
                <div class="contact-page__form-box">
                    <form action="/assets/contact-form-handler.php" class="contact-page__form contact-form-validated"
                        method="post">
                        <div class="row">
                            <div class="col-xl-6 col-lg-6 col-md-6">
                                <div class="contact-page__form-input-box">
                                    <input type="text" id="name" placeholder="Your name" name="visitor_name" pattern=[A-Zsa-z]{3,20} required>
                                </div>
                            </div>
                            <div class="col-xl-6 col-lg-6 col-md-6">
                                <div class="contact-page__form-input-box">
                                    <input type="email" id="email" placeholder="Email address" name="visitor_email" required>
                                </div>
                            </div>
                            <div class="col-xl-6 col-lg-6 col-md-6">
                                <div class="contact-page__form-input-box">
                                    <input type="text" id="visitor_phone" placeholder="Phone" name="visitor_phone">
                                </div>
                            </div>
                            <div class="col-xl-6 col-lg-6 col-md-6">
                                <div class="contact-page__form-input-box">
                                    <select class="selectpicker" aria-label="Default select example">
                                        <option selected>Select service</option>
                                        <option value="1">Select Service 01</option>
                                        <option value="2">Select Service 02</option>
                                        <option value="3">Select Service 02</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-xl-12">
                                <div class="contact-page__form-input-box text-message-box">
                                    <textarea name="visitor_message" id="visitor_message" name="visitor_message" placeholder="Write a message"></textarea>
                                </div>
                                <div class="contact-page__form-btn-box">
                                    <button type="submit" class="thm-btn contact-page__form-btn">Send a Message</button>
                                </div>
                            </div>
                        </div>
                    </form>
                    <div class="result"></div>
                </div>
            </div>
        </section>

PHP contact-form-handler.php

<?php
 
if($_POST) {
    $visitor_name = "";
    $visitor_email = "";
    $email_title = "";
    $visitor_phone = "";
    $visitor_message = "";
    $email_body = "<div>";
     
    if(isset($_POST['visitor_name'])) {
        $visitor_name = filter_var($_POST['visitor_name'], FILTER_SANITIZE_STRING);
        $email_body .= "<div> 
<label><b>Visitor Name:</b></label>&nbsp;<span>".$visitor_name."</span> 
</div>";
    }
    if(isset($_POST['visitor_email'])) {
        $visitor_email = str_replace(array("r", "n", "%0a", "%0d"), '', $_POST['visitor_email']);
        $visitor_email = filter_var($visitor_email, FILTER_VALIDATE_EMAIL);
        $email_body .= "<div> 
<label><b>Visitor Email:</b></label>&nbsp;<span>".$visitor_email."</span> 
</div>";
    }
     
    if(isset($_POST['email_title'])) {
        $email_title = filter_var($_POST['email_title'], FILTER_SANITIZE_STRING);
        $email_body .= "<div> 
<label><b>Reason For Contacting Us:</b></label>&nbsp;<span>".$email_title."</span> 
</div>";
    }
    
       if(isset($_POST['visitor_phone'])) {
        $concerned_department = filter_var($_POST['visitor_phone'], FILTER_SANITIZE_NUMBER_INT);
        $email_body .= "<div> 
<label><b>Phone Number:</b></label>&nbsp;<span>".$visitor_phone."</span> 
</div>";
    }
    
    if(isset($_POST['visitor_message'])) {
        $visitor_message = htmlspecialchars($_POST['visitor_message']);
        $email_body .= "<div> 
<label><b>Visitor Message:</b></label> 
<div>".$visitor_message."</div> 
</div>";
    }
     
    $recipient = "[email protected]"; 
    $email_body .= "</div>";
    $headers  = 'MIME-Version: 1.0' . "rn"
    .'Content-type: text/html; charset=utf-8' . "rn"
    .'From: ' . $visitor_email . "rn";
     
    if(mail($recipient, $email_title, $email_body, $headers)) {
        echo "<p>Thank you for contacting us, $visitor_name. You will get a reply within 24 hours.</p>";
    } else {
        echo '<p>We are sorry but the email did not go through.</p>';
    }
     
} else {
    echo '<p>Something went wrong</p>';
}
?>

If someone could point me in the right direction that would be great.

Thanks in advance.

Have tried changing id’s and names in both HTML and the PHP file. Also tried removing sanitize, and tried just adding the visitor_phone to the name box, the PHP file isn’t getting it from the HTML doc for some reason.

No matter how I have it, the email pulls the other data fine and the phone number just remains empty.

2

Answers


  1. In the code section:

    if(isset($_POST['visitor_phone'])) {
        $concerned_department = filter_var($_POST['visitor_phone'], FILTER_SANITIZE_NUMBER_INT);
        $email_body .= "<div><label><b>Phone Number:</b></label>&nbsp;<span>".$visitor_phone."</span></div>";
    }
    

    Ypou are assigning the phone number information to ‘$concerned_department’ but are then printing out ‘$visitor_phone’.

    As an aside, you definitely do not want to sanitise a phone number to an integer, for a number of reasons. It may start with a zero, sometimes people put spaces in, in some countries they put punctuation in …

    Login or Signup to reply.
  2. You are storing the visitor’s phone number in $concerned_department and trying to print the value from $visitor_phone. Change either $concerned_department to $visitor_phone or vice-versa

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search