Being so bad at PHP, I've decided to post here as a last resort.
I want to add a "who" variable to the message body of emails sent via PHP contact form. The form works fine for name, email and message but the "who" input I would like to be a part of the email message that comes through, as a way to communicate who is being referred.
I have tried to add $who=$_REQUEST['who']; as well as $who to the mail line but neither work, the latter doesn't even send an email at all.
<?php
$action=$_REQUEST['action'];
if ($action=="") /* display the contact form */
{
?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
<input name="name" type="text" placeholder="Your Name" value="" size="14"/>
<input name="email" type="text" placeholder="Your Email" value="" size="14"/>
<textarea name="who" placeholder="Who should we contact?" rows="1" cols="14"></textarea>
<textarea name="message" placeholder="Description" rows="2" cols="14"></textarea><br>
<input type="submit" class="button special" value="SUBMIT"/>
</form>
<?php
}
else
{
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$message=$_REQUEST['message'];
if (($name=="")||($email=="")||($message==""))
{
echo "All fields are required, please fill out the form again.";
}
else{
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="Referral for ******* **";
mail("chris#********.com.au", $subject, $message, $from);
}
{
echo "<script type='text/javascript'>window.location.href ='../thanks.php';</script>";
}
}
?>
In PHP . is the concatenation operator which returns the concatenation of its right and left arguments
Try this
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$message = $_REQUEST['message'] . "\n\rFrom: " . $_REQUEST['who'];
Related
how to set validation in contact from
suppose if user submit empty field then how to show Invalid input
now email address only invalid input show i want to fix all field validation please help me how can i do this
thanks in advance
<html>
<body>
<?php
function spamcheck($field) {
// Sanitize e-mail address
$field=filter_var($field, FILTER_SANITIZE_EMAIL);
// Validate e-mail address
if(filter_var($field, FILTER_VALIDATE_EMAIL)) {
return TRUE;
} else {
return FALSE;
}
}
?>
<h2>Form</h2>
<?php
// display form if user has not clicked submit
if (!isset($_POST["submit"])) {
?>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
From: <input type="text" name="email"><br>
Subject: <input type="text" name="subject"><br>
Message: <textarea rows="10" cols="40" name="message"></textarea><br>
<input type="submit" name="submit" value="Submit Feedback">
</form>
<?php
} else { // the user has submitted the form
// Check if the "from" input field is filled out
if (isset($_POST["email"])) {
// Check if "from" email address is valid
$mailcheck = spamcheck($_POST["email"]);
if ($mailcheck==FALSE) {
echo "Invalid input";
} else {
$email = $_POST["email"]; // sender
$subject = $_POST["subject"];
$message = $_POST["message"];
// message lines should not exceed 70 characters (PHP rule), so wrap it
$message = wordwrap($message, 70);
// send mail
mail("demo#gmail.com",$subject,$message,"From: $email\n");
echo "Thank you for sending us feedback";
}
}
}
?>
</body>
</html>
Use these
From: <input type="email" name="email" required><br>
Subject: <input type="text" name="subject" required><br>
Message: <textarea rows="10" cols="40" name="message" required></textarea><br>
Refer this and this
I have a really simple php contact form on one of my sites, the problem is that it won't work when sending emails to some addresses.
It works fine sending to my gmail address, but it doesn't work with iCloud (#me.com) addresses or other domain specific emails that I have set up.
<?php
$action=$_REQUEST['action'];
if ($action=="") /* display the contact form */
{
?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
Your name:<br>
<input name="name" type="text" value="" size="30"/><br>
Your email:<br>
<input name="email" type="text" value="" size="30"/><br>
Your message:<br>
<textarea name="message" rows="7" cols="30"></textarea><br>
<input type="submit" value="Send email"/>
</form>
<?php
}
else /* send the submitted data */
{
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$message=$_REQUEST['message'];
if (($name=="")||($email=="")||($message==""))
{
echo "All fields are required, please fill the form again.";
}
else{
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="Message sent using your contact form";
mail("myemailaddress#me.com", $subject, $message, $from);
echo "Email sent!";
}
}
?>
If it didn't work at all I'd know there was a syntax error, but I get the 'Email Sent!' confirmation.
add if statement to the mail function :
if( mail("myemailaddress#me.com", $subject, $message, $from)) echo "Email sent!";
else echo "failed" ;
this way you'll know if it was sent or not .
then start checking the problem .
you man check your php.ini file :
check
sendmail_from = '';
sendmail_path = '';
and fill them with needed data ... maybe some address doesnt accept emails with no full data in the header . maybe they found it as a spam or somthing else .
Just have a look at your else part where you are using mail .Just below that is the echo so it always get executed as soon as it enters the else block.So, check for mail by if & else condition and then show the massage accordingly
weird problem here, my html form disapears from the page when I click the send button regardless of success or not.
I display an alert box to indicate if the email was sent or not.
here is the code
<?php
$action = $_REQUEST['action'];
if ($action == "") /* display the contact form */ {
?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
<input class="champTextFormulaire" placeholder="Votre Nom" name="name" type="text" value="" size="30"/><br>
<input class="champTextFormulaire" placeholder="Votre email" name="email" type="text" value="" size="30"/><br>
<textarea id="champMessage" placeholder="Votre Message..." name="message" rows="7" cols="30"></textarea><br>
<input class="btnEnvoiFormulaire" type="submit" value="Envoi"/>
</form>
<?php
} else /* send the submitted data */ {
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$message = $_REQUEST['message'];
if (($name == "") || ($email == "") || ($message == "")) {
print '<script type="text/javascript">';
print 'alert("Veuillez remplir tout les champs")';
print '</script>';
} else {
$from = "From: $name<$email>\r\nReturn-path: $email";
$subject = "Message sent using your contact form";
mail(desiletsmathieu#gmail.com", $subject, $message, $from);
print '<script type="text/javascript">';
print 'alert("Mail envoyé")';
print '</script>';
}
}
}
?>
This is happening because you are using $_REQUEST['action']
After your are submitting the form, your hidden field action becomes $_REQUEST['action'];
And after you submit the form, you get $action = $_REQUEST['action']; to be submit.
Where as you should have a blank value for $_REQUEST['action'] to display the form.
Solution:
1) Either, modify if ($action == "submit")
2) Or, assign blank value to the action (hidden) field
Try it this way.
First off, you forgot a double quote just after mail( that read like this:
mail(desiletsmathieu#gmail.com", $subject, $message, $from);
and needed to be changed to:
mail("desiletsmathieu#gmail.com", $subject, $message, $from);
Plus enctype="multipart/form-data" is for file attachments/uploading so you don't need that.
I also removed this line, which was no longer required:
<input type="hidden" name="action" value="submit">
This works and tested:
Note: I added a name to your submit button in order to give it an extra condition. Plus, you basically had your conditions already set, it just needed to be reworked/rethinked and using less code to achieve the same result.
<form action="" method="POST">
<input class="champTextFormulaire" placeholder="Votre Nom" name="name" type="text" value="" size="30"/><br>
<input class="champTextFormulaire" placeholder="Votre email" name="email" type="text" value="" size="30"/><br>
<textarea id="champMessage" placeholder="Votre Message..." name="message" rows="7" cols="30"></textarea><br>
<input class="btnEnvoiFormulaire" type="submit" name="submit" value="Envoi"/>
</form>
<?php
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$message=$_REQUEST['message'];
if (isset($_POST['submit'])) {
if (($name=="")||($email=="")||($message==""))
{
print '<script type="text/javascript">';
print 'alert("Veuillez remplir tout les champs")';
print '</script>';
exit;
}
else
{
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="Message sent using your contact form";
mail("desiletsmathieu#gmail.com", $subject, $message, $from);
print '<script type="text/javascript">';
print 'alert("Mail envoyé")';
print '</script>';
exit;
}
}
?>
I have a simple html contact form, and the php script to send the emails. It's working good, but I want the result (The email has been sent...) to show in the same page, without changing the page. How can I do this?
HTML:
<form name="contact" action="includes/send.php" id="contact_form">
<input type="text" placeholder="Name" name="name" /> <br />
<input type="email" placeholder="Email Address" name="email" /> <br />
<textarea name="message" placeholder="Message" rows="8"></textarea> <br />
<input type="submit" name="submit" id="submit_btn" value="Send" />
</form>
PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = 'amar123syla#gmail.com';
$subject = 'Message from AMARSYLA.COM';
$message = 'FROM: '.$name.' Email: '.$email.'Message: '.$message;
$headers = 'From: amar123syla#gmail.com';
if (filter_var($email, FILTER_VALIDATE_EMAIL)) { // this line checks that we have a valid email address
mail($to, $subject, $message) or die('Error sending Mail'); //This method sends the mail.
echo "Your email was sent!"; // success message
}
?>
jQuery.post('email.php', score, function(result) {
jQuery('#textBlock').html(result);
});
Something like that should work(Result is the echo in your php in this case)
A preffered method is to encode your php result to json so you've got a little more control over it in Javascript though.
jQuery.post('email.php', score, function(result) {
if (result.result == true) {
jQuery('#textBlock').html(result.text);
}
});
And in the email.php:
$result['result'] = true;
$result['text'] = 'Message has been sent';
header('Content-Type: application/json');
echo json_encode($result);
Here see if this works:
<form name="contact" action="**submit to same page**" id="contact_form">
<input type="submit" name="submit" id="submit_btn" value="Send" />
</form>
Then for the php (on the same page as the email form) just add if submit
<?php
if(isset($_POST['submit'])){
run your email script, add javascript, etc.
echo "Your email was sent!"; // success message
}
}
?>
What I have right now is a form with simple input fields such as name, phone number, email, and comment.
<div id="specialsForm"><h3>Interested in this coupon? Email us! </h3>
<form method="post" action="emailMonthlySpecials.php">
Name: <input name="name" type="text" /><br />
Email: <input name="email" type="text" /><br />
Phone Number: <input name="phone" type="text" /><br /><br />
Comment: <br/>
<textarea name="comment" rows="5" cols="30"></textarea><br /><br />
<input type="submit" value="Submit Email"/>
</form></div>
In my emailMonthlySpecials.php I have the following code:
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$phone = $_REQUEST['phone'] ;
$comment = $_REQUEST['comment'] ;
if(!empty($name) && (!empty($email) || !empty($phone))) {
mail("my.email#hotmail.com", "Monthly Specials Email",
"Name: $name
Email: $email
Phone Number: $phone
Comment: $comment", "From: $email" );
} else {
echo '<span class="error">A name, comment, phone number/email is required</span><br />';
};
It goes to a new page and shows the error (if there is one) and if not it goes to a blank page (I know how to make it go to a thank you page but it needs to be kept on its current page) It does work with sending the info to the email.
I have tried
header("Location: ".$_SERVER["PHP_SELF"]);
And it didnt work.. Any ideas?
Have the page that displays the form also be where the form is submitted to. That was if there is an error the page can just continue and show the form again. ie.
contact.php (pseudo code)
<?
if ([form submitted]) {
if ([fields filled out correctly]) {
[send mail]
header('location: thankyou.php');
} else {
$error = 'error message';
}
}
?>
<span class="error"><?=$error;?></span>
<form method="post" action="contact.php">
[field]
</form>
You can also use redirects, but you will need to store the message in $_SESSION variables to carry it over to the other page.
If you want to redirect back to the caller, then you have to write this:
header("Location: ".$_SERVER["REQUEST_URI"]);