Having some troubles with a contact form.
When pressing submit I get redirected to http://minerva.hivolda.no/~oleav/eksamensandkasse/kontakt/ (same page), and recieve a message that my site was not found etc.
function haugsdalen_kontaktplugin () {
function haugsdalen_kontakt_header () {
echo ('<link rel="stylesheet" type="text/css" href="'.plugin_dir_url( __FILE__ ).'haugsdalen-kontakt.css">');
}
$from = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = 'MYMAIL';
$subject = 'Ny melding fra Haugsdalen Skisenter';
$body = "Ny melding fra Haugsdalen Skisenter:\n Fra: $name\n E-post: $email\n Melding:\n $message";
echo ('<div id="kontakthead">');
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Din melding har blitt sendt!</p>';
} else {
echo '<p>Noe gikk galt. Vennligst prøv igjen.</p>';
}
}
echo ('<form method="post" action="http://minerva.hivolda.no/~oleav/eksamensandkasse/kontakt/">
<h2>Kontakt</h2>
<label>Navn</label>
<input name="name" placeholder="Ditt navn">
<label>E-post</label>
<input name="email" type="email" placeholder="Din e-post">
<label>Melding</label>
<textarea name="message" placeholder="Din melding..."></textarea>
<input id="submit" name="submit" type="submit" value="Send inn"></form><br/>
<h3><strong>Kontaktinformasjon:</strong></h3>
Tlf: 73 85 46 05<br/>
E-post: web#haugsdalen.com<br/>
</div>');
}
Any suggestion?
Page can be found here: http://minerva.hivolda.no/~oleav/eksamensandkasse/kontakt/
EDIT:
I recieve my messages now, but the messages just contains:
Ny melding fra Haugsdalen Skisenter: Fra: E-post: Melding:
However I just recived one mail with some text:
E-post: 2#2.co Melding: 3
There is error somewhere else in your code related to the $_POST['name'] variable. When I delete this DOM node (input with name="name") and submit data everything working as expected, I've got mail function executed and your script echoed Din melding har blitt sendt!.
It seems that this script or some of your included scripts generate 404 error exception when you send any unexpected $_POST['name'] data.
I suggest you to try renaming this name="name" to name="from", for example.
Could you post the whole code of the webpage? We need to see the function calls.
Also,
Add required to each input field to reduce the number of spams or use some captcha mecanism.
Related
This question already has answers here:
Php form failing
(1 answer)
Simple form not sending data via _POST [duplicate]
(4 answers)
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 3 years ago.
My contact form in php doesnt send the message to the mail so i need to know whats the problem here
You will find the html form here with file name : index.php
and php form with name : mail.php
<form class="form" action="mail.php" method="post" name="contactform">
<input class="name" type="text" placeholder="Name" name="name">
<input class="email" type="email" placeholder="Email" name="email" >
<input class="phone" type="text" placeholder="Phone No:" name="phone">
<textarea class="message" id="message" cols="30" rows="10" placeholder="Message"name="message" ></textarea>
<input class="submit-btn" type="submit" value="Submit">
</form>
<?php
if (isset($_POST['submit']) ) {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$from = 'From: phone';
$to = 'modysaid26#gmail.com';
$subject = 'message';
$body = "From: $name\n E-Mail: $email\n Phone Number: $phone\n Message:\n $message";
if (isset($_POST['submit'])) {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been submitted</p>';
} else {
echo '<p>Something went wrong, please try again!</p>';
}
}
}
?>
<input class="submit-btn" name='submit' type="submit" value="Submit">
You are missing to add the name to submit button so your case if (isset($_POST['submit']) ) { fails
you dont need to put name on the form tag remove the class either:
<form action="mail.php" method="post">
First yor submit button name is missing, please use a
<input class="submit-btn" type="submit" value="Submit" name="submit">
The second you email command ( mail ($to, $subject, $body, $from) ) has no right email header. Insead your $from
please define header with following parameters
$email_headers = "From: ".$from_name." <".$from_email.">\r\n".
"Reply-To: ".$reply_to."\r\n" ;
if ($cc) $email_headers.="Cc: ".$cc."\r\n";
if ($bcc) $email_headers.="Bcc: ".$bcc."\r\n";
$email_headers.="MIME-Version: 1.0" . "\r\n" .
"Content-type: text/html; charset=UTF-8" . "\r\n";
$email_body=$_POST['message'];
and then send it using
mail($to, $subject, $email_body, $email_headers);
And then your email shouldbe send properly.
I have searched and searched for the answer and cannot find the answer to my particular problem. I am simply trying to send an email when a user fills out a contact page and clicks submit.
Below here is the code for the form.
<form method="post" action="contactRob.php">
<label for="name">First Name:</label>
<input type="text" name="name" id="name" />
<label for="lastName">Last Name:</label>
<input type="text" name="lastName" id="lastName" />
<label for="email">Email:</label>
<input type="text" name="email" id="email" />
<label for="message">Message:</label><br />
<textarea name="message" rows="20" cols="20" id="message"></textarea>
<label>*What is 2+2? (Anti-spam)</label>
<input name="human" placeholder="Type Here">
<input type="submit" name="submit" value="Submit" class="submit-button" />
</form>
And below here is the PHP I am using.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['lastName'];
$to = 'juliansilvestri92#gmail.com';
$subject = 'Hello';
$human = $_POST['human'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit'] && $human == '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'] && $human != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
?>
What am I missing?
Currently this website is live on a hosted server domain.
When I click the submit button I am getting 'something went wrong' error throw that I placed.
Any help would be greatly appreciated.
Your names are first capital while results arent.
$message = $_POST['email'];
<input type="text" name="Email" id="Email" />
make both lowercase. Then echoing $body could help, see if the data is the same as you inputed, comment out the mailer part, see if the error comes from mail or from post DOM itself. If its mailer problem that's causing you the error, read about mailer a bit more, if that doesn't help.
and your from is incorrect.
$to = "somebody#example.com";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: webmaster#example.com" . "\r\n" .
"CC: somebodyelse#example.com";
mail($to,$subject,$txt,$headers);
Your from should look like this:
$from = "From: ".$_POST['email']."\r\n CC: ".$_POST['lastName'];
or something like that.
There is all ready similar topic about it, and there is a honest huge comment, you wont miss it.
PHP mail function doesn't complete sending of e-mail
Do what that comment is telling you to do, and you will most likely solve or understand the problem.
Good luck..
Most of the server not allow you to send email using just php mail function use php mailer function to send mail from any where local development server/ live web server.
https://github.com/PHPMailer/PHPMailer
also for default mail send you can use these params
$to = "somebody#example.com";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: webmaster#example.com" . "\r\n" .
"CC: somebodyelse#example.com";
mail($to,$subject,$txt,$headers);
I have a contact form on my website. I'd like to change the text of the submit button to say "submitted" after the form has successfully submitted, and maybe even make it say "submitting" while the form is submitting. I am unsure of how to do this, i could do an onclick event that would change the text, but not the route i want to take as the message could fail to send and the button would still say submitted.
Here is my html for the form
<form method="post" action="contact.php">
<input type="text" name="name" placeholder="Name"><br>
<input type="email" name="email" placeholder="Email"><br>
<textarea rows="8" cols="65" name="message"placeholder="Message"></textarea><br>
<input id="submit" type="submit" name="submit" value="Let's Get In Touch">
</form>
and here is my php code:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: Portfolio Website';
$to = 'kyle.a.binger#gmail.com';
$subject = 'Message From Personal Site';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
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>';
}
}
?>
Is there a way to do this with my existing php code? Thanks in advance for any help.
What you are going to need is something to pass the data to the php script, and return something/echo something back without leaving the page.
Take a look into AJAX. You will be able to exactly this.
Here's a link to one of the first posts on stackoverflow that showed up.
Here's a link to w3schools to give you a quick example/idea.
If you don't want to use AJAX and you're posting to page itself you can do the following
<form method="post" action=""> <!-- removed the PHP file name to post to itself -->
<input type="text" name="name" placeholder="Name"><br>
<input type="email" name="email" placeholder="Email"><br>
<textarea rows="8" cols="65" name="message"placeholder="Message"> </textarea><br>
<?php
if (isset($_POST['submit'])) {
echo '<input id="submit" type="button" name="submit" value="Submitted">'; //Changed type submit to button
} else {
echo '<input id="submit" type="submit" name="submit" value="Let\'s Get In Touch">';
}
?>
</form>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: Portfolio Website';
$to = 'kyle.a.binger#gmail.com';
$subject = 'Message From Personal Site';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
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>';
}
}
?>
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');
}
}
I am beginner in php and I tried to programe an easy contact form. My question is: whats wrong with my code? I cant seem to find any mistake with formating or anything... This is my code:
contact.php:
...page content...
<div class="contact">
<form action="send_mail.php" method="post">
<fieldset>
<label for="name">Name:</label>
<input type="text" name="name" placeholder="Enter your name" />
<label for="email">Email:</label>
<input type="email" name="email" placeholder="Enter your email address" />
<label for="message">Message:</label>
<textarea name="message" placeholder="What's on your mind?"></textarea>
<br>
<input type="submit" name="submit" value="Send message" />
</fieldset>
</form>
</div>
...page content...
send_mail.php:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: My website';
$to = 'someone#email.cz';
$subject = 'My contact form message';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
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>';
}
}
?>
I am getting the "Something went wrong" message, which means taht mail() returned false but I dont know why. Thanks for any help!
This is a common problem with running scripts on your local computer. Most computers will not have an email server and be configured to send email from that server.
For the PHP mail function to work, your computer must have an email server set up.
You can either look at online hosting with email support or for local testing, install a test mail server.
There is a bit about non mac OS's at the end. I've had some luck with this in the past.
If your on windows, this is probably better: http://www.toolheap.com/test-mail-server-tool/
Your headers need to contain proper headers.
Using only From: My website stands at going to Spam.
Using an Email => From: $email\r\n will work better.
Give this a try, tested.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$headers = "From: $email\r\n";
$to = 'your_email#somewhere.com';
$subject = 'My contact form message';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if(empty($email)) {
die("Something went wrong, go back and try again!");
}
elseif (isset($_POST['email'])) {
mail($to,$email,$body,$headers);
echo "<p>Your message has been sent!</p>";
}
?>
1 . To made work this code.you should send your mail from any domain.
for example: you should have a live website thru that domain email id only send mail to
others
2 . use PHP-MAILER coding for sending mail.just download from net and make changes and use