简体   繁体   中英

form input to javascript to php

I don't understand what the problem is, I'm left confused. I assume the problem might be very simple as I am new to PHP.

My form:

<form name="sentMessage" id="contactForm" novalidate>
                        <div class="row control-group">
                        <label>Name</label>
                            <div class="form-group col-xs-12 floating-label-form-group controls">
                                <input type="text" class="form-control" placeholder="Name" id="name" required data-validation-required-message="Please enter your name.">
                                <p class="help-block text-danger"></p>
                            </div>
                        </div>
                        <!-- Email Address -->
                        <div class="row control-group">
                        <label>Email Address</label>
                            <div class="form-group col-xs-12 floating-label-form-group controls">
                                <input type="email" class="form-control" placeholder="Email Address" id="email" required data-validation-required-message="Please enter your email address.">
                                <p class="help-block text-danger"></p>
                            </div>
                        </div>
                        <!-- Home Location -->
                        <div class="row control-group">
                        <label>Home Location</label>
                            <div class="form-group col-xs-12 floating-label-form-group controls">
                                <input type="text" class="form-control" placeholder="Home Location" id="homelocation" required data-validation-required-message="Please enter your home location.">
                                <p class="help-block text-danger"></p>
                            </div>
                        </div>
                        <!-- Phone Number -->
                        <div class="row control-group">
                        <label>Phone Number</label>
                            <div class="form-group col-xs-12 floating-label-form-group controls">
                                <input type="tel" class="form-control" placeholder="Phone Number" id="phone" required data-validation-required-message="Please enter your phone number.">
                                <p class="help-block text-danger"></p>
                            </div>
                        </div>
                         <div class="row control-group">
                        <label>How did you hear about us?</label><br>
                        <div class="form-group col-xs-12 floating-label-form-group controls">
                            <select name="advert" <!-- To configure -->>
                              <option value="google">Google Search</option>
                              <option value="trademe">Trademe</option>
                            </select>
                            <p class="help-block text-danger"></p>
                        </div>
                    </div>

JS:

submitSuccess: function($form, event) {
    // Prevent spam click and default submit behaviour
    $("#btnSubmit").attr("disabled", true);
    event.preventDefault();

    // get values from FORM
    var name = $("input#name").val();
    var email = $("input#email").val();

    var phone = $("input#phone").val();
    var homelocation = $("input#homelocation").val();
    ....
    $.ajax({
            url: "././mail/contact_me.php",
            type: "POST",
            data: {
                name: name,
                phone: phone,
                email: email,
                homelocation: homelocation,
                .........

and my php:

<?php
// Check for empty fields
if(empty($_POST['name'])        ||
   empty($_POST['email'])       ||
   empty($_POST['phone'])       ||
   empty($_POST['homelocation'])    ||
   !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
   {
    echo "No arguments Provided!";
    return false;
   }

$name = strip_tags(htmlspecialchars($_POST['name']));
$email_address = strip_tags(htmlspecialchars($_POST['email']));
$phone = strip_tags(htmlspecialchars($_POST['phone']));
$homelocation = strip_tags(htmlspecialchars($_POST['homelocation']));
..............

The code works perfectly fine if I comment out homelocation , I have no idea why.

I am new to PHP so I am a bit confused as to why the above code doesn't work, but if homelocation is commented out, then it works. The name and homelocation have the same input type, so I don't think data type has anything to do with it, and I am sure the variable names are used correctly, could someone please point me to the right direction on this one.

Thanks!

You have problem with your html tags. You can check for errors in html document here . Just fix those errors and it will work just fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM