Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
i've got a basic php script for simple contact form with name, email and message inputs.
I wish to include few more options in it but don't know how to. I've searched but couldn't find all in one solution for. I would like to:
1. Send a copy to senders email
I would like to include input for sender to have an option to receive a copy of he's submit to he's email if he checkes that input in the form.
2. Upload a file
Also if possible in the same php script i wish to give a possibility for the sender to attach a file (preferably img extensions only) when submiting a form.
3. Thank you message
Not sure about this, but now i have a simple thank you message in echo when form is submited. If possible, i wish for this message to stay visible for 5 seconds then redirect to index.html.
Here is php for the form:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="Name: $name \nEmail: $email \nMessage: $message";
$recipient = "test123#...";
$subject = "Contact";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo
"<div style='display: block; text-align: center;'>" .
"<span style='font-size: 14px;'>You message has been sent!</span>" .
"<a href='index.html'>Go back</a>" .
"</div>";
?>
and demo jsfiddle of the form setup.
Thanks for any help.
This is a global setup just to let you know how I would do this (if I wanted to do this on 1 page, but it's better to make functions, etc.)
EDIT: Please note also that I don't know if this works. Maybe there are errors but I have done this just to get you started.
<?php
//Check if form submitted
if (isset($_POST)) {
//this all will run when form is submitted
//First sanitize you data thats been posted
$name = htmlentities($_POST['name'], ENT_QUOTES, 'UTF-8');
$email = htmlentities($_POST['email'], ENT_QUOTES, 'UTF-8');
$message = htmlentities($_POST['message'], ENT_QUOTES, 'UTF-8');
//make a error array to hold errors
$error = array();
//check if fields are not empty you can also do other checks
if (!empty($name) || !empty($email) || !empty($message))
//here you could do extra checks.. like check if emai is really a email...
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
//email invalid
array_push($error, 'email not valid');
}
//for image you could also do a if...
if(isset($_FILES)) {
$uploads_dir = 'YOUR DIR'
$name = $_FILES['image']['name'];
$type = $_FILES['image']['type'];
$size = $_FILES['image']['size'];
$temp = $_FILES['image']['tmp_name'];
$error = $_FILES['image']['error'];
if ($error === 4) {
//No file was selected
return false;
}
else
{
//do your stuff with the image here...
move_uploaded_file($temp, "$uploads_dir/$temp");
}
///you could do more ifs.. but if all is good then do the mail
$subject = 'new contact form message';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($email, $subject, $message, $headers);
$success = "here the success message";
} else {
//some fields are empty
array_push($error, 'some fields are empty');
}
?>
<!-- THE ENCTYPE IS NEEDED FOR IMAGES -->
<form action="submit.php" name="contact-form" id="contact-form" method="post" enctype="multipart/form-data">
<input name="name" placeholder="Name" type="text" id="name" required />
<input name="email" placeholder="Email" type="email" id="email" required />
<textarea name="message" placeholder="Message" id="message" required></textarea>
<input type="file" id="upload-file" accept="image/*" />
<div class="clear"></div>
<input type="checkbox" id="copy" name="copy" />
<label for="copy">Send a copy to my email</label>
<div class="clear"></div>
<input type="submit" value="Submit" form="contact-form" name="submit-form" />
</form>
<?php
if (isset($success) && !empty($success)) {
//echo the success
echo $success
}
if (isset($error) && !empty($error)) {
//loop trough error
foreach ($error as $e) {
echo $e;
}
}
?>
Related
I am using Js and PHP, so basically I would like form submissions to be sent via emails with out the page to reload which it is not working. Any help would be great thanks.
This is just a simple form where you submitted and the input data appears below where it should be sent via email as well.
<form id="myForm" method="post">
Name: <input name="name" id="name" type="text" /><br />
<input type="button" id="submitFormData" onclick="SubmitFormData();" value="Submit" />
</form>
<br/>
Your data will display below..... <br />
==============================<br />
<div id="results">
<!-- All data will display here -->
</div>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
function SubmitFormData() {
var name = $("#name").val();
$.post("./submit.php", { name: name },
function(data) {
$('#results').html(data);
$('#myForm')[0].reset();
});
}
</script>```
Here another file on form process.
<?php
if( $_SERVER['REQUEST_METHOD'] == 'POST') {
$name = $_POST['name'];
echo $name;
$to = 'name#name.com';
$subject = " Test Enquiries";
// 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: domain#domain.com' . "\r\n";
// Email body content
$message = "
<p><strong>Name:</strong> <span>{$name}</span></p>
";
mail($to,$subject,$message,$headers);
if (mail ($to, $subject, $message, $headers))
{
echo '<p>Your message has been sent!</p>';
}
else
{
echo '<p>Something went wrong, go back and try again!</p>';
}
}
?>
I want to send an email when someone click on my website's download link. I am working in PHP and JavaScript. The following two snippets are in same file.
HTML
<form method="post">
<div class="form-group">
<input type="email" class="form-control" placeholder="Enter Email ID" id="email" onchange="check();" require pattern="[a-z0-9._%+-]+#[a-z0-9.-]+\.[a-z]{2,3}$"/>
</div>
<div class="form-group">
<label style="font-size:14px;"><input type="checkbox" id="toggle"> I agree to receive emails from MarketerBoard. We will not share your any personal information with anyone.</label>
</div>
<div class="form-group" style="min-height:40px;">
<a style="display:none;" href="/images/docs/EMAIL-MARKETING-GUIDE.pdf" name="downloadBtn" download class="mb-button" id="dwn-btn" onchange="check();" onClick="sendmail();">Get your free guide</a>
</div>
</form>
JavaScript
if(isset($_POST['downloadBtn']))
{
$to = 'examle#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: example#example.com' . "\r\n" .
'Reply-To: example#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
//echo 'Email Sent.';
}
I validate the email id of user from using following code. I want to send the email on click of the link, which does not seems to work.
<script type="text/javascript">
function check() {
document.getElementById("toggle").checked = false;
var email_x = document.getElementById("email").value;
filter = /^([a-zA-Z0-9_\.\-])+\#(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if (filter.test(email.value)) {
document.getElementById("email").style.border = "2px solid teal";
var userEmail = document.getElementById('email').value
$('#toggle').click(function () {
//check if checkbox is checked
if ($(this).is(':checked') & filter.test(email.value)) {
$("#dwn-btn").show(); }
else {
$("#dwn-btn").hide();}});
return true;
} else {
document.getElementById("email").style.border = "2px solid red";
$("#dwn-btn").hide();
return false;
}
}
</script>
Firstly check/change your PHP mail configuration:
Open your php.ini file
Search for the line that reads [mail function]
Add/change the details of your mail server. This could be a local mail server or the mail server of your ISP.
Save/close the php.ini file
Restart your web server
Example code below :
<form action="" method="post">
<input type="submit" value="Send email" />
<input type="hidden" name="send_mail" value="1" />
</form>
<?php
if(isset($_POST['send_mail']))
{
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: nobody#example.com' . "\r\n" .
'Reply-To: nobody#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
echo 'Email Sent.';
}
?>
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I am trying to create a contact form using php and a html form. When the user clicks submit I want the details that they have entered into the form to be sent in an email.
When the user clicks submit they are shown a message to say the message has been sent but when I check my email nothing has been sent.
Here is my code, please note the email has been changed to show the code here.
<div id = "form">
<form action ="contact2.php" method="post">
Name:
<input type="text" name="name">
<br>
<br>
Email:
<input type="text" name="email">
<br>
<br>
Message:
<br>
<br>
<TEXTAREA NAME="message" ROWS=6 COLS=40>
</TEXTAREA>
<br>
<br>
<input type="submit" value="Submit">
</div>
</form>
<?php
$field_name = $_POST['name'];
$email = $_POST['email'];
$field_message = $_POST['message'];
$mail_to = 'example#yahoo.co.uk';
$subject = 'Message from a site visitor ' . $field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$email."\n";
$body_message .= 'Message: '.$field_message;
$headers = "From: $email\r\n";
$headers .= "Reply-To: $email\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'contact.php';
</script>
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to example#yahoo.co.uk');
window.location = 'contact.php';
</script>
<?php
}?>
you want to have all that javascript wrapped in a php echo or else it doesnt work.
so...
if ($mail_status) {
echo '<script language="javascript" type="text/javascript">';
thats just a bit of the code you need to implement...
you want to echo every javascript line too.
After searching for about 3 hours i still can't figure this one out.
I Have a html template with a contact form and im trying to make it work using a PHP script.
I changed the template to PHP and pasted the PHP form script in it. everything is working fine except the confirmation text.
After a successful submission it will just refresh the page instead of printing "Your mail has been sent successfuly ! Thank you for your feedback". i do not want a redirect, i just want it to print on the same page.
Any ideas?
I got a sample of my code.
<form action="<? echo $_SERVER['PHP_SELF']; ?>" id="contact-form" method="post" class="form afsana-form" role="form">
<div class="row">
<div class="col-sm-12 form-group">
<input class="form-control afsana-style" id="name" name="name" placeholder="name" type="text" required autofocus />
</div>
<div class="col-sm-12 form-group">
<input class="form-control afsana-style" id="email" name="email" placeholder="email" type="email" required />
</div>
<div class="col-sm-12 form-group">
<textarea class="form-control" id="message" name="message" placeholder="message" rows="5"></textarea>
</div>
<div class="col-sm-12 form-group">
<button class="btn btn-primary afsana-btn" name="submit" value="verzenden" type="submit">Verzenden <i class="ion-arrow-graph-up-right"></i></button>
</div>
</div>
</form>
<?php
if(isset($_POST["submit"])){
// Checking For Blank Fields..
if($_POST["name"]==""||$_POST["email"]==""||$_POST["message"]==""){
echo "Fill All Fields..";
}else{
// Check if the "Sender's Email" input field is filled out
$email=$_POST['email'];
// Sanitize E-mail Address
$email =filter_var($email, FILTER_SANITIZE_EMAIL);
// Validate E-mail Address
$email= filter_var($email, FILTER_VALIDATE_EMAIL);
if (!$email){
echo "Invalid Sender's Email";
}
else{
$subject = (Contact_form);
$message = $_POST['message'];
$headers = 'From:'. $email . "\r\n"; // Sender's Email
$headers .= 'Cc:'. $email2 . "\r\n"; // Carbon copy to Sender
// Message lines should not exceed 70 characters (PHP rule), so wrap it
$message = wordwrap($message, 70);
// Send Mail By PHP Mail Function
mail("something#domain.com", $subject, $message, $headers);
echo "Your mail has been sent successfuly ! Thank you for your feedback";
}
}
}
?>
First, you have this: $subject = (Contact_form); which should throw an error, so I assume you have error reporting turned off. When developing, you should have error reporting on so you can see errors in your code... Else you are just working blind. I don't mean by throwing tacky error_reporting(0) in every file either, I mean to set your error reporting level to E_ALL in your php.ini.
You also have: $headers .= 'Cc:'. $email2 . "\r\n";
However, $email2 is not defined anywhere, so you would get an error here too.. which is why it's important to test with error reporting on.
See if this works:
<?php
$error = '';
if(isset($_POST['submit']))
{
if ( !empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['message']) )
{
$email = $_POST['email'];
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
if ( $email = filter_var($email, FILTER_VALIDATE_EMAIL) )
{
$subject = '(Contact_form)';
$message = $_POST['message'];
$headers = 'From:'. $email . "\r\n"; // Sender's Email
$message = wordwrap($message, 70);
if ( $result = mail("something#domain.com", $subject, $message, $headers) ) {
$error = 'Success';
} else {
$error = 'There was an error sending your email!';
}
} else {
$error = 'Invalid Email Address!';
}
} else {
$error = 'Please fill all fields.';
}
}
?>
<p><?= $error ?></p>
<form action="" method="post">
<input type="text" name="name" value="" /><br />
<input type="email" name="email" value="" /><br />
<textarea name="message" rows="5"></textarea><br />
<input type="submit" name="submit" value="submit" />
</form>
Try to put in $subject just a string value like:
$subject = 'Test subject';
change also the following line to this (there is no $email2 defined):
$headers .= 'Cc:'. $email . "\r\n"; // Carbon copy to Sender
and give it a try. You can also put as first line of your code
<?php error_reporting(E_ALL); ?>
and check for errors when submiting the form.
This is my code for a simple contact form. All the emails successfully get sent however when they don't enter any information the supposedly error message that should appear doesn't and it sends an email blank. As I do not wish to have spam emails, what am I doing wrong?
Code:
<?php
$field_name = $_POST['name'];
$field_email = $_POST['email'];
$field_message = $_POST['message'];
$mail_to = 'myemail#goeshere';
$subject = 'Message from client: '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for contacting us');
window.location = 'index.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Your message failed to send due to invalid credentials.');
window.location = 'index.html';
</script>
<?php
}
?>
The HTML form:
<form method="post" action="contact.php">
<div class="row half">
<div class="6u"><input type="text" class="text" name="name" placeholder="Name" /></div>
<div class="6u"><input type="text" class="text" name="email" placeholder="Email" /></div>
</div>
<div class="row half">
<div class="12u">
<textarea name="message" placeholder="Message"></textarea>
</div>
</div>
<div class="row">
<div class="12u">
Send Message
</div>
</div>
</form>
Sorry if the code isn't indented properly...
You don't have any code that checks if the fields are filled in. You should check that the fields are set using isset() and that they contain valid data by using things like filter_var
Also, you should not be relying on JavaScript to do redirects, If the user has JavaScript disabled this won't work. Use PHP to do your redirect.
The code always sends the email, regardless of the validation status of the form. You can, instead, check the form validation before attempting to send the email. The overall logic would look like this:
Parse user input
Are required fields valid?
Yes: Send email, display success message
No: Display error message
In the code, it might be something as simple as:
$field_name = $_POST['name'];
$field_email = $_POST['email'];
$field_message = $_POST['message'];
if ($field_email != '') {
// compose and send the email
// display success message
} else {
// display error message
}
For additional checks on the other fields, you'd add additional conditions to the if statement.
The mail function will return TRUE if the mail is accepted for delivery. Since all required parameters are present it will accept the mail even though the parameters are empty strings.
You would need to check if the input is valid or not.