0

I am using a theme and this is the php that came with it. I am testing out the emails and one thing I am noticing is that a few sections are contniuosly popping up blank no matter what I do. This is my first time using php and Im not sure if I should delete the themes php and create my own. Specifically the Number,Address,City,State,Date,Zip Code are the texts that show up blank. I receive the emails but those sections are continuously left blank. Is my code not correct?

 <php>

          <?php    

if ($_POST['fname']) {

    // Your Email
    $recipient = "thttkidd@yahoo.com"; // PLEASE SET YOUR EMAIL ADDRESS

    // Check $recipient
    if($recipient === '') {
        returnAndExitAjaxResponse(
            constructAjaxResponseArray(
                FALSE,
                'RECIPIENT_IS_NOT_SET',
                array('error_message'=> 'RECIPIENT email address is not set. Please configure the script.')
            )
        );
    }

    // Check for empty required field
    if(!isset($_POST["email"]) || !isset($_POST["fname"]) || !isset($_POST["message"])) {
        returnAndExitAjaxResponse(
            constructAjaxResponseArray(
                FALSE,
                'MISSING_REQUIRED_FIELDS',
                array('error_message'=> 'MISSING_REQUIRED_FIELDS should not be occurred.')
            )
        );
    }

    // Sanitize input
    $fname  = filter_var($_POST["fname"], FILTER_SANITIZE_STRING);
    $lname  = filter_var($_POST["lname"], FILTER_SANITIZE_EMAIL);
    $website = $_POST["website"];
    if (!preg_match("~^(?:f|ht)tps?://~i", $website)) $website = "http://" . $website;
    $website = filter_var($website, FILTER_VALIDATE_URL);
    $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
    $message = filter_var($_POST["message"], FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES);
$number = filter_var($_POST["number"], FILTER_SANITIZE_STRING);
$adress = filter_var($_POST["Adress"], FILTER_SANITIZE_STRING);
$City = filter_var($_POST["City"], FILTER_SANITIZE_STRING);
$State = filter_var($_POST["State"], FILTER_SANITIZE_STRING);
$Date = filter_var($_POST["date"], FILTER_SANITIZE_STRING);
$Zcode = filter_var($_POST["ZCode"], FILTER_SANITIZE_STRING);
var_dump($_POST);

    // If non required fields are empty
    if ( empty($lname) ){
        $lname = "No last name entered.";
    }

    // Headers
    $headers = 'From: '.$fname.' <'.$email.'>' . "\r\n";
    $headers .= 'Reply-To: '.$email.'' . "\r\n";
    $headers .= 'X-Mailer: PHP/' . phpversion();

    // Subject
    $subject = "New email from book now form";

    // Build Message
    $email_content = "First Name: $fname\n";
    $email_content .= "Last Name: $lname\n";
    $email_content .= "Email: $email\n\n";
    $email_content .= "Number: $number\n";
    $email_content .= "Address: $adress\n\n";
    $email_content .= "City: $City\n\n";
    $email_content .= "State $State\n\n";
    $email_content .= "Date $Date\n\n";
    $email_content .= "Zcode $Zcode\n\n";
    $email_content .= "Message:\n$message\n\n\n";
    $email_content .= "CLIENT IP:\n".get_client_ip()."\n";
    $email_content .= "HOST IP:\n".$_SERVER['SERVER_ADDR']."\n";

    // Check if sent
    try {
        $sendmailResult = mail($recipient, $subject, $email_content, $headers);
        if( $sendmailResult === TRUE ) {
            returnAndExitAjaxResponse(
                constructAjaxResponseArray(
                    TRUE
                )
            );
        } else {
            returnAndExitAjaxResponse(
                constructAjaxResponseArray(
                    FALSE,
                    'ERROR_AT_PHPMAIL',
                    array('error_information'=> error_get_last() )
                )
            );
        }
    } catch (Exception $_e) {
        returnAndExitAjaxResponse(
            constructAjaxResponseArray(
                TRUE,
                'ERROR_AT_PHPMAIL',
                array('error_message'=> $_e->getMessage())
            )
        );
    }

}
/*
    Construct ajax response array
    Input: Result (bool), Message (optional), Data to be sent back in array
*/
function constructAjaxResponseArray ($_response, $_message = '', $_json = null) {
    $_responseArray = array();
    $_response = ( $_response === TRUE ) ? TRUE : FALSE;
    $_responseArray['response'] = $_response;
    if(isset($_message)) $_responseArray['message'] = $_message;
    if(isset($_json)) $_responseArray['json'] = $_json;

    return $_responseArray;
}
/*
    Returns in the Gframe ajax format.
    Input: data array processed by constructAjaxResponseArray ()
    Outputs as a html stream then exits.
*/
function returnAndExitAjaxResponse ($_ajaxResponse) {
    if(!$_ajaxResponse){
        $_ajaxResponse = array('response'=>false,'message'=>'Unknown error occurred.');
    }
    header("Content-Type: application/json; charset=utf-8");
    echo json_encode($_ajaxResponse);
    die();
}


