Processing a form on the same page - php

So, I've been messing with this for a day now and I've read a lot tutorials and even posts on stackoverflow. I know it's possible to have all your php form processing steps on the same page as your form, but for some reason, my form won't send an email. Any help?
<div id="mc_embed_signup">
<form method="post" action="index.php" enctype="text/plain" name="emailform">
<input type="text" name="name" placeholder="First Name or Alias"><br>
<input type="text" name="email" placeholder="Email Address"><br>
<textarea name="message" rows="10" size="50" placeholder="Placeholder Text."></textarea><br>
<input type="submit" value="Submit →" name="submit" class="button round">
</form>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['email'];
$to = 'email#address.com'; //set to the default email address
$subject = 'Hello';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
$headers = "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
if($_POST['submit']) {
mail ($to, $subject, $body, $headers); //mail sends it to the SMTP server side which sends the email
echo "<p>Your message has been sent!</p>";
}
else {
echo "<p>Something went wrong, go back and try again!</p>";
}
?>
</div>

Remove the enctype="text/plain" that's a main factor.
Plus, you also need to check if fields were left empty using (if)empty().
Not doing so, you may receive emails that either do not contain the user's email, name, message or a combination of all.
Assuming this file is called index.php if not, use action=""
<div id="mc_embed_signup">
<form method="post" action="index.php" name="emailform">
<input type="text" name="name" placeholder="First Name or Alias"><br>
<input type="text" name="email" placeholder="Email Address"><br>
<textarea name="message" rows="10" size="50" placeholder="Placeholder Text."></textarea><br>
<input type="submit" value="Submit →" name="submit" class="button round">
</form>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['email'];
$to = 'email#example.com'; //set to the default email address
$subject = 'Hello';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
$headers = "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
if(isset($_POST['submit']) && !empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['message']) ){
// check if mail was sent
if(mail ($to, $subject, $body, $headers)){ //mail sends it to the SMTP server side which sends the email
echo "<p>Your message has been sent!</p>";
}
else {
echo "<p>Mail was not sent. Check your logs.</p>";
}
}
else {
echo "<p>Something went wrong, go back and try again!</p>";
}
?>
</div>

