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.
Related
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 3 years ago.
I'm new to PHP and I'm making a contact form for my website. However whenever I fill out and submit the form it leads me to a blank page and I never receive an email. I have tested it when my website is live. My host is infinity free if it matters.
I tried troubleshooting such as making minor modifications to the code (changing a few names, etc.) Nothing worked.
<html>
<section id="contact">
<div class="container-contact">
<div style="text-align:center">
<h3> Contact Me </h3>
<p> I will get back to you shortly. </p>
</div>
<div class="row">
<div class="column">
<form id="contact-form" method="post" action="contactform.php">
<label for="firstname">First Name</label>
<input type="text" id="firstname" placeholder="Your first name..." required>
<label for="lastname">Last Name</label>
<input type="text" id="lastname" placeholder="Your last name..." required>
<label for="email">Email Address</label>
<input type="email" id="email" placeholder="Your email..." required>
<label for="message">Message</label>
<textarea id="message" name="message" placeholder="Write your message..." style="height:170px" required></textarea>
<input type="submit" name="submit" value="Submit">
</form>
</div>
</div>
</div>
</section>
</html>
<?php
$firstname = $_POST['firstname']
$lastname = $_POST['lastname']
$user_email = $_POST['email'];
$message = $_POST['message']
$email_from = 'myemail#domain.com'
$email_subject = "New Form Submission from $firstname.\n";
$email_body = "First Name: $firstname.\n".
"Last Name: $lastname.\n".
"Email: $user_email.\n".
"Message: $message.\n";
$to = "myemail#domain.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply To: $user_email \r\n";
mail($to,$email_subject,$email_body,$headers);
header("Location: contact.html");
?>
Τhe HTML file name is contact.html and the PHP file is contactform.php
The form doesn't submit and I don't receive an email. Am I doing something wrong? Do I need to setup something else?
Thanks in advance,
Thomas
Guessing you are getting undefined index errors and your environment is not set to display errors. I would add the name attribute to the input fields that are missing it as a start.
For example the first name field
<input type="text" id="firstname" placeholder="Your first name..." required>
should be
<input type="text" name="firstname" id="firstname" placeholder="Your first name..." required>
In addition you should look at turning errors on or viewing the log file.
Due to PHP parse error, you are getting blank page. You need to use semicolon(;) in PHP, in order to end a statement.
For example, instead of,
$firstname = $_POST['firstname']
it should be,
$firstname = $_POST['firstname'];
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 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>.
So I recently uploaded my first website to an iPage server. The website runs perfectly with the exception of the Contact Form which for some reason refuses to send any email whatsoever from the form.
I use a PHP Script with the Post Method, and tried to fix my code multiple times to correct any error I might have entered, but so far to no avail. Here is the code:
HTML:
<
form action = "js/mailer.php" form method="post" name="contactform" id="contactform" class="form validate item_bottom" role="form">
<div class="form-group">
<input type="text" name="name" id="name" class="form-control required" placeholder="Name">
</div>
<div class="form-group">
<input type="email" name="email" id="email" class="form-control required email" placeholder="Email">
</div>
<div class="form-group">
<textarea name="message" id="message" class="form-control input-lg required" rows="9" placeholder="Enter Message"></textarea>
</div>
<div class="form-group text-center">
<input type="submit" id="contactForm_submit" class="btn btn-trans btn-border btn-full" value="Submit">
</div>
</form>
PHP:
<?php
if(isset($_POST['submit'])) {
$to = "aravindm3095#gmail.com";
$subject = "Hello Aravind!";
// data the visitor provided
$name_field = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
$email_field = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
$comment = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
//constructing the message
$body = " From: $name_field\n\n E-Mail: $email_field\n\n Message:\n\n $comment";
mail($to, $subject, $body);
// redirect to confirmation
header('Location: confirmation.html');
} else {
echo "Failure";
}
?>
Can someone help me with this? It might be an error with my hosting server, or an error with my code. Your help is very much appreciated
Additional Comments: Followed the help provided (thank you for that) I made the necessary changes to the HTML and PHP, but the form is still not functional. It does not echo failure or redirect to the confirmation page, and upon inspecting the element with Firefox, I notice that upon hitting the submit button a subdivision appears under it saying "Sending....". But no email is sent, no message is echoed or page opened.
From what i see in the code you posted, the PHP mailing script won't work as you are checking if a POST variable with the name 'submit' exists which it does not as in your form the submit button does not have a name attribute.
Try giving the submit button a name and put that name in the PHP if statement.
Submit button name is missing in your form, You need to add name into your submit button,
<input type="submit" name="submit" id="contactForm_submit" class="btn btn-trans btn-border btn-full" value="Submit">
^^^^^^^^^^^^
instead of
<input type="submit" id="contactForm_submit" class="btn btn-trans btn-border btn-full" value="Submit">
Your HTML part should be something like below,
<form action = "js/mailer.php" method="post" name="contactform" id="contactform" class="form validate item_bottom" role="form">
<div class="form-group">
<input type="text" name="name" id="name" class="form-control required" placeholder="Name">
</div>
<div class="form-group">
<input type="email" name="email" id="email" class="form-control required email" placeholder="Email">
</div>
<div class="form-group">
<textarea name="message" id="message" class="form-control input-lg required" rows="9" placeholder="Enter Message"></textarea>
</div>
<div class="form-group text-center">
<input type="submit" name="sendemail" id="contactForm_submit" class="btn btn-trans btn-border btn-full" value="Submit">
</div>
</form>
In above, I have added name="sendemail" in submit button.
And your PHP script that sends email should be like
<?php
if(isset($_POST['sendemail'])) { // <-- variable name changed
$to = "aravindm3095#gmail.com";
$subject = "Hello Aravind!";
// data the visitor provided
$name_field = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
$email_field = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
$comment = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
//constructing the message
$body = " From: $name_field\n\n E-Mail: $email_field\n\n Message:\n\n $comment";
$headers = "From: " . $email_field; // <-- Code added
mail($to, $subject, $body, $headers); // <-- Code added
// redirect to confirmation
header('Location: confirmation.html');
exit;
} else {
echo "Failure";
}
?>