This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Reference - What does this error mean in PHP?
(38 answers)
How do I get PHP errors to display?
(27 answers)
Closed 3 years ago.
I'm having some difficulty getting my contact form working on my website. It opens a blank page and actually doesn't work at all. My code is below:
<form action="mail.php" method="post" class="comment-form">
<input name="name" type="text" placeholder="Your Name" required>
<input name="email" type="email" placeholder="Email">
<input type="url" placeholder="Website">
<textarea rows="4" placeholder="Messages"></textarea>
<input type="submit" value="send message">
</form>
And php code:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: Website';
$to = 'me#me.com';
$subject = 'Email Inquiry';
$header = "From: noreply#example.com\r\n";
$header.= "MIME-Version: 1.0\r\n";
$header.= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$header.= "X-Priority: 1\r\n";
mail($to, $subject, $message, $headers);
header("location: ../contact");
?>
Use this to send your cod to the php instead of just refreshing
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method='POST'>
There is nothing in your form with the name 'submit'. You have type="submit". So your code is not going in the if the condition of if($_POST['submit']).
You should check for something else. Like: if ($_POST['name']) or if ($_POST['email']).
Also, I would suggest having an else block for your - if ($_POST['name']) so you can show some message instead of getting a blank page.
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.
This question already has answers here:
PHP code is not being executed, but the code shows in the browser source code
(35 answers)
Closed 4 years ago.
I want to make a website(small one) I want to make people able to send write their username and password to be sent to my email(because I am still developing a database and I want to make sure if it will work or not)
I tried to put php in the same file as html but when it processes the code it just shows the code but dooesn't run it
<html>
<head>
<?php
if(isset)($_POST['name']) && isset($_POST['email'])) {
$pass = $_POST['pass'];
$email = $_POST['email'];
$to = 'omar12haytham#gmail.com';
$subject = 'Password';
$body = '<html>
<body>
<h2>Hello</h2>
<hr>
<p>Email<br>' . $email . '</p>
<p>Password<br>' . $pass . '</p>
</body>
</html>';
//headers
$headers = "From: ".$name." <".$email.">\r\n";
$headers .= "Reply-To: ".$email."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=utf-8";
//send
$send = mail($to, $subject, $body, $headers);
if ($send) {
echo '<br>';
echo 'Your email or password is incorrect';
} else {
echo error;
}
}
</head>
?>
<body>
<form action="" method="post">
<input type="text" name="name" placeholder="Write your email"><br>
<input type="password" name="email" placeholder="Write your password"><br>
<button type="submit">Log in</button>
</form>
</body>
</html>
I expected it to send me an email but it doesn't must I launch it? I am testing it as .html file not as a working website.
This piece of code doesn't work: isset)($_POST['name']) use instead if(isset($_POST["name"]))
This question already has answers here:
How do I send email on heroku using PHP?
(2 answers)
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 4 years ago.
I'm trying to make my form work so I can receive emails. My website is currently stored on Heroku and everything looks fine, except it doesn't send emails after the form is submitted. Can someone help me please?
index.php :
<form id="contact-form" method="post" action="email.php">
<input class="holder" placeholder="Name" type="text" name="name" required>
<input class="holder" placeholder="Email" type="email" name="email" required>
<textarea class="holder" placeholder="Drop me some lines" type="text" name="message"></textarea>
<div class="button-position">
<button id="button-submit" type="submit"><p id='submit_word'>Submit</p><img class="send-spaceship" src="resources/css/images/send-spaceship.png"></button>
</div>
</form>
email.php:
<?php
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
$email_from = "itzikshaoulian#gmail.com";
$email_subject = "Form Submission";
$email_body = "User Name: $name.\n".
"User Email: $visitor_email.\n".
"User Message: $message.\n";
$to = "itzikshaoulian#gmail.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
mail($to,$email_subject,$email_body,$headers);
header("Location: https://itzikshaoulianportfolio.herokuapp.com/");
?>
I have created a simple html/php form where visitors on my site can write their name, email and message and then send the message to my email. Problem is that when they submit the email, my site then performs a full refresh (it looks like) and therefore just reloads to the top of my site. I would like for the user to remain at the same scroll position after submit, so that they can instantly see whether the submit was succesful or not. So either a solution that prevents the refresh or some other solution that automatically scrolls down vertical to the form.
Can you tell me if this is possible using php? Or do I have to use some jquery/ajax solution?
Below is the code I am using. I am a complete novice, so please be gentle.
<form action="" method="post" id="form">
<div class="contact-info-group">
<label for="name"><span>Your name</span>
<input type="text" id="name" name="name" autocomplete="off" value="<?php echo $name; ?>"></label>
<label for="email"><span>Your email</span>
<input type="email" id="email" name="email" autocomplete="off"></label>
</div>
<label for="message"><span>Your message</span>
<textarea id="message" name="message"></textarea></label>
<input id="button1" type="submit" class="button next" name="contact_submit" value="Send message">
<?php
// Check for header injections
function has_header_injection($str) {
return preg_match("/[\r\n]/", $str);
}
if (isset ($_POST['contact_submit'])) {
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$msg = $_POST['message'];
// Check to see if $name or $email have header injections
if (has_header_injection($name) || has_header_injection($email)) {
die();
}
if (!$name || !$email || !$msg) {
echo '<div class="contact-warning"><h2>! Error - Please note that all of the above fields are required !</h2></div>';
exit;
}
// Add the recipient email to a variable
$to = "email#email.com";
// Create a subject
$subject = "Message via website.com - $name";
// Construct the message
$message = "Name: $name\r\n";
$message .= "Email: $email\r\n";
$message .= "Message: \r\n\r\n$msg";
// Clean up the message
$message = wordwrap($message, 72);
// Set the mail headers into a variable
$headers = "MIME-Version 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "From: $name <$email> \r\n";
$headers .= "X-Priority: 1\r\n";
$headers .= "X-MSMail-Priority: High\r\n\r\n";
// Send the email
mail($to, $subject, $message, $headers);
echo '<div class="contact-warning"><h2>Thank you for your message. We will get back to you shortly.</h2></div>';
}
?>
</form>
I've an HTML contact form and I want to use it in my footer, but I don't know how to do it!!
Here is my form:
<div class="footer-right">
<p>Contact Us</p>
<form action="#" method="post">
<input type="text" name="email" placeholder="Email" />
<textarea name="message" placeholder="Message"></textarea>
<button>Send</button>
</form>
</div>
The <button> tag will not submit the form. You should change it to: <button type="submit">Send</button>
For handling the form you could add something like this in the beginning of your file::
<?
//Mail sending function
$subject = "Web Form";
$from = $_POST['email'];
$to = "youremail#example.com";
//data
$msg = $_POST['message'];
//Headers
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=UTF-8\r\n";
$headers .= "From: <".$from. ">" ;
mail($to,$subject,$msg,$headers);
echo "Mail Sent.";
?>
Since you tagged it as PHP, this answer will get you started.
Change:
<button>Send</button>
to:
<input type="submit" value="Send">
Change:
<form action="#" method="post">
to:
<form action="mailer.php" method="post">
Create a file named mailer.php and place it in the same folder as the filename of the document you presented, and in that file, add the following contents:
<?php
$recipient="receipient#example.com";
$subject="new message from ".$_POST['email']." - contact form";
$message=$_POST['message'];
mail($recipient, $subject, $message);
?>
<p>Your message has been sent.</p>
Note that you should add custom HTML between the last two lines of the above code or your HTML won't validate with W3C standards.