This might help,
if(isset($_POST['submit'])) {
if(empty($_POST[name]) && empty($_POST[email]) && empty($_POST[message])){
echo 'Fields cannot be empty';
} else{
$name = $_POST['name'];
$to = $_POST['email'];
$subject = "Hello";
$message = $_POST['message'];
$from = $_POST['email'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
mail ($to, $subject, $body);
}
}else{
echo "<p>Something went wrong, go back and try again!</p>";
}

Related

Wordpress Contact Form does not work - probably due to code in functions.php

Below is the code to my wordpress contact form, probably there is some mistake because when I enter my page url http://muzykablog.pl/ the url jumps automatically to http://muzykablog.pl/page-kontakt.php?msg_sent=true ...
This is the code inside my contact page (page-kontakt.php) :
<h4>Send Us Mail</h4><br/>
<?php
if ($_GET[msg_sent]=='true' ) {
echo '<div>Your message has been sent!</div>';
}elseif ($_GET[msg_sent]=='false') {
echo '<div>An error occurred sending your message.</div>';
}else{
?>
<form method="post" action="functions.php">
<label>Name</label>
<input name="name" placeholder="Type Here">
<label>Email</label>
<input name="email" type="email" placeholder="Type Here">
<label>Message</label>
<textarea name="message" placeholder="Type Here"></textarea>
<label>*What is 2+2? (Anti-spam)</label>
<input name="human" placeholder="Type Here">
<input id="submit" name="submit" type="submit" value="Submit">
</form>
<?php } ?>
This is the code inside my functions page (functions.php) :
// KONTAKT - MESSAGE SENDING FUNCTIONS FOR page-kontakt.php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: http://muzykablog.pl/';
$to = 'piterdeja#gmail.com';
$subject = 'Hello';
$human = $_POST['human'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if(mail($to, $subject, $body, $from)){
header('Location:page-kontakt.php?msg_sent=true');
}else{
header('Location:page-kontakt.php?msg_sent=false');
}
There has to be some mistake here
It could be better to see the mail() function. But seeing the code it seems it will always return true or false so it will redirect to ?msg_sent=false or ?msg_sent=true. For some reason your page is always sending the form when loads. You must avoid that. Its rare too that the action of the form is functions.php. Are you trying to put the form in the home page?
I think there are more deep problems but it could be arranged (not very pro) with this in your functions.php
if($_POST['human']!=""){
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: http://muzykablog.pl/';
$to = 'piterdeja#gmail.com';
$subject = 'Hello';
$human = $_POST['human'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if(mail($to, $subject, $body, $from)){
header('Location:page-kontakt.php?msg_sent=true');
}else{
header('Location:page-kontakt.php?msg_sent=false');
}
}

Error in creating contact form with php

I created a simple contact form as follows.
<form method="post" action="mail_receive.php">
<label>Name *</label>
<input name="name" placeholder="Type Here">
<label>Email *</label>
<input name="email" type="email" placeholder="Type Here">
<label>Phone No *</label>
<input name="phone" placeholder="Type Here">
<input id="submit" name="submit" type="submit" value="">
And php file called mail_receive.php
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$from = 'From: Someone';
$to = 'admin#gmail.com';
$subject = 'Ticket Ordering';
$body = "From: $name\n E-Mail: $email\n Phone:\n $phone";
if (isset($_POST['submit'])) {
if ($name != '' && $email != '' && $phone != '') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else {
echo '<p>Please fill all required fields !!</p>';
}
}?>
}
And i uploaded to my hosting.
Although I can fill up the form and submit, I can't receive email to my inbox.
Is there any wrong?
With mail, it's meant to be
mail($to, $subject, $message, $headers);
Try something like this:
$to = "youremailaddress#whatever.com";
$subject = "Your tickets yo";
$headers = "From: someone#someone.com\r\n";
$headers .= "Reply-To: someone#someone.com\r\n";
$headers .= "MIME=VERSION:1.0\r\n";
$headers .= "Content-Type: text/plain; charset=UTF-8\r\n;"
$message = "Nice tickets bro.";
mail($to, $subject, $message, $headers);
And please I hope it's just for example purposes, but please don't actually be sending it to admin#gmail.com because obviously (unless you ARE actually admin#gmail.com, you wont recieve the e-mail.

Contact form is not working?

I'm trying to create a simple contact page however when I submit the form, it leads me to the blank page contact-form.php. I used this article as reference: http://tangledindesign.com/how-to-create-a-contact-form-using-html5-css3-and-php/
What am I doing wrong?
Code on index.php (corner divs are purely for styling purposes)
<form action="contact-form.php" method="post" autocomplete="off" id="contact">
<div class="corner"></div>
<input type="text" required name="name" placeholder="Name" value="">
<div class="corner"></div>
<input type="text" required name="email" placeholder="Email" value="">
<div class="corner"></div>
<input type="text" required name="check" placeholder="Question of the day: What's 2 + 2 ?" value="">
<div class="corner"></div>
<textarea name="message" rows="25" cols="50" placeholder="Drop me a line!"></textarea>
<button class="send" type="submit" name="submit">Send</button>
</form>
Code on contact-form.php
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: MessageAnita';
$to = 'myemail#gmail.com';
$subject = 'Hello';
$check = $_POST['check'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit'] && $check == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if ($_POST['submit'] && $check != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
?>
change the button tag to an input tag
< input type ="submit" name="submit"/>
In your form, change:
<button class="send" type="submit" name="submit">Send</button>
to:
<input type="submit" name="submit" value="Submit">
Plus, you may want to use this PHP mailing method, since the mail came into my SPAM when testing.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['email'];
$to = 'myemail#gmail.com';
$subject = 'Hello';
$check = $_POST['check'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
$headers = "From: $from" . "\r\n" .
"Reply-To: $from" . "\r\n";
if ($_POST['submit'] && $check == '4') {
if (mail ($to, $subject, $body, $headers)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if ($_POST['submit'] && $check != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
?>
And if you want to send as HTML use:
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\n";
$headers .= "From: $from" . "\r\n" .
"Reply-To: $from" . "\r\n";

Simple HTML/PHP Contact Form Not Working

I am trying to create simple HTML/PHP Contact form, But this is not working correctly. Pl check this below given code that's I'm actually trying.
My simple form process
HTML form,
<form action="send_mail.php" method="post" onsubmit="return validateForm();" name="myform" >
<input type="hidden" name="recipient" value="email">
<input type="hidden" name="subject" value="FormMail E-Mail">`
<table cellspacing="2" cellpadding="2" border="0">
<tr>
<td align="left">Nome:
<input type="text" name="name" id="name" size="50"></td>
</tr>
<tr>
<td align="left">Email:
<input type="text" name="email" id="email" size="50"></td>
</tr>
<tr>
<td align="left">Mensagem</td></tr>
<tr>
<td><textarea style="resize:none;" cols="44" rows="8" name="message" id="message"></textarea></td>
</tr>
<tr>
<td align="left">
<input name="submit" type="submit" value="Aceder"><input type="reset" value="Recompor">
<input type="hidden" name="redirect" value="thankyou.html"></td>
</tr>
</table>
</form>
PHP Code
if(isset($_POST['submit']))
{
$to = "xyz#abc.com";
$subject = "Email from Xyz company";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
mail($to, $subject, $body);
}
else
{
echo "Failure!";
}
?>
I am using this code, plz suggest me if there is any error in my code and any changes required..
Thank you.
quotation marks end on subject?
if(isset($_POST['submit']))
{
$to = "xyz#abc.com";
$subject = "Email from Xyz company; // quotation marks end?
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
mail($to, $subject, $body);
}
else
{
echo "Failure!";
}
?>
In your PHP Code on the line 4 the quotation marks are missing at the end of the line before ;.
Correct line 4:
$subject = "Email from Xyz company";
Change above PHP code to this;
<?php
if(isset($_POST['submit']))
{
$to = "xyz#abc.com";
$subject = "Email from Xyz company";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
mail($to, $subject, $body);
}
else
{
echo "Failure!";
}
?>
Try this
<?php
if(isset($_POST['submit']))
{
$to = "xyz#abc.com";
$subject = "Email from Xyz company";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf8' . "\r\n";
if( !mail($to, $subject, $body, $headers) )
{
echo "Failure!";
}
}
?>

PHP email form CC

I have a very simple contact form on my site, Im looking for users to be able to just put a check mark next to "CC:" to have it CC them without creating a whole new field for that they have to fill out again.
Here is the HTML:
<form action="send.php" method="post" name="form1">
Name: <input name="name" size="35"><br/>
E-mail:<input name="email" size="35"><br/>
CC: <input input type="checkbox" name="mailcc"><br/>
Comment: <textarea cols="35" name="comment" rows="5"></textarea> <br />
<input name="Submit" type="submit" value="Submit">
</form>
And here is the PHP:
<?php
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$comment = $_REQUEST['comment'] ;
mail( "me#me.com", "Message Title", "Name: $name\n Email: $email\n Comments: $comment\n " );
echo "Message Sent! Thanks!"
?>
Ive been trying to add some items from this site:
http://w3mentor.com/learn/php-mysql-tutorials/php-email/send-email-with-cccarbon-copy-bccblind-carbon-copy/
But it wants to create a text field for CC which means the user would have to enter their email twice.
Ive also tried $mailheader.= "Cc: " . $email ."\n"; but I cant get that to work either.
Make the checkbox have a value (value="1") in HTML.
Add a variable ($mailheader) to the end of mail() function, as the last parameter.
So essentially:
$name = $_POST['name'] ;
$email = $_POST['email'] ;
$comment = $_POST['comment'] ;
if ($_POST['mailcc'] == 1) {
$mailheader .= "CC: $name <$email>";
}
mail("me#me.com", "Message Title", "Name: $name\n Email: $email\n Comments: $comment\n ", $mailheader);
echo "Message Sent! Thanks!";
Is the Cc address you are testing with the same as the "to" address(me#me.com on your example)?
I did a quick test and with this code i get only one mail:
<?php
$to = "my#address.com";
$subject = "Testing";
$message = "Testing message";
$headers = "Cc: my#address.com";
mail($to, $subject, $message, $headers);
But with this i get a copy to my other email account:
<?php
$to = "my#address.com";
$subject = "Testing";
$message = "Testing message";
$headers = "Cc: my#otheraddress.com";
mail($to, $subject, $message, $headers);

Categories