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
Related
After manually submitting a form on my website, an email with the subject appears in my inbox. The email's subject is correctly sent as "Feedback Form Submission", but the email itself is blank. There is no body nor input from the email box on my form.
Form:
<form action="feedback-form.php" method="post" enctype="text/plain">
E-mail:<br>
<input type="text" class="textForm" name="email_address" size="35"><br>
Comment:<br>
<textarea name="feedback" class="textForm" rows="6" cols="35"></textarea><br><br>
<input type="submit" id="submit" class="button" value="Send">
</form>
PHP:
<?php
#Receive user input
$email_address = $_POST['email_address'];
$feedback = $_POST['feedback'];
#Filter user input for invalid characters
function filter_email_header($form_field)
{
return preg_replace('/[nr|!/<>^$%*&]+/', '', $form_field);
}
$email_address = filter_email_header($email_address);
#Send email
$headers = "From: $email_address";
$sent = mail('me#website.com', 'Feedback Form Submission', $feedback, $headers);
I believe my mail function is set up properly. My variables in the form match the variables my PHP script is using. My PHP and HTML are in the same file. I have read in other questions that having the two pieces of code in the same file can lead to issues when a blank form is submitted, but I am having issues while inputting valid information into my form. What am I doing incorrectly here?
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.
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 need make this form send me a email like a contact form:
Script code:
<script type="text/javascript">
$(document).ready(function(){
$("#contactLink").click(function(){
if ($("#contactForm").is(":hidden")){
$("#contactForm").slideDown("slow");
}
else{
$("#contactForm").slideUp("slow");
}
});
});
function closeForm(){
$("#messageSent").show("slow");
setTimeout('$("#messageSent").hide();$("#contactForm").slideUp("slow")', 2000);
}
</script>
HTML CODE:
<div class="box">
<div id="contactFormContainer">
<div id="contactForm">
<fieldset>
<label for="Name">Nome: </label>
<input id="name" type="text" />
<label for="Telefone">Telefone Fixo: </label>
<input type="text" id="phone" maxlength="15" onkeypress="Mascara(this);" />
<label for="Message">Assunto:</label>
<textarea id="Message" rows="3" cols="20"></textarea>
<input id="sendMail" type="submit" name="submit" onclick="closeForm()" />
<span id="messageSent">Sua solicitação foi enviada com sucesso, por favor, aguarde...</span>
</fieldset>
</div>
<div id="contactLink"></div>
</div>
When click and close the form i need send me a email with the content of form, how to?
Some idea? thanks!
Firstly i can't see the form tags in your code. According to me you're doing this wrong and i'm sure many of our friends on stack will agree too.
Your question suggests that you basically want to receive an email with the data submitted through the form. Why don't you try the below method.
HTML
<form action="mail.php" method="POST">
<input type="text" name="fname"></input>
<input type="text" name="lname"></input>
<button>SUBMIT</button>
</form>
PHP
<?php
$firstname = $_POST['fname'];
$lastname = $_POST['lname'];
$to = "someone#example.com";
$subject = "Hello World";
$message = "Firstname: $firstname \n\n Lastname: $lastname";
$from = "sender#example.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
The above example is the most simplest method of sending an email. You can go advance by adding more header information and graphically formatting the email.
Go through these tutorials if you get confused.
http://www.w3schools.com/php/php_mail.asp
http://www.phpeasystep.com/phptu/8.html
And since you mentioned that you want to perform the task via javascript you can try submitting the form via ajax, refer the below tutorials
http://teachingyou.net/php/simple-php-contact-form-using-ajax/
http://www.sitepoint.com/forums/showthread.php?1055068-Send-PHP-email-using-jQuery-AJAX
Since you've tagged the question php, have a look at php's mail function. http://php.net/manual/en/function.mail.php
$to = 'you#domain.com';
$subject = 'Contact Form';
$message = '...' //concatenate the $_POST (or $_GET) variables to create this message
mail($to, $subject, wordwrap($message, 70, "\r\n");
This function requires that your server has a properly configured to send mail - see the php documentation for requirements: http://www.php.net/manual/en/mail.requirements.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