I am working on the contact form on a webpage. The HTML and CSS is complete, how it supposed to be. However, now I need to make this button work so it sends an e-mail to the given e-mail address. I am a newbie in PHP, so hope someone could help me out.
HTML code:
<div id="thirdColumn">
<div id="contactOns">
<form action="send.php" method="post" enctype="text/plain" class="form">
<p class="name">
<input type="text" name="name" id="name" placeholder="NAAM" />
</p>
<p class="email">
<input type="text" name="email" id="email" placeholder="EMAIL" />
</p>
<p class="text">
<textarea name="text" name="message" id="message" placeholder="BERICHT"></textarea>
</p>
<p class="submit">
<input type="submit" id="sent" value="VERSTUUR" />
</p>
</form>
</div> <!-- End contactOns -->
</div> <!-- End thirdColumn -->
PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$mail_to = 'riksterrr#gmailc.com';
$subject = 'Bericht van een bezoeker '.$name;
$body_message = 'From: '.$name."\n";
$body_message .= 'E-mail: '.$email."\n";
$body_message .= 'Message: '.$message;
$headers = 'From: '.$email."\r\n";
$headers .= 'Reply-To: '.$email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
?>
2 things that needed to be changed in your HTML form are, get rid of enctype="text/plain"
and change:
<textarea name="text" name="message" id="message" placeholder="BERICHT"></textarea>
to:
<textarea type="text" name="message" id="message" placeholder="BERICHT"></textarea>
You had name="text" which should be type="text"
Since you do not have your own Web server setup with PHP, you will need to find a hosting company that has mail() available in order to run your code properly.
Related
I am having trouble with my PHP code for my website. The email is being sent but has no messages.
This is the email I get when it sends. It's empty.
Data I input in my contact form
I tested my php code without the css files and bootstrap, and received the email perfectly with the messages. But when I included everything, from css and bootstrap codes, I receive the email without any messages.
HTML CODE:
<div class="contact-form bottom">
<h2> We want to hear from you . Send us a message!</h2>
<form id="main-contact-form" name="contact-form" method="post" action="send_email_test.php">
<div class="form-group">
<input type="text" name="userName" class="form-control" required="required" placeholder="Name">
</div>
<div class="form-group">
<input type="email" name="userEmail" class="form-control" required="required" placeholder="Email Id">
</div>
<div class="form-group">
<textarea name="userMessage" id="message" required class="form-control" rows="8" placeholder="Your text here"></textarea>
</div>
<div class="form-group">
<input type="submit" name="submit" class="btn btn-submit" value="Submit">
</div>
</form>
</div>
This is my PHP code:
<?php
$field_name = $_POST['userName'];
$field_email = $_POST['userEmail'];
$field_subject = $_POST['userSubject'];
$field_message = $_POST['userMessage'];
$mail_to = 'info#mariamulan.com'; /* Add your email address here */
$subject = 'Message from website'.$field_name; /* Create your own subject */
$body_message .= 'From: '.$field_name."\n";
$body_message .= 'Email: '.$field_email."\n";
$body_message .= 'Subject: '.$field_subject."\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('Hey! Thanks for the message! We will try to reply to you as soon as possible!');
window.location = 'index.html'; /* Where you want to get directed */
</script>
<?php
} else { ?>
<script language="javascript" type="text/javascript">
alert('Sorry, your message was not sent! Please send an email to
hello.mariamulan#gmail.com instead.');
window.location = 'index.html'; /* Where you want to get directed
*/
</script>
<?php
}
?>
Verify that your form has the correct "name" attributes.
Your code is pretty dirty tho.
I try to show a message in html page, after send an e-mail.
I don't want to use javascript with an alert, just a simple message after send button.
I made a contact php page with this code:
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_telefon = $_POST['cf_telefon'];
$field_message = $_POST['cf_message'];
$mail_to = 'diaconu.eduardstefan#gmail.com';;
$from = 'Mesaj nou de la:'.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Telefon: '.$field_telefon."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $from, $body_message, $headers);
?>
And in html page, i insert a contact form with this code:
<form action="contact.php" method="post">
<div class="form-group">
<label for="name">
Nume
</label>
<input type="text" name="cf_name" placeholder="" id="name" class="form-control" required="true">
</div>
<div class="form-group">
<label for="email">
Email
</label>
<input type="text" name="cf_email" placeholder="" id="email" class="form-control" required="true">
</div>
<div class="form-group">
<label for="phone">
Telefon
</label>
<input type="text" name="cf_telefon" id="phone" class="form-control">
</div>
<div class="form-group">
<label for="phone">
Mesaj
</label>
<textarea name="cf_message" placeholder="" rows="5" class="form-control" required="true">
</textarea>
</div>
<input class="btn btn-info" type="submit" value="Trimite" >
<?php if($send_mail) {
if($mail_status){
print "succes";
exit();
}
else {
print "eroare";
}
}
?>
</form>
Email was successfully sent, but the message isn't show. After press send button, contact form return a blank page. I want to return the same page with message. (ater refresh)
Maybe exist another way to do that? With get or something like that?
Thanks for help!
Place your PHP in the top of the page of where your contact form is.
then change: <form action="contact.php" method="post">
to
<form action="" method="post">
you also need to add name="submit" to your submit button field and then wrap:
if (isset($_POST['submit'])) {
// code
}
Also if you are trying to make sure people fill in all fields, you should use
if (!empty($var))
{
// code
} else
echo "Fill in this field please";
}
required fields can be easily bypassed by just using required within the html side.
EDIT: once all conditions are met you can simply add echo "thanks" or whatever message you want to beneath the mail() field for a success message which will output to the page above the form.
if($_POST){
// send mail code here
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_telefon = $_POST['cf_telefon'];
$field_message = $_POST['cf_message'];
$mail_to = 'diaconu.eduardstefan#gmail.com';;
$from = 'Mesaj nou de la:'.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Telefon: '.$field_telefon."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $from, $body_message, $headers);
if($mail_status){
echo 'mail send';
}else{
echo 'mail not send';
}
}
//place html code here below the php code
<form action="" method="post">
<div class="form-group">
<label for="name">
Nume
</label>
<input type="text" name="cf_name" placeholder="" id="name" class="form-control" required="true">
</div>
<div class="form-group">
<label for="email">
Email
</label>
<input type="text" name="cf_email" placeholder="" id="email" class="form-control" required="true">
</div>
<div class="form-group">
<label for="phone">
Telefon
</label>
<input type="text" name="cf_telefon" id="phone" class="form-control">
</div>
<div class="form-group">
<label for="phone">
Mesaj
</label>
<textarea name="cf_message" placeholder="" rows="5" class="form-control" required="true">
</textarea>
</div>
<input class="btn btn-info" type="submit" value="Trimite" >
</form>
The contact form will only send an email will the words name email and message. I have been going over it for a few hours but nothing seems to be working.
Here is the contact php file (email is edited)
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];
$mail_to = 'outlook.com';
$subject = 'Message about Tejano Fest '.$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 the message. We will contact you shortly.');
window.location = 'index.html';
</script>
<?php
Here is the html segment it's related to:
<form method="post" name="contact" action="#contact">
<div class="left">
<label for="author">Name:</label>
<input name="author" type="text" class="input_field" id="author" maxlength="40" />
</div>
<div class="right">
<label for="email">Email:</label>
<input name="email" type="text" class="input_field" id="email" maxlength="80" />
</div>
<div class="clear"></div>
<label for="text">Message:</label> <textarea id="text" name="text" rows="0" cols="0"></textarea>
<input type="submit" class="submit_btn float_l" name="submit" id="submit" value="Send" />
</form>
Thank you in advance for any assistance.
change it (there are the name of input):
$field_name = $_POST['author'];
$field_email = $_POST['email'];
$field_message = $_POST['text'];
Enjoy your code!
Use this... your POST array values does not matching with your form field names .
<form method="post" name="contact" action="#contact">
<div class="left">
<label for="author">Name:</label>
<input name="cf_name" type="text" class="input_field" id="author" maxlength="40" />
</div>
<div class="right">
<label for="email">Email:</label>
<input name="cf_email" type="text" class="input_field" id="email" maxlength="80" />
</div>
<div class="clear"></div>
<label for="text">Message:</label> <textarea id="text" name="cf_message" rows="0" cols="0"></textarea>
<input type="submit" class="submit_btn float_l" name="submit" id="submit" value="Send" />
<?php
if(isset($_POST['submit'])){
$field_name = $_POST['author'];
$field_email = $_POST['email'];
$field_message = $_POST['text'];
$mail_to = 'test#outlook.com';
$subject = 'Message about Tejano Fest '.$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 the message. We will contact you shortly.');
window.location = 'index.html';
</script>
<?php
} }?>
Some other option to consider(of course after changing the values of the POST arrays), you may want to try using PhpMailer-http://phpmailer.worxware.com/?pg=examplebsendmail as maybe your hosting solution requires Smtp authourisation.
I'm trying to send an email using php but it's not working..
This is my code:
<form method="post" name="contact" action="#contact">
<div class="left">
<label for="author">Name:</label> <input name="nom" type="text" class="input_field" id="author" maxlength="40" />
</div>
<div class="right">
<label for="email">Email:</label> <input name="email" type="text" class="input_field" id="email" maxlength="40" />
</div>
<div class="clear"></div>
<label for="text">Message:</label> <textarea id="text" name="text" rows="0" cols="0"></textarea>
<input type="submit" class="submit_btn float_l" name="submit" id="submit" value="Send" />
</form>
<?php
$name= ($_POST["nom"]);
$mail= ($_POST["email"]);
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From:<nelson-book#outlook.fr>' . "\r\n";
$suject="Book";
$message = "Nom: <br/>" .$name. "email:<br/> " .$email. "message: " .$_POST["text"];
if (isset($_POST['submit'])) {
mail("nelson-book#outlook.fr", $suject, $message, $headers);
echo" teste";
}
?>
I already used a code SUPER similar to this ant it totally worked.. Can the problem be from the server?
thanks in advance.
check out your SMTP server restriction when you send email, with another hosting domain like, "outlook.fr", somethime this is't allowed.
I'm a total novice so please bear with me :)…I've managed to create a form and used PHP to send the data to an email address. However, once I click submit; the screen goes blank instead of staying on the current page and displaying a message. I'm guessing i'm missing some sort of PHP code?
Also, i'd like to use the JQuery validator plugin on my form, how can I add it without basically screwing up the form?
MY HTML:
<div>
<form id="form_id" name="form_name" action="scripts/index.php" method="post">
<div>
<label for="name">Name: </label>
<input type="text" name="name" id="name" placeholder="John Smith" required/>
</div>
<div>
<label for="email">Email: </label>
<input type="email" name="email" id="email" placeholder="name#mail.com" required/>
</div>
<div>
<label for="message">Message: </label>
<textarea name="message" id="message" rows="5" cols="30"></textarea>
</div>
<div>
<input id="submit" type="submit" name="submit" value="submit" />
</div>
</form>
<p id="feedback"><?php echo $feedback; ?></p>
</div>
MY PHP:
<?php
$to = 'example#gmail.com';
$subject = 'Message from The Rocket Factory';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$body = <<<EMAIL
Hi, my name is $name.
$message
From $name
My Address is $email
EMAIL;
$header = "From: $email";
if($_POST){
mail($to, $subject, $body, $header);
$feedback = 'Thanks for your message';
}
?>
PHP script that you create will return an empty page, because that script just to send email. I think you need to combine PHP script and HTML script together with PHP script in top of script to get that you want and edit form action to empty like this sample:
<?php
$to = 'example#gmail.com';
$subject = 'Message from The Rocket Factory';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$body = <<<EMAIL
Hi, my name is $name.
$message
From $name
My Address is $email
EMAIL;
$header = "From: $email";
if($_POST){
mail($to, $subject, $body, $header);
$feedback = 'Thanks for your message';
}
?>
<div>
<form id="form_id" name="form_name" action="" method="post">
<div>
<label for="name">Name: </label>
<input type="text" name="name" id="name" placeholder="John Smith" required/>
</div>
<div>
<label for="email">Email: </label>
<input type="email" name="email" id="email" placeholder="name#mail.com" required/>
</div>
<div>
<label for="message">Message: </label>
<textarea name="message" id="message" rows="5" cols="30"></textarea>
</div>
<div>
<input id="submit" type="submit" name="submit" value="submit" />
</div>
</form>
<p id="feedback"><?php echo $feedback; ?></p>
</div>
Your form will take the user to scripts/index.php. You are echoing the '$feedback' var on the page with the HTML form. Redirect from scripts/index.php using
header("location: filelocation");
exit();
You can achieve this in two ways :
1. Have php and html code in one page.
2. Use ajax to submit your form.
<div>
<form id="form_id" name="form_name" action="scripts/index.php" method="post">
<div>
<label for="name">Name: </label>
<input type="text" name="name" id="name" placeholder="John Smith" required/>
</div>
<div>
<label for="email">Email: </label>
<input type="email" name="email" id="email" placeholder="name#mail.com" required/>
</div>
<div>
<label for="message">Message: </label>
<textarea name="message" id="message" rows="5" cols="30"></textarea>
</div>
<div>
<input id="submit" type="submit" name="submit" value="submit" />
</div>
</form>
</div>
<?php
$to = 'example#gmail.com';
$subject = 'Message from The Rocket Factory';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$body = <<<EMAIL
Hi, my name is $name.
$message
From $name
My Address is $email
EMAIL;
$header = "From: $email";
if($_POST){
mail($to, $subject, $body, $header);
$feedback = 'Thanks for your message';
echo '<p id="feedback">'.$feedback.'</p>'; <-- Notice this..
}
?>
You can also use ajax in jquery ($.ajax) or javascript.