I have an html form where users will submit their contact information for us to get in contact with them. I would like to have all of this live on index.html, but I am not opposed to having to create another file in the folder.
<form class="row" method="post" action="contact.php">
<div class="col-md-12">
<div class="form-group">
<input type="text" class="form-control" placeholder="Your Full Name">
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="Phone Number*">
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="Email Address*">
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="Company Name*">
</div>
<div class="form-group">
<textarea class="form-control lg_textarea" placeholder="Questions / comments"></textarea>
</div>
</div>
<div class="buttn_outer">
<button type="submit" class="btn signin_btn col-sm-5 clearfix" style="margin:0 0 0 4%; float: left;">Submit</button>
</div>
</form>
I have had php installed on the server machine. Looking at other examples I have found attempted using a php script as follows.
<?php
if($_POST["submit"]) {
$recipient = "me#me.com";
$subject = "Some Message subject";
$sender = $_POST["sender"];
$senderEmail = $_POST["senderEmail"];
$message = $_POST["message"];
$mailBody = "Name: ".$sender."\nEmail: ".$senderEmail."\n\n".$message;
mail($recipient, $subject, $mailBody, "From: ".$sender." <".$senderemail.">");
$thankYou = "<p>Thank you! Your message has been sent.</p>";
}
?>
As of right now it goes to the success message however I never get anything to go to my email.
I am extremely new to php, any help/advice is appreciated.
None of your input fields have name attributes.
<input type="text" class="form-control" placeholder="Email Address*">
<input type="text" class="form-control" placeholder="Your Full Name">
<textarea class="form-control lg_textarea" placeholder="Questions / comments"></textarea>
Should be
<input type="text" class="form-control" name="senderEmail" placeholder="Email Address*">
<input type="text" class="form-control" name="sender" placeholder="Your Full Name">
<textarea class="form-control lg_textarea" name="message" placeholder="Questions / comments"></textarea>
Basically, the name attribute of the input field should match what the $_POST key is.
So, <input name="name" /> would be handled as $_POST['name']
Also, it looks like you're mixing up the $recipient and $senderEmail variables. PHP's mail function specifies that the first parameter is the recipient. I understand that me#me.com is clearly not a real email. But, the recipient should be the user's email that he/she has submitted in the form. Make sure the mail function works by testing it without submitting the form and if it does, then try to make it work with the form variables.
Try This May Help You...
index.html
<form class="row" method="post" action="contact.php">
<div class="col-md-12">
<div class="form-group">
<input type="text" class="form-control" name="sender" placeholder="Your Full Name">
</div>
<div class="form-group">
<input type="text" class="form-control" name="phno" placeholder="Phone Number*">
</div>
<div class="form-group">
<input type="text" class="form-control" name="email" placeholder="Email Address*">
</div>
<div class="form-group">
<input type="text" class="form-control" name="cmp" placeholder="Company Name*">
</div>
<div class="form-group">
<textarea class="form-control lg_textarea" name="cmt" placeholder="Questions / comments"></textarea>
</div>
</div>
<div class="buttn_outer">
<button type="submit" class="btn signin_btn col-sm-5 clearfix" style="margin:0 0 0 4%; float: left";>Submit</button>
</div>
</form>
contact.php
<?php
$to='me#me.com';
$header = "From: info#".$_SERVER["SERVER_NAME"]."\n";
$header .= "Content-Type: text/html; charset=iso-8859-1\n";
$sender=$_POST['sender'];
$email=$_POST['email'];
$cmt=$_POST['cmt'];
$cmp=$_POST['cmp'];
$phno=$_POST['phno'];
$subject='Message Subject';
$body='<table width="90%" border="0">
<tr>
<td><b>Sender:</b></td> <td>'.$sender.'</td>
</tr>
<tr>
<td><b>Email:</b></td> <td>'.$email.'</td>
</tr>
<tr>
<td><b>Phone No:</b></td> <td>'.$phno.'</td>
</tr>
<tr>
<td><b>Copmany Name:</b></td> <td>'.$cmp.'</td>
</tr>
<tr>
<td><b>Comment:</b></td> <td>'.$cmt.'</td>
</tr>
<tr></table>';
mail($to,$subject,$body,$header);
header('location:index.html');
?>
Other things, I'd throw in
($_POST["submit"])
should be changed to
!empty($_POST["submit"]) - or - isset($_POST["submit"])
Never assume a variable is set - don't run any tests on it or use it's value until you check to see if it's been set (or you initialize it yourself).
Depending on your PHP config, that line can fail. So if you move your contact.php page to another server, and then it may crash with errors.
As Lance mentioned, every form field that you want to pass back to your server needs a name attribute. This is what links it to the $_POST array when it gets passed to the server.
Also, you can use the users email as the from address. However, your email will likely get flagged as spam when the receiving server sees that it's not originated from that user's domain email server. You'll have to play around with your server's email settings so you authenticate it as a legitimate from address if you're trying to "spoof" it (aka not use the server's default).
If you want to override the default from address (or reply to address), you need to do it in the header fields (you can google this one to see you options).
If you want to include HTML tags or elements in your HTML, you'll also need to indicate this in the headers parameter of the mail function. For simply alert emails, like what you're doing - I just use plain text and use carriage returns (like you have with \n's) and don't use items like <p></p>.
Related
I have a basic HTML/PHP web form. Everything works fine when I fill out all fields, except I want to make the file upload optional, not required. I have taken "required" off of the form html for the upload. I checked my handler.php and it does not say that field is required. I checked the formhandler.php and it says that attachments are null. I have looked through similar questions, but any of the more complex solutions I am probably implementing incorrectly. I've expended my knowledge of php looking for a solution. What am I missing?
Here is the html of the form:
<div class="row pb6">
<div class="col-md-6 offset-md-3">
<form role="form" method="post" id="reused_form" enctype="multipart/form-data" >
<div class="form-group">
<label for="name"> Name:</label>
<input type="text" class="form-control" id="name" name="name" required maxlength="50">
</div>
<div class="form-group">
<label for="email"> Email:</label>
<input type="email" class="form-control" id="email" name="email" required maxlength="50">
</div>
<div class="form-group">
<label for="tel"> Phone: </label>
<input type="tel" class="form-control" id="tel" name="tel" maxlength="50">
</div>
<div class="form-group">
<label for="name"> Message:</label>
<textarea class="form-control" type="textarea" name="message" id="message" placeholder="We want to hear all about your performance! Also, please include the date you need the music." maxlength="6000" rows="7"></textarea>
</div>
<div class="form-group">
<label for="file">File Upload (optional)</label>
<input type="file" class="form-control" id="image" name="image">
</div>
<button type="submit" class="btn btn-lg btn-success pull-right" id="btnContactUs">Post It! →</button>
</form>
<div id="success_message" style="width:100%; height:100%; display:none; "> <h3>Sent your message successfully!</h3> </div>
<div id="error_message" style="width:100%; height:100%; display:none; "><h3>Error</h3>We are very sorry. There was an error sending your form. Email us and we will send you a free demo.</div>
</div>
</div>
</div>
And here is the handler.php:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
/*
Tested working with PHP5.4 and above (including PHP 7 )
*/
require_once './vendor/autoload.php';
use FormGuide\Handlx\FormHandler;
$pp = new FormHandler();
$validator = $pp->getValidator();
$validator->fields(['name','email','tel'])->areRequired()->maxLength(50);
$validator->field('email')->isEmail();
$validator->field('message')->maxLength(6000);
if($_FILES['image']){ $pp->attachFiles(['image']); }
$pp->sendEmailTo('email'); //
echo $pp->process($_POST);
after code edit:
1)Chrome and Firefox will send form, Safari will not.
2)All browsers show a perpetual "Sending" message, and do not show a success or failure message for the form.
3) Forms send WITH attachments will not send the form with attachment to email, only the form alone.
You are trying to attach the image whether it exists or not with $pp->attachFiles(['image']);. You should first check if it does indeed exists and only attach it if it does like so:
if($_FILES['image']){
$pp->attachFiles(['image']);
}
This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I have designed a form in html and I'm using php to send it to my email. The form validates OK and everything is fine apart from the fact that it doesn't send some information. Here is how it comes to my email:
Name:
Email:
Tel.:
Message: test
So when I fill it in the only field that goes through is the "Message" field. The other ones are blank although the form validates and process. I guess there is something in php code. I don't know much about php, so need some help please. Thanks.
<form action="post.php" method="post">
<div class="form-group">
<label for="name">Name *</label>
<input type="text" class="form-control" id="name"
placeholder="" required="required" oninvalid="this.setCustomValidity('Please enter your name')"
oninput="setCustomValidity('')">
</div>
<div class="form-group">
<label for="email">E-mail *</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="" required="required"
oninvalid="this.setCustomValidity('please enter your email')" oninput="setCustomValidity('')">
</div>
</div>
<div class="form-group">
<label for="name">phone *</label>
<input type="text" class="form-control" id="phone"
placeholder="" required="required" oninvalid="this.setCustomValidity('please enter your phone')"
oninput="setCustomValidity('')">
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label for="name">message *</label>
<textarea name="message" id="message" class="form-control"
rows="10" cols="25" required="required" placeholder="" oninvalid="this.setCustomValidity('please enter your message')"
oninput="setCustomValidity('')"></textarea>
</div>
</div>
<div class="col-md-12">
<button type="submit" class="btn btn-primary pull-right" id="btnContactUs">send</button>
</form>
and php code:
<?php
$addressto = "info#xxx.com";
$subject = "Message from the website";
$content = "Name: ".$_POST['name']."\n"
."Email: ".$_POST['email']."\n"
."Tel.: ".$_POST['phone']."\n"
."Message: ".$_POST['message']."\n";
if(!$_POST['name'] || !$_POST['email'] || !$_POST['message']){
header("Location: contact.html");
}
$email = $_POST['email'];
if (mail($addressto, $subject, $content, 'Od: <' . $email . '>')) {
header("Location: contact-ok.html");
}
?>
Your input fields are missing the name attribute
id is mostly for JS/CSS manipulation, while name will be the keys in your POST array.
They can both be the same. Hence why your message works.
Remember that JS validation can easily be bypassed and that the user input should also be checked in php.
<input type="text" class="form-control" id="name" name="name"...>
I am unable to configure my email address on the submit button in this piece of code:
<div id="contact" class="spacer">
<div class="container contactform center">
<h2 class="text-center wowload fadeInUp">Get in touch with us</h2>
<div class="row wowload fadeInLeftBig">
<div class="col-sm-6 col-sm-offset-3 col-xs-12">
<form class="cmxform" id="commentForm" method="post" action="email.php">
<fieldset>
<input type="text" placeholder="Subject" id="csubject" name="subject" minlength="2" type="text" required>
<input type="text" placeholder="Email" id="cemail" type="email" name="email" required>
<textarea rows="5" placeholder="Message" id="ccomment" name="comment" required></textarea>
<input class="submit btn btn-primary" type="submit" value="Submit">
</fieldset>
</form>
</div>
</div>
I tried linking it to a php page (email.php), but it says server error. I don't know what to do. Can someone please help me?
To send an email you need the mail() function http://php.net/manual/en/function.mail.php
With your code you need to name the submit like name="submit" then inside action.php file you have to write something like this (or inside the same php where you have this code):
if(isset($_POST["submit"]))
{
//Remember to do input validations
$subject = $_POST["subject"];
$from = $_POST["email"];
$comment = $_POST["comment"];
$body = "User with email $from send the next comment: $comment";
//then here you set your email
$to = "myemail#email.com"; //change this
mail($to,$subject,$body);
}
This is only to explain some basic usage and set the variables you need, I advice you read also PHPmailer class
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I know this may have been submitted before (sorry)
I have basic form, these are the details id like to be sent, however i cannot get the reCaptcha to work with it. I have googled all day, but when i try other peoples code (amending to fit mine) it doesnt seem to work.
I would like: Name, Email, Number, newsletter (yes/no) and recaptcha to be sent/work.
Can someone please give me an idea where i may be going wrong? what i may need to add?
Thanks in advance!
Here is my Form (html)
<form method="POST" action="Form_Activation.php">
<div class="form-group">
<label for="name">Name:</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Full Name" value="" required/>
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" class="form-control" id="email" name="email" value="" placeholder="you#example.com" required/>
</div>
<div class="form-group">
<label for="number">Number:</label>
<input class="form-control" name="number" id="number" value="" placeholder="Contact Number" required/>
</div>
<div class="form-group">
<label for="message">Message:</label>
<textarea class="form-control" name="message" id="message" placeholder="Enter Message.." required></textarea>
</div>
<div class="form-group">
<input type="checkbox"/> <b> Subscribe to Newsletter</b>
</div>
<div class="g-recaptcha" data-sitekey="6Le2SBQTAAAAADIOrUEPpcEVvR_c0vN9GzQpLg05"></div>
<button type="submit" class="btn btn-default sendbutton">SEND</button>
</form>
Here is my php (basic)
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$number = $_POST['number'];
$message = $_POST['message'];
//$password = $_POST['password'];
//$keyy = $_SERVER['UNIQUE_ID'];
$msg = "Name: $name\r\n \r\n";
$msg .= "Email: $email\r\n \r\n";
$msg .= "Number: $number\r\n \r\n";
$msg .= "Message: $message\r\n \r\n";
$recipient = "info#islandwebdesign.co.uk";
$subject = "New Website Request";
$mailheaders = "From:$email";
//$mailheaders .= "Reply-To:$email";
mail($recipient,$subject,$msg,$mailheaders);
header("Location: contactus.php?msg=1");
?>
First of all make sure that you've included the necessary JavaScript resource to render reCAPTCHA widget properly, like this:
<html>
<head>
<title>reCAPTCHA demo: Simple page</title>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
</head>
<body>
<form action="?" method="POST">
<div class="g-recaptcha" data-sitekey="your_site_key"></div>
<br/>
<input type="submit" value="Submit">
</form>
</body>
</html>
Here's the reference:
Displaying the widget
Now comes to your user's response. The response from the user's captcha challenge can be fetched in three ways. It can be as,
Now comes to your user's response. The response from the user's captcha challenge can be fetched in three ways. It can be as,
g-recaptcha-response - a POST parameter in the submitted form
grecaptcha.getResponse(widget_id) - will provide the response after the user completes the captcha.
A string argument to the callback function specified in the config object passed to the render method.
Here's the reference:
Verifying the user's response
For your purpose use g-recaptcha-response to get the user's response. So your code should be like this:
HTML
<form method="POST" action="Form_Activation.php">
<div class="form-group">
<label for="name">Name:</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Full Name" value="" required/>
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" class="form-control" id="email" name="email" value="" placeholder="you#example.com" required/>
</div>
<div class="form-group">
<label for="number">Number:</label>
<input class="form-control" name="number" id="number" value="" placeholder="Contact Number" required/>
</div>
<div class="form-group">
<label for="message">Message:</label>
<textarea class="form-control" name="message" id="message" placeholder="Enter Message.." required></textarea>
</div>
<div class="form-group">
<input type="checkbox"/> <b> Subscribe to Newsletter</b>
</div>
<div class="g-recaptcha" data-sitekey="6Le2SBQTAAAAADIOrUEPpcEVvR_c0vN9GzQpLg05"></div>
<button type="submit" name="submit" class="btn btn-default sendbutton">SEND</button>
</form>
Add a name attribute in your submit button.
Form_Activation.php
<?php
if(isset($_POST['submit'])){
//your site secret key
$secret = 'XXXXXXX_Secret-key_XXXXXXX';
if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])){
//get verified response data
$param = "https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$_POST['g-recaptcha-response'];
$verifyResponse = file_get_contents($param);
$responseData = json_decode($verifyResponse);
if($responseData->success){
// success
$name = $_POST['name'];
$email = $_POST['email'];
$number = $_POST['number'];
$message = $_POST['message'];
// so on
}else{
// failure
}
}
}
?>
Don't forget to add your secret key in $secret variable.
I'm totally new to web design and have gotten stuck creating a contact form. FWIW The original 'frame' of the site came from a template I downloaded.
This is what my form looks like on the index.html side of things:
<div class="col-sm-6 col-md-6">
<form action="contact.php" method="post">
<div class="form-group">
<!--<label for="name">name</label>-->
<input type="text" id="name" class="form-control" placeholder="Name" />
</div>
<div class="form-group">
<!--<label for="email">email</label>-->
<input type="text" id="email" class="form-control" placeholder="Email Address" />
</div>
<div class="form-group">
<!--<label for="message">message</label>-->
<textarea id="message" class="form-control" rows="7" placeholder="Write a message">
</textarea>
</div>
<button type="submit" class="btn btn-primary">Send</button>
And this is what the contact.php looks like:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = "exampleemail#gmail.com";
$subject = "New Contact Us Enquiry";
$text = "A visitor of ninadaviesopticians.co.uk has submitted the following enquiry.\n\nName:$name\n\nEmail: $email\n\nMessage: $message\n\nPlease respond to this enquiry within 24 hours";
mail ($to, $subject, $text);
header("Location:index.html");
?>
When I fill in the contact form, it sends the email to the desired address and gives me the 'A visitor of.....' text without an issue. However, it leaves 'Name', 'Email' and 'Message' fields blank, as if the user never entered them.
Any ideas? Appreciate the help in advance.
Your fields require the "name" attribute to be filled to be sent in POST or GET data, not ID.
So for example, your name field would be this :
<input type="text" name="name" class="form-control" placeholder="Name" />
You can have both an id and a name, though the id is not used for posting data.
Ref : http://www.php.net//manual/en/reserved.variables.post.php
To post data via forms, the fields need to have the name attribute:
<input type="text" id="name" name="name" class="form-control" placeholder="Name" />
The ID is only really useful for client side tools.