Reputation: 806
I'm trying to make a simple submission form in my index.html file that will send an email with the values, but they are always blank after grabbing them with $_POST. Here is my form:
<form id="contact-form" action="/rtp/php/submit.php" method="POST" enctype="text/plain">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="name">
Name</label>
<input type="text" name="theName" class="form-control" id="theName" placeholder="Enter name" required="required" />
</div>
<div class="form-group">
<label for="email">
Email Address</label>
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-envelope"></span>
</span>
<input type="email" name="theEmail" class="form-control" id="theEmail" placeholder="Enter email" required="required" /></div>
</div>
<div class="form-group">
<label for="subject">
Subject</label>
<select id="subject" name="theSubject" id="theSubject" class="form-control" required="required">
<option value="na" selected="">Choose One:</option>
<option value="service">Criminal Defense</option>
<option value="service">Personal Injury</option>
<option value="service">Consitutional Law</option>
<option value="service">Immigration</option>
<option value="suggestion">General Inquiry</option>
<option value="suggestion">Scholarship</option>
</select>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="name">
Message</label>
<textarea name="message" name="theMessage" id="theMessage" class="form-control" rows="9" cols="25" required="required"
placeholder="What can we help you with?"></textarea>
</div>
</div>
<div class="col-md-12">
<button type="submit" class="btn btn-skin pull-right" id="btnContactUs">
Send Message</button>
</div>
</div>
</form>
And here is my submission script:
<?php
ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);
// Set destination
$email_to = "bali*****l.com";
$email_from = "br******offee";
$email_subject = "Rai*******rship";
// Grab fields
$name = isset($_POST["theName"]) ? $_POST['theName'] : 'Name Not set';
$email = isset($_POST['theEmail']) ? $_POST['theEmail'] : 'Email not set';
$subject = isset($_POST['theSubject']) ? $_POST['theSubject'] : 'subject not set';
$message = isset($_POST['theMessage']) ? $_POST['theMessage'] : 'message not set:(';
$email_message = "Form details below.\n\n";
// Clean up
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
// Set string
$email_message .= $name."\n";
$email_message .= $email."\n";
$email_message .= $subject."\n";
$email_message .= $message."\n";
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
print("The message -> $email_message");
?>
Congratulations, you are entered to win! You should hear from us shortly :)
All variables end up blank. What am I doing wrong? Any help is appreciated, I've been trying to figure this out for hours. I do not understand why the POST function is not grabbing the values and passing them through.
Upvotes: 2
Views: 4998
Reputation: 497
I agree that you can just remove the enctype tag altogether -- I've never needed to include that on a form sent via Post.
As an additional aside, though:
Do you mean that the variables are blank if the web form is left blank?
When a form is submitted via POST and a field is empty, it is sent as an empty string.
So, for example, if you left the entire form blank then $_POST['theName'] would be sent as "".
ISSET will return true on a blank string -- so, all of your ISSET tests are returning true.
Try !empty instead. For example:
$name = !empty($_POST['theName']) ? $_POST['theName'] : 'No Name Set';
If it is returning blanks even when something is entered on the form please clarify.
Also, in your <select><option>
tags, POST is going to send the value of whatever is selected. You currently have multiple Options with the same value. It does NOT send what is between the <option
> tag (in other words, whether they choose "Criminal Defense" or "Personal Injury" $_POST['theSubject'] will be "service" for both.
Upvotes: 1