I'm trying to create a PHP form that allows users to insert their email address and it automatically sends me an email with their email address. something like a subscription.
what I have so far is this:
<form action="" method="post">
<input type="email" name="email" placeholder="Enter your email address" /><br>
</form>
I found this PHP sample that I believe answers my problem, but I have no idea how to call it from my HTML.
<?php
if(isset($_POST['email'])){
$email = $_POST['email'];
$to = 'myemail#something.com';
$subject = 'new subscriber';
$body = '<html>
<body>
<p>Email:<br>'.$email.'</p>
</body>
</html>';
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset-utf-8";
$send = mail($to, $subject, $body, $headers);
if($send){
echo '<br>';
echo 'thanks';
}else{
echo 'error';
}
}
?>
There's insufficient code for me to be able to answer completely, but the one thing that comes immediately to my mind is not leaving action="" empty. Try $_SERVER['PHP_SELF'] variable, it should print the path to the file that is currently running so you'll be presented with the same page, but with data in $_POST you'll send. You can try it like this:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="email" name="email" placeholder="Enter your email address" /><br>
</form>
If you wish to send data to the same file like this, please make sure your PHP code is in the same file as the HTML structure of your form. It may make things easier if you put your PHP code first, so you can exit; from the file (not displaying the form anymore) telling the user that the message has been sent or that the error has occured.
Related
I am trying to get a simple two-field form to submit to an email address and then echo a "thanks for registering your interest" below the form (or instead of the form).
FYI, this is on a WordPress template file.
Here is the code, including the form:
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"
method="POST" autocomplete="on" id="register-form">
<input type="text" name="name" placeholder="Name"/>
<input type="email" name="email" placeholder="Email address"/>
<button type="submit" name="submit" class="button">Send
<img src="<?= get_image('icon-arrow-right-tiny.svg'); ?>"/></button>
</form>
<?php
if (isset($_POST['submit'])) {
// validate the email address first
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
// process the form only if the email is valid
if ($email) {
$to = 'info#example.com'; // where you want to send the mail
$from = 'info#mydomain.com';
$subject = 'Website Submission';
$message = 'Name: ' . $_POST['name'] . "\r\n\r\n";
$message .= 'Email Address: ' . $_POST['email'] . "\r\n\r\n";
$headers = "From: $from\r\nReply-to: $email";
$sent = mail($to, $subject, $message, $headers);
} ?>
<p style='color: #fff; font-size: 14px;'>Thank you for registering your interest.</p>
<?php
}
?>
At the present time, the form does get sent, and the page does echo "Thank you for registering your interest" underneath the form, however it does not seem to be returning us to the correct page when you click the submit button.
Any ideas?
Thank you for all of your contributions. I have worked out the problem, and will share here for anybody else who comes here to find the answer.
WordPress has something important reserved for the "name" parameter, and thus you can't use it in PHP-based forms. Changing the parameter name from "name" to something else resolved the issue.
Additionally, WordPress also has the following names reserved and you cannot use them in forms - "day" "month" and "year".
I have check your code i think you have use color code #fff i.e. for the message.
Please try to make black or any other color rest of code are working.
:)
Thank you for registering your interest.
You have to put a php code below your Thank you message.
header("location:$_SERVER["PHP_SELF"]);exit;
So I want my contact form to work on my site so I wrote some php to make it work. Here is the code: (form_process.php)
<?php
$name = $_POST('name');
$company = $_POST('company');
$email = $_POST('email');
$message = $_POST('message');
$to ="arp2222#yahoo.com";
$subject="New Message from Kincentive";
mail($to, $subject, $message, "From: ".$name);
echo "Your Message has been sent";
?>
I want to know how I can make this php work with my html file. I put the php file in the root folder with the index.html file and I believe I need to set up a form tag. I believe I need to use the action or method attribute? to setup as
for example.
I am using MAMP PRO as a local host since my site is not live yet and I want to test the contact form and recieve the test to my email.
Any help please i am new to php
in sendEmail.html you should write code as given
<form name="frmEmail" id="frmEmail" action="sendEmail.php" method="post">
<input type="text" name="fName" id="fName">
<input type="text" name="email" id="email">
<input type="text" name="company" id="company">
<textarea name="message" id="message"></textarea>
<input type="submit">
</form>
this form redirect to sendEmail.php
<?php
$name=$_POST['fName'];
$company=$_POST['company'];
$message=$_POST['message'];
$to =$_POST['email'];
$subject="New Message from Kincentive";
mail($to, $subject, $message, "From: ".$name);
echo "Your Message has been sent";
?>
$_POST is an array, so you should reference it like this, using [ brackets instead of curly ones.
$name = $_POST['name'];
$company = $_POST['company'];
$email = $_POST['email'];
$message = $_POST['message'];
In your HTML, wrap your inputs in a form like this, pointing to your Php file:
<form action="form_process.php" method="POST">
<-- input elements here !-->
</form>
I am using PHP to send data to an email address from a HTML form. It worked fine while the PHP file was a pure PHP file, displaying the confirmation text upon submitting the form. However, I needed the confirmation text to appear within our usual templates so I added the same PHP into the body of a page and set the form action to go to that page. When someone now submits the form, an email does get sent but it contains none of the information from the form. Can you help?
HTML:
<form method="post" action="thank-you-page.html">
Email: <input name="email" type="text"><br />
Name: <input name="name" type="text"><br />
<h3>Your message</h3>
Subject: <input name="subject" type="text"><br />
Message:<br /> <textarea name="message" rows="15" cols="40"></textarea><br />
<input type="submit" />
</form>
PHP within body of thank-you-page.html:
<?php
$to = "myemail#email.com";
$subject = 'Feedback from online form';
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
$headers = "From: $email";
$sent = mail($to, $subject, $message, $headers) ;
if($sent)
{print 'Your mail was sent successfully. Thank you for your feedback.'; }
else
{print 'We encountered an error sending your mail.'; }
?>
Thank you!
Your thank you page needs to be a PHP page, not just an HTML page.
Change it to be thank-you-page.php
I've got a very simple PHP contact form, containing Email and Message.
I would like to add a functionality so that every time the contact form is sent I know the URL that it is being sent from, and I'd like to include it into the body of the message that gets sent to my email.
Here's the PHP code that runs the Contact form.
<?php
$to = "email#email.com" ;
$from = "Something Broke!" ;
$subject = "Something Broke!";
$fields = array();
$fields{"emailOptional"} = "Email:";
$fields{"message"} = "Message:";
$body = "We have received the following information:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%s: %s\n",$b,$_REQUEST[$a]); }
if(mail($to, $subject, $body)){
echo 'sent';// we are sending this text to the ajax request telling it that the mail is sent..
}else{
echo 'failed';// ... or to tell it that it wasn't sent
}
?>
And here's the markup:
<form method="post" action="widgetScript.php" id="contactForm">
<input type="text" name="emailOptional" placeholder="Your Email (optional)" />
<textarea rows="5" type="text" name="message" id="message"></textarea><br />
<input type="submit" name="send" id="Submit" value="Send">
</form>
I've found that you can use [_post_url] to get the current URL if I understand correctly - but I'm unsure what to do with it. Would appreciate all the help I could get
In your php code, use $ _SERVER ['HTTP_REFERER'] to get the url the form was submitted from.
For more information on $_SERVER:
http://php.net/manual/en/reserved.variables.server.php
created a simple little php code to populate email with email address and info from a textbox on the form. it originally worked when I was calling to the script from a html form, but once I converted my site to PHP it stopped working. Eventually I would like to put this same info into a Database table but right now I would be content just getting the email to work.
When the form is submitted I get the email address from the customer, but I don't get the info from the textbox. here is my code.
<?php
$email = $_POST['email'];
$message = $_POST['message'];
mail( "sales#sixtoed-design.com", "Service Request", "From: $email",
$message );
header( "Location: http://www.sixtoed-design.com/thankyou.php" );
?>
Like I said it is a very simple code and worked fine before I converted my site completely to PHP.
Below is my code for the form if you need it.
<form method="POST" action="sendmail.php" enctype="multipart/form-data">
Email: <input name="email" type="text" /><br />
Message:<br />
<textarea name="message" rows="15" cols="40">
</textarea><br />
<input name="" type="submit" value="Send Email">
</form>
You're using the mail function wrong. See the docs.
The 3rd parameter should be the message and you send from as a header in the 4th parameter, so:
mail( "sales#sixtoed-design.com", "Service Request", $message, "From: $email" );
See example 2 in the docs