// Function to get the client IP address
function get_client_ip() {
    $ipaddress = '';
    if (isset($_SERVER['HTTP_CLIENT_IP'])) {
        $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
    } else if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } else if(isset($_SERVER['HTTP_X_FORWARDED'])) {
        $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
    } else if(isset($_SERVER['HTTP_FORWARDED_FOR'])) {
        $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
    } else if(isset($_SERVER['HTTP_FORWARDED'])) {
        $ipaddress = $_SERVER['HTTP_FORWARDED'];
    } else if(isset($_SERVER['REMOTE_ADDR'])) {
        $ipaddress = $_SERVER['REMOTE_ADDR'];
    } else {
        $ipaddress = 'UNKNOWN';
    }
    return $ipaddress;
}

?>
    < HTML >
                <!--Contact Form -->
        <section class="section-block replicable-content contact-2 no-padding-top">
                <div class="row">
                    <div class="column width-8 offset-2 center">
                        <h2 class="mb-30"><strong>Book Your Appointment Now</strong></h2>
                        <div class="contact-form-container">
                            <form class="contact-form" action="" method="post" novalidate>
                                <div class="row">
                                    <div class="column width-6">
                                        <input type="text" name="fname" class="form-fname form-element large" placeholder="First Name*" tabindex="1" required>
                                    </div>
                                    <div class="column width-6">
                                        <input type="text" name="lname" class="form-lname form-element large" placeholder="Last Name*" tabindex="2" required>
                                    </div>
                                    <div class="column width-6">
                                        <input type="email" name="email" class="form-email form-element large" placeholder="Email address*" tabindex="3" required>
                    </div>
                    <div class="column width-6">
    <input type="number" name="number" class="form-number form-element large" placeholder="Phone*" tabindex="4" required>
                                    </div>                                      
                                    <div class="column width-6">
                                        <input type="text" name="Adress" class="form-address form-element large" placeholder="Street Address*" tabindex="5" required>
                                    </div>                                        
                                    <div class="column width-6">
                                        <input type="text" name="City" class="form-city form-element large" placeholder="City*" tabindex="6" required>
                                    </div>                                        
                                    <div class="column width-6">
                                        <input type="text" name="State" class="form-state form-element large" placeholder="State*" tabindex="7" required>
                                    </div>                                       
                                    <div class="column width-6">
                                        <input type="text" name="ZCode" class="form-zcode form-element large" placeholder="Zip Code*" tabindex="8" required>
                                    </div>
                                    <div class="column width-6">
                                            <input type="date" name="date" class="form-date form-element large" placeholder="Date*" tabindex="9" >
                                        </div>  
                                    <div class="column width-6">
                                        <div class="form-select form-element large">
                                            <select name="options" class="form-aux" data-label="Options" tabindex="10" required>
                                                <option>Time Window</option>
                                                <option value="">10AM-12PM</option>
                                                <option value="">12PM - 2PM</option>
                                                <option value="">2PM - 4PM</option>
                                                <option value="">4PM - 6PM</option>
                                            </select>
                                        </div>
                                    </div>

                                    <div class="column width-12">
                                            <input type="junk" name="junk" class="form-junk form-element large" placeholder="Where Is Your Junk Located Ex. (Attic, Backyard,Shed, Front Yard, Inside Home Etc.)*" tabindex="9" required>
                                        </div>
                                    <div class="column width-12">
                                        <div class="form-select form-element large">
                                            <select name="options" class="form-aux" data-label="Options" tabindex="5">
                                                <option selected="selected" value="" >How'd You Find Us</option>
                                                <option value="">From A Friend</option>
                                                <option value="">Google</option>
                                                <option value="">Bing</option>
                                                <option value="">Kudzo</option>
                                                <option value="">Yelp</option>
                                                <option value="">Yahoo</option>
                                                <option value="">Angie's List</option>
                                                <option value="">Other</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="column width-6">
                                        <input type="text" name="honeypot" class="form-honeypot form-element large">
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="column width-12">
                                        <div class="field-wrapper">
                                            <textarea name="message" class="form-message form-element large" placeholder="Briefly Describe what you need removed*" tabindex="7" required></textarea>
                                        </div>
                                    </div>
                                    <div class="column width-12">
                                        <input type="submit" value="Book Now" class="form-submit button medium bkg-theme bkg-hover-theme color-white color-hover-white">
                                    </div>
                                </div>
                            </form>
                            <div class="form-response center"></div>
                        </div>
                    </div>
                </div>
        </section>
