0

For some odd reason, the business input value won't submit to the email... it's always blank.. can someone tell me what's wrong? Thank you!

<?php
// Check for empty fields
if(isset(empty($_POST['submit'])      ||
   empty($_POST['email'])     ||
   empty($_POST['phone'])     ||
   empty($_POST['business'])  ||
   empty($_POST['message'])   ||
   !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']));
$business = strip_tags(htmlspecialchars($_POST['business']));
$message = strip_tags(htmlspecialchars($_POST['message']));

// Create the email and send the message
$to = 'somekoreanguy@gmail.com'; // Add your email address inbetween the '' replacing yourname@yourdomain.com - This is where the form will send a message to.
$email_subject = "Website Contact Form:  $name";
$email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $phone\n\nBusiness: $business\n\nMessage:\n$message";
$headers = "From: noreply@yourdomain.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
$headers .= "Reply-To: $email_address";   
mail($to,$email_subject,$email_body,$headers);
return true;         
?>

And here's my HTML:

<form name="sentMessage" id="contactForm" novalidate method="post">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <input type="text" class="form-control" placeholder="Your 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="email" class="form-control" placeholder="Your 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="tel" class="form-control" placeholder="Your 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="text" class="form-control" placeholder="Your Business Name *" id="business" required data-validation-required-message="Please enter your business name.">
                                <p class="help-block text-danger"></p>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <textarea class="form-control" placeholder="Your Message *" id="message" required data-validation-required-message="Please enter a 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" name="submit" class="btn btn-xl">Send Message</button>
                        </div>
                    </div>
                </form>

For some reason, when I put that "empty($_POST['business']) ||" in there, nothing comes in to the email. When I remove "type="text" from my business html section the email goes through but nothing as a value. it'll just say Business: and blank....

Jason Han
  • 23
  • 1
  • 8

2 Answers2

0

you did not define method="post"

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

so it will be like

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

give name for

<button type="submit" name ="submit" class="btn btn-xl">Send Message</button>

So your code will be

if(isset($_POST['submit']){
//set your code here
}
Putra L Zendrato
  • 332
  • 3
  • 12
  • After doing what you posted it shows me a message saying that the mail service is not responding, please try later when I click the submit button. – Jason Han Apr 06 '17 at 03:07
  • I've updated the PHP and HTML code now to your changes. – Jason Han Apr 06 '17 at 03:11
  • The submit code is working now, the another problem is in your PHP mail code, I recommend you can use email plugin to handle your mail services PHP mailer - https://github.com/PHPMailer/PHPMailer – Putra L Zendrato Apr 06 '17 at 03:24
  • I'm still learning PHP and when I see a plugin, it doesn't process fully to me yet so when I learn a bit more I'll look into the plugins. Thank you. – Jason Han Apr 07 '17 at 05:25
0

It's always blank, because no name="" in your input form, name="" to reference form data after a form is submitted. Add name="" to your input like this..

<?php
// Check for empty fields
if(isset($_POST['submit'])) {
if(trim($_POST['email'])== ''  || trim($_POST['phone'])== ''   || trim($_POST['business'])== ''  || trim($_POST['message'])== ''  || !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!";
}else{
$name = strip_tags(htmlspecialchars($_POST['name']));
$email_address = strip_tags(htmlspecialchars($_POST['email']));
$phone = strip_tags(htmlspecialchars($_POST['phone']));
$business = strip_tags(htmlspecialchars($_POST['business']));
$message = strip_tags(htmlspecialchars($_POST['message']));

// Create the email and send the message
$to = 'somekoreanguy@gmail.com'; // Add your email address inbetween the '' replacing yourname@yourdomain.com - This is where the form will send a message to.
$email_subject = "Website Contact Form:  $name";
$email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $phone\n\nBusiness: $business\n\nMessage:\n$message";
$headers = "From: noreply@yourdomain.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
$headers .= "Reply-To: $email_address";   
mail($to,$email_subject,$email_body,$headers);
}
}
?>
<html>
<head>
</head>     
<body>
    <form name="contactForm" novalidate method="POST">
    <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                <input type="text" class="form-control" placeholder="Your Name *" name="name" id="name" data-validation-required-message="Please enter your name." required>
                <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" data-validation-required-message="Please enter your email address." required>
                <p class="help-block text-danger"></p>
            </div>
            <div class="form-group">
                <input type="tel" class="form-control" placeholder="Your Phone *" name="phone" id="phone" data-validation-required-message="Please enter your phone number." required>
                <p class="help-block text-danger"></p>
            </div>
            <div class="form-group">
                <input type="text" class="form-control" placeholder="Your Business Name *" name="business" id="business" data-validation-required-message="Please enter your business name." required>
                <p class="help-block text-danger"></p>
            </div>
            <div class="col-md-6">
                <div class="form-group">
                    <textarea class="form-control" placeholder="Your Message *" name="message" id="message" data-validation-required-message="Please enter a message." required></textarea>
                    <p class="help-block text-danger"></p>
                </div>
            </div>
        <div class="clearfix"></div>
        <div class="col-lg-12 text-center">
            <div name="success"></div>
            <button type="submit" name ="submit" class="btn btn-xl">Send Message</button>
    </form>
</body>
</html>
Abed Putra
  • 950
  • 1
  • 15
  • 32