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.
remove enctype="text/plain" from form tag and check.
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.
Related
I have a form and the form is working: I receive all the details on my e-mail. But when I click submit, it redirects to a blank page with the text: "Request submitted successfully. We will contact with you very soon.".
What I would like to do is to show a green box saying that the form was submitted successfully, on the same page like most websites do, and not a redirect...
I am using Bootstrap :)
Mailer.php code:
<?php
$tipoIn = $_POST['tipoIn'];
$Tipologia_input = $_POST['Tipologia_input'];
$sender_name = $_POST['Nome'];
$sender_email = $_POST['email'];
$phone = $_POST['Telefone'];
$slider_value = $_POST['slider_value'];
$mail_body = $_POST['message'];
$body = $sender_name." sent a new message for you<br><br> Name: ".$sender_name."<br>Email: ".$sender_email."<br>Phone: ".$phone."<br>Tipo: ".$tipoIn."<br>Tipologia: ".$Tipologia_input."<br>Slider Value: ".$slider_value."<br>Message: ".$mail_body;
sendMail($sender_name , $sender_email, $body);
function sendMail($sender, $sender_mail, $body) {
$to = 'geral#porta10.com'; // Set Receiver Email Here
$myemail = 'geral#porta10.comm'; // Set Sender Email Here
$subject = "New EasyLoan Client"; // Set Subject Here
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: Lead <geral#porta10.com>\r\n"; // Set Header Here
$message = $body;
$sentmail = mail($to,$subject,$message,$headers);
if($sentmail) { echo "Request submitted successfully. We will contact with you very soon."; }
else { echo "Mail not sent"; }
}
?>
Form code:
<div class="form-container">
<div class="form-mockup">
<h2>Que imóvel procura ?</h2>
<!-- <h4>Easy to apply for a loan with us,Once you have complete this form. </h4>-->
<form action="mailer.php" method="post">
<div class="form-group">
<select class="form-control" id="tipo" name="tipoIn">
<option value="Tipo" disabled selected>Tipo de imóvel</option>
<option value="Moradia">Moradia</option>
<option value="Apartamento">Apartamento</option>
</select>
</div>
<div class="form-group">
<select class="form-control" id="tipologia" name="Tipologia_input">
<option value="Tipologia" disabled selected>Tipologia</option>
<option value="T1">T1</option>
<option value="T2">T2</option>
<option value="T3">T3</option>
<option value="T4">T4</option>
<option value="T5">T5</option>
</select>
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="Nome Completo" name="Nome">
</div>
<div class="form-group">
<input type="email" class="form-control" placeholder="E-mail" name="email">
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="Telefone" name="Telefone">
</div>
<div class="form-group">
<textarea class="form-control" rows="3" id="comentario" placeholder="Diga-nos como imagina a sua casa..." name="message"></textarea>
</div>
<div class="form-group">
<div class="button-slider">
<div class="btn-group btn_group">
<div class="btn btn-default btn_amount">Valor </div>
<div class="btn btn-default btn_slider">
<input id="bootstrap-slider" type="text" data-slider-min="1" data-slider-max="50000" data-slider-step="1" data-slider-value="25000" name="slider_value" />
<div class="valueLabel"><span class="text_span">Valor </span> <span id="sliderValue">25000</span>€</div>
</div>
</div>
</div>
</div>
<button type="submit" class="btn btn-default quote_btn">Enviar</button>
</form>
</div>
</div>
You can do various things, it depends on the framework or cms or the logic of your code. there are many ways to accomplish this task (showing on (same/existing) page and not redirecting to (blank/defaced)) but its still depends.
2 main ways i currently can imagine are:
Using Ajax and json for messaging between Server and client and
prevent reloading
Using session-keys to show up messages on any page user loads
afterwards.
I'm currently working through setting up a fully functional contact form using the Bootstrap framework and PHP. I'm receiving test emails from the form but it's not displaying the content the way I understood it would and I'm having some other issues.
First, the user-submitted email is showing up as being received from the host of my site. It is also not including the name of the person, which I thought I included in my PHP
Here is my code for contact.php and the form in my index.html file
<?php
$to = 'example#gmail.com';
$name = $_POST['name'];
$email = $_POST['email'];
$subject=$_POST['subject'];
$message = $_POST['msg'];
if ($_POST){
mail($to, $subject, $message);
$confirm= "Your email was sent successfully!";
echo "<script type='text/javascript'>confirm('$confirm');</script>";
}
else {
$error_msg= "All fields are required!";
echo "<script type='text/javascript'>alert('$error_msg');</script>";
}
?>
index.html
<form class="form-horizontal" role="form" action="contact.php" method="post">
<div class="modal-header">
<h2>Contact</h2>
</div>
<div class="modal-body">
<!--name-->
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="name" id="name" placeholder="First and Last Name">
</div>
</div>
<!--email-->
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" name="email" id="email" placeholder="example#somewhere.com">
</div>
</div>
<!--subject-->
<div class="form-group">
<label for="subject" class="col-sm-2 control-label">Subject</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="subject" id="subject" placeholder="Enter subject here...">
</div>
</div>
<!--message-->
<div class="form-group">
<label for="msg" class="col-sm-2 control-label">Message</label>
<div class="col-sm-10">
<textarea class="form-control" rows="4" name="msg" id="msg" placeholder="Enter message here..."></textarea>
</div>
</div>
</div>
<div class="modal-footer">
<a class="btn btn-default" data-dismiss="modal">Close</a>
<button type="submit" class="btn btn-primary">Send</button>
</div>
</form>
Another unintended issue is that once the page is submitted, a blank page (contact.php) is loaded. I thought, looking at my code, a javascript alert or confirm box would pop up instead though? I'm new at this so, I apologize if these are dumb questions.
Thanks
-K
First of, you don't have from defined in your header. You need
$headers = "From: sender_email#your_domain.com\r\n";
mail($to,$subject,$message,$headers);
You can also modify header to send html message which is desired in most cases in addition to setting from and cc: carbon copy or bcc: blind carbon copy email addresses.
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: <webmaster#example.com>' . "\r\n";
$headers .= 'Cc: myboss#example.com' . "\r\n";
Get this code incorporated in your if statement. I also noticed you have
else {
$error_msg= "All fields are required!";
Even if there was only one field passed in as $_POST method, the else statement isn't checking if all fields were passed in. You need to break that down or check the fields individually or do it on with client side JavaScript field validation.
What is the confirm javascript function? can you post it on here? if you're only looking for an alert, you can straight insert alert function in between script tags.
I have a problem with checking if a email is valid. but the weird is that i have the same form on to different pages/urls, and on one of the forms it keeps saying that the email is invalid and on the form its valid.
The form on this page works - http://night.sendme.to/about
the form on this page doesnt - http://night.sendme.to/book/jokeren
The HTML on the forms is the same
<form action="" method="post" id="myform">
<div class="form-group">
<label for="name">Navn *</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Navn" required="required">
</div>
<div class="form-group">
<label for="corp">Virksomhed</label>
<input type="text" class="form-control" id="corp" name="corp" placeholder="Virksomhed">
</div>
<div class="form-group">
<label for="email">Email adresse *</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Email adresse" required="required">
</div>
<div class="form-group">
<label for="tel">Telefon *</label>
<input type="tel" class="form-control" id="tel" name="tel" placeholder="Telefon" required="required">
</div>
<div class="form-group">
<label for="message">Kommentar</label>
<textarea class="form-control" id="message" name="message" rows="10" required="required"></textarea>
</div>
<button type="submit" class="btn btn-default" id="submit">Send</button>
</form>
<div id="success" style="color:red;"></div>
The PHP is this
<?php // Here we get all the information from the fields sent over by the form.
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = 'YOURMAIL';
$subject = 'the subject';
$message = 'FROM: '.$name.' Email: '.$email.'Message: '.$message;
$header = "MIME-Version: 1.0" . "\r\n";
$header .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$header .= "from:".$_POST['email'];
if (filter_var($email, FILTER_VALIDATE_EMAIL)) { // this line checks that we have a valid email address
mail($to, $subject, $message, $header); //This method sends the mail.
echo "Your email was sent!";
echo var_dump(filter_var($email, FILTER_VALIDATE_EMAIL));
} else {
echo "Invalid Email, please provide an correct email.";
echo var_dump(filter_var($email, FILTER_VALIDATE_EMAIL));
}
?>
The javascript is this
$(document).ready(function(){
$('#submit').click(function(){
$.post("email.php", $("#myform").serialize(), function(response) {
$('#success').html(response);
//$('#success').hide('slow');
});
return false;
});
});
Hope some one can help, why the form only works on the http://night.sendme.to/about and the others
So, to not leave this question answer-less:
In your HTML code, you actually had <form action="" method="post" id="myform"> in both pages – but in your second page, you had another <form> tag right before it … and because of this invalid HTML, the browser ignored the second form tag, and that made $("#myform").serialize() not return any data at all, because it could not find the form element with that id.
You should always validate your HTML code. This helps avoiding such errors.
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 8 years ago.
I can't seem to get the form to send a message to my email. Is it maybe due to the server? Here's my code:
<?php
$action=$_REQUEST['action'];
if ($action=="") /* display the contact form */
{
?>
<form id="contact-form" action:"" method="post" enctype="multipart/form-data">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input type="hidden" name="action" value="submit">
<label for="name">
Name</label>
<input type="text" class="form-control" id="name" placeholder="Enter name" 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" class="form-control" id="email" placeholder="Enter email" required /></div>
</div>
<div class="form-group">
<label for="subject">
Subject</label>
<select id="subject" name="subject" class="form-control" required="required">
<option value="na" selected="">Choose One:</option>
<option value="General">General</option>
<option value="Hiring">Hiring</option>
<option value="My Work">My Work</option>
</select>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="message">
Message</label>
<textarea name="message" id="message" class="form-control" rows="9" cols="25" required
placeholder="Message"></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>
<?php
}
else /* send the submitted data */
{
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$subject=$_REQUEST['subject'];
$message=$_REQUEST['message'];
if (($name=="")||($email=="")||($subject=="")||($message==""))
{
echo "All fields are required.";
}
else{
$from="From: $name, $email";
$subject="Message sent using your contact form";
mail("nilsbittmannmyers#yahoo.co.uk", $subject, $message, $from);
echo "Message sent!";
}
}
?>
THANKS :)
Im using byet as a web host by the way, I think they have PHP enabled in the free service, excuse me, I'm a bit of a noob when it comes to coding, to say the least XD XD
You're using a colon action:"" instead of an equal sign which should be action="" which is the main issue in your code.
You're also missing a name attribute for the email and name input fields.
I.e.: name="email" and name="name"
Also, the From: should initially be an email, and not a name as you have in
$from="From: $name, $email";
It's best to use something like this:
$from = "From: ". $name . " <" . $email . ">\r\n";
as your header's 4th parameter.
That way you will have a proper From Email with the person's name appearing in the Email also.
Using $from="From: $name, $email"; will most likely end up in Spam or rejected altogether.
Consult the PHP.net Website for details on mail() and headers:
http://php.net/manual/en/function.mail.php
a few headers options:
$headers .= 'Reply-To: ' . $from . "\r\n";
$headers .= 'Return-Path: ' . $from . "\r\n";
in your case would be:
$from = "From: ". $name . " <" . $email . ">\r\n";
$from .= 'Reply-To: ' . $email . "\r\n";
$from .= 'Return-Path: ' . $email . "\r\n";
Sidenote:
I noticed you are using enctype="multipart/form-data"
Unless you're wanting to attach/upload a file, it isn't required for what you're using your form as, it's safe to remove it.
Footnotes:
Using error reporting would have signaled these errors, including Undefined index... warnings:
error_reporting(E_ALL);
ini_set('display_errors', 1);
placed just beneath your opening <?php tag.
I have created a simple html based website hosted on appharbor and wants to add a form that visitors can completed and then send the information by email using php.
I have gathered some php scripts for sending the mail and have also created the form, when I test the page on my computer (without php installed) I can enter information and press "Send" and then I'm transfered to the php page which I feel seems correct.
The problem is when I deploy this at appharbor, I can enter the information in the form but I when I press the button to send the mail I either get a 404 error stating that the page is missing or I get the information that the HTML verb is unsupported.
Do I need to create some web.config file even if it otherwise is a static html site?
Do I need to activate something on my site or at appharbor?
--- Update ----
<form class="form-horizontal" action="test.php" method="POST">
<fieldset>
<div id="legend">
<legend class="">Request</legend>
</div>
<div class="control-group">
<label class="control-label" for="email">Email</label>
<div class="controls">
<input type="text" id="email" email="email" placeholder="" class="input-xlarge">
</div>
</div>
<div class="control-group">
<label class="control-label" for="name">Your name</label>
<div class="controls">
<input type="text" id="name" name="name" placeholder="" class="input-xlarge">
</div>
</div>
<div class="control-group">
<div class="controls">
<button class="btn btn-success">Skicka</button>
</div>
</div>
</fieldset>
</form>
php script file
<?php
if(isset($_REQUEST['email'])) {
$email_to = "something#mail.ma";
$email_subject = "Message from homepage";
function died($error) {
echo "Error occured please correct. ";
echo $error."<br /><br />";
die();
}
if(!isset($_REQUEST['name']) ||
!isset($_REQUEST['email'])) {
died('Something was wrong, please correct.');
}
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$email_message = "Message.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email)."\n";
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
Thanks for the request
<?php
}
?>
I've removed some of the form as well as some error testing in the script that I normally have.