<!--Contact Form End -->
  • 2
    Because you never declare those variables?? – Tom Udding Mar 18 '17 at 15:41
  • I never used php before so please excuse my ignorance. How would I go about declaring the variables? – overwhelmed Mar 18 '17 at 15:56
  • I think you have that information in some kind of form a user submits, because you use `$_POST["fname"]` for `$fname` etc. but then you use `$adress`, `$Date` etc. without saying what they need to be (e.g. `$_POST['address']`). – Tom Udding Mar 18 '17 at 16:00
  • ohhhh. So if I go in and change all those to have a $_POST is should work? Ill try it. – overwhelmed Mar 18 '17 at 16:09
  • If they come from a form, yes. You might want to check if they are set, [check this question for more information](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef). – Tom Udding Mar 18 '17 at 16:11
  • if(!isset($_POST["email"]) || !isset($_POST["fname"]) || !isset($_POST["number"]) || !isset($_POST["address"]) || !isset($_POST["city"]) || !isset($_POST["state"]) || !isset($_POST["date"]) || !isset($_POST["zcode"]) || !isset($_POST["message"])) now im getting error message. "MISSING_REQUIRED_FIELDS should not be occurred" – overwhelmed Mar 18 '17 at 16:23
  • That means one (or more) are not set, could you add the HTML form you use to your question (not in the comments)? – Tom Udding Mar 18 '17 at 16:26
  • added to the question. Also if this helps the
    automatically appears on the email without me adding it to the php .
    – overwhelmed Mar 18 '17 at 16:32
  • In the HTML you defined the names of the inputs with certain upper-case characters, in your PHP code you didn't. PHP variables (`$_POST["SoMeThInG"]`) are case-sensitive. – Tom Udding Mar 18 '17 at 16:41
  • Still not working correctly. I'm going to post updated version of the html and php if you still want to help me man. – overwhelmed Mar 18 '17 at 17:11
  • If you could update the code, that would be nice. – Tom Udding Mar 18 '17 at 17:13

2 Answers2

1

It seems the variables are not getting any value from $_POST

So, right below the line // Sanitize input add these lines

$number = filter_var($_POST["number"], FILTER_SANITIZE_STRING);
$adress = filter_var($_POST["Adress"], FILTER_SANITIZE_STRING);
$City = filter_var($_POST["City"], FILTER_SANITIZE_STRING);
$State = filter_var($_POST["State"], FILTER_SANITIZE_STRING);
$Date = filter_var($_POST["date"], FILTER_SANITIZE_STRING);
$Zcode = filter_var($_POST["ZCode"], FILTER_SANITIZE_STRING);
Sharky
  • 6,154
  • 3
  • 39
  • 72
  • 1
    This is what I meant, but couldn't verify without updated code. Have that up vote. – Tom Udding Mar 18 '17 at 17:15
  • Hey. I did exactly as you said. Still nothing, I updated the code if you want to take another look at it. Thanks alot. – overwhelmed Mar 18 '17 at 17:28
  • i believe something is executed prior to this code. i see a "honeypot" dummy field that makes me to believe there is something running prior to the code you posted. I m taking a wild guess and say maybe there is some antispam mechanism that may have a whitelist of allowed fields and the missing variables are not whitelisted hence they got deleted? – Sharky Mar 18 '17 at 18:00
  • If that was the case what should I do? – overwhelmed Mar 18 '17 at 18:16
  • @overwhelmed thats already a very very wild guess, so i cant speculate further. i would suggest setting the `$email_content = print_r($_POST, true);` to see the whole requests content and see if there is any address or zip code in there in the first place. place that line right after the last `$email_content .= ....` – Sharky Mar 18 '17 at 18:34
0

Have you tried contacting support for your theme? They would likely have the info you (and we) would need in terms of how it's supposed to work, what environment is required (i.e. PHP5.6 vs PHP7.1, which modules enabled/disabled), and it would be good to know what environment you're using as well.

  • thank you for replying. No I have not done that yet. I thought I would come here first. I will contact them now because I don't have any of that information. – overwhelmed Mar 18 '17 at 15:58
  • Sure thing, I'm learning PHP myself, so no shame there. I'd still recommend putting in a ticket for support if you haven't already, that way they can work parallel to any help you find here. – Justin Ponce Mar 18 '17 at 18:33