简体   繁体   中英

Not receiving input from all fields from PHP contact form

This has been bugging me all day and I've given up trying to figure it out for myself, but the answer's probably really obvious...

My contact form works fine, however I don't receive input for all fields. I only get name, phone, email and message. I've given the form fields a name attribute so I'm not sure what's happening.

Here is the HTML:

 <form name="sentMessage" id="contactForm" novalidate> <div class="row"> <div class="col-md-6"> <div class="form-group"> <input type="name" class="form-control" placeholder="Your Name *" name="name" id="name" required data-validation-required-message="Please enter your name."> <p class="help-block text-danger"></p> </div> <div class="form-group"> <input type="phone" class="form-control" placeholder="Your Phone Number *" name="phone" id="phone" required data-validation-required-message="Please enter your phone number."> <p class="help-block text-danger"></p> </div> <div class="form-group"> <input type="email" class="form-control" placeholder="Your Email *" name="email" id="email" required data-validation-required-message="Please enter your email address."> <p class="help-block text-danger"></p> </div> <div class="form-group"> <input type="text" class="form-control" name="suburb" placeholder="Your Suburb *" id="suburb" required data-validation-required-message="Please enter your address."> <p class="help-block text-danger"></p> </div> <div class="select"> <div class="col-sm-6"> <div class="form-group"> <select class="selectpicker" name="state" id="state" data-width="100%" data-height="100%" title="Choose State *" data-style="btn-primary"> <option>QLD</option> <option>NSW</option> </select> </div> </div> </div> <div class="right-form-column"> <div class="col-sm-6"> <div class="form-group"> <input type="number" class="form-control" placeholder="Post Code *" name="postcode" id="postcode" required data-validation-required-message="Please enter your post code."> <p class="help-block text-danger"></p> </div> </div> </div> </div> <div class="col-md-6"> <div class="select"> <div class="col-xs-6"> <div class="form-group"> <select class="selectpicker" name="product1" id="product1" data-width="100%" data-height="100%" data-live-search="true" title="Choose Product" data-style="btn-primary"> <optgroup label="100 Series"> <option>90mm Grey Standard</option> </optgroup> </select> </div> </div> </div> <div class="right-form-column"> <div class="col-xs-6"> <div class="form-group"> <input type="number" class="form-control" placeholder="Quantity" name="quantity1" id="quantity1"> <p class="help-block text-danger"></p> </div> </div> </div> <div class="select"> <div class="col-xs-6"> <div class="form-group"> <select class="selectpicker" name="product2" id="product2" data-width="100%" data-height="100%" data-live-search="true" title="Choose Product" data-style="btn-primary"> <optgroup label="100 Series"> <option>90mm Grey Standard</option> </optgroup> </select> </div> </div> </div> <div class="right-form-column"> <div class="col-xs-6"> <div class="form-group"> <input type="number" class="form-control" placeholder="Quantity" name="quantity2" id="quantity2"> <p class="help-block text-danger"></p> </div> </div> </div> <div class="select"> <div class="col-xs-6"> <div class="form-group"> <select class="selectpicker" name="product3" id="product3" data-width="100%" data-height="100%" data-live-search="true" title="Choose Product" data-style="btn-primary"> <optgroup label="100 Series"> <option>90mm Grey Standard</option> </optgroup> </select> </div> </div> </div> <div class="right-form-column"> <div class="col-xs-6"> <div class="form-group"> <input type="number" class="form-control" placeholder="Quantity" name="quantity3" id="quantity3"> <p class="help-block text-danger"></p> </div> </div> </div> <div class="form-group"> <textarea class="form-control" placeholder="Additional comments" name="message" id="message"></textarea> <p class="help-block text-danger"></p> </div> </div> <div class="clearfix"></div> <div class="col-lg-12 text-center"> <div id="success"></div> <button type="submit" class="btn btn-xl" name="submit">submit</button> </div> </div> </form>

And the PHP:

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

$name = $_POST['name'];
$phone = $_POST['phone'];
$email_address = $_POST['email'];
$message = $_POST['message'];

// create email body and send it    
$to = 'myname@mydomain.com'; // PUT YOUR EMAIL ADDRESS HERE
$email_subject = "P Services Contact Form:  $name"; // EDIT THE EMAIL SUBJECT LINE HERE
$email_body = "You have received a new message from your website's contact form.\n\n"."Here are the details: From: $name\n Phone: $phone\n E-Mail: $email_address\n Suburb: $suburb\n State: $state\n Post Code: $postcode\n Product: $product1\n Quantity: $quantity1\n Product: $product2\n Quantity: $quantity2\n Product: $product3\n Quantity: $quantity3\n Message:\n $message";
$headers = "From: noreply@yourdomain.com\n";
$headers .= "Reply-To: $email_address"; 
if(mail($to,$email_subject,$email_body,$headers)){ 
    echo "Mail sent successfully.";
} 
    else{ echo "Error.";
}        
?>

make sure that, you've removed disabled tag in html elemets.

THIS IS WRONG. PHP WON'T RECOGONIZE YOUR ELEMENT.

<input type="text" disabled>

INSTED OF USING disabled you can use readonly . THIS IS THE CORRECT METHORD

<input type="text" readonly>

The only thing you need to change is specify the method of form submission ( POST or GET ).

Either use

<form name="sentMessage" id="contactForm" novalidate method="POST">

OR

Access the form variables using $_GET like $_GET['name'])

Your code returns all the information for me

在此处输入图片说明

All i did was change the form method so all the data is being passed its just needs to be handled correctly in your PHP

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