Form, receive variables on same php file [duplicate] - php

This question already has answers here:
How to '$_POST' back to the same page or to a different result page in php? [closed]
(3 answers)
Closed 9 years ago.
What I want to do is to create a simple contact form. I have a file called contact.php with the simple html form:
<form action="process-contact.php" method="post">
<input type="text" name="name" placeholder="Nombre*">
<input type="text" name="company" placeholder="Compañía">
<input type="text" name="position" placeholder="Posición">
<input type="text" name="country" placeholder="País*">
<input type="text" name="email" placeholder="Correo electrónico*">
<input type="text" name="subject" placeholder="Asunto">
<textarea name="message" placeholder="Mensaje*"></textarea>
<input type="submit" value="Enviar">
</form>
And I have this php code:
$name = $_POST['name'];
$company = $_POST['company'];
$position = $_POST['position'];
$country = $_POST['country'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$headers = "Sent by: ".$name.' ('.$email.');'." Country: ".$country.'; Company: '.$company. '; Position: '.$position.';';
$to = "email#domain.com";
if($name != '' && $country != '' && $email != '' && $message != ''){
mail($to, $subject, $message, $headers); //calling php mail function
echo 'Thank\'s for contacting us. We will be answering back soon.<br><br>Go back.';
}else{
echo 'Plese verify all the fields and try to send the form again.<br><br>Go back.';
}
The php code is in another file, but I would like to have the php code on the same file file. Is that possible? How can I do it?
Thank you.

You can put everything inside one file and using action=""
Your headers had errors in them and by using those, the Email ended up in my SPAM box, so I changed those with proper headers and put other details using the $message variable.
I added header('Location: thank_you.php'); and will redirect to that page if all the fields were filled. Create a file called thank_you.php or change it to go to your home page.
I replaced:
if($name != '' && $country != '' && $email != '' && $message != ''){
by:
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['country']) ||
empty($_POST['message'])) {
echo "Fill in all the fields. All marked by an asterisk are mandatory.";
}
Form and PHP handler (tested successfully)
<?php
$name = $_POST['name'];
$company = $_POST['company'];
$position = $_POST['position'];
$country = $_POST['country'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$to = "email#example.com";
$message = "Sent by: ".$name.' ('.$email.');'." Country: ".$country.'; Company: '.$company. '; Position: '.$position.';';
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['country']) ||
empty($_POST['message'])) {
echo "Fill in all the fields. All marked by an asterisk are mandatory.";
}
else {
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\n";
$headers .= "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers); //calling php mail function
header("Location: thank_you.php");
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="" method="post">
<input type="text" name="name" placeholder="Nombre*">
<input type="text" name="company" placeholder="Compañía">
<input type="text" name="position" placeholder="Posición">
<input type="text" name="country" placeholder="País*">
<input type="text" name="email" placeholder="Correo electrónico*">
<input type="text" name="subject" placeholder="Asunto">
<textarea name="message" placeholder="Mensaje*"></textarea>
<input type="submit" name="submit" value="Enviar">
</form>
</body>
</html>
Or you can use this as your PHP handler
<?php
$name = $_POST['name'];
$company = $_POST['company'];
$position = $_POST['position'];
$country = $_POST['country'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$to = "email#example.com";
$message = "Sent by: ".$name.' ('.$email.');'." Country: ".$country.'; Company: '.$company. '; Position: '.$position.';';
if(!empty($_POST['name']) &&
!empty($_POST['email']) &&
!empty($_POST['country']) &&
!empty($_POST['message'])) {
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\n";
$headers .= "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers); //calling php mail function
echo 'Thanks for contacting us. We will be answering back soon.<br><br>Go back.';
// to prevent re-submission, use header but NOT with echo.
// header("Location: thank_you.php");
}else{
echo 'Please verify all the fields.<br><br>Go back.';
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="" method="post">
<input type="text" name="name" placeholder="Nombre*">
<input type="text" name="company" placeholder="Compañía">
<input type="text" name="position" placeholder="Posición">
<input type="text" name="country" placeholder="País*">
<input type="text" name="email" placeholder="Correo electrónico*">
<input type="text" name="subject" placeholder="Asunto">
<textarea name="message" placeholder="Mensaje*"></textarea>
<input type="submit" name="submit" value="Enviar">
</form>
</body>
</html>

Set the action attribute of the form to the name of the file containing the form and php code and then include all your form processing php code in the following conditional.
if(count($_POST)) > 0)
This will make sure the php code runs only when the form is submitted.

You'll want to use the isset feature of PHP, perhaps.
$name = $_POST['name'];
if(isset($name) && $name != ''){
blah....
}
else {
blah...
}

Related

Why can't I see the email address from contact form?

I receive an email but it will read from unknown. Everything else seems to work. Here is a link to the page link I don't know much about php.
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$mailTo = "contact#podmtg.com";
$headers = "From: ".$mailFrom;
$txt = "You have recieved an email from ".$name.".\n\n".$message;
mail($mailTo, $subject, $txt, $headers);
header("Location: contact.html?mailsend");
}
?>
<form action="contactform.php" method="post" onsubmit="return confirm('Are you sure you want to submit this form?');">
<input type="text" name="name" placeholder="Name" required="required">
<input type="text" name="email" placeholder="Email" required="required">
<textarea name="message" placeholder="Write message here..." required="required"></textarea>
<button type="submit" name="submit">SUBMIT</button>
</form>
In this you have mention this `$headers = "From: ".$mailFrom; but not $mailFrom found. Use $email in place of $mailFrom.
I changed the code. You guys were right. Thank you!
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$mailFrom = $_POST['email'];
$message = $_POST['message'];
$mailTo = "contact#podmtg.com";
$headers = "From: ".$mailFrom;
$txt = "You have recieved an email from ".$name.".\n\n".$message;
mail($mailTo, $headers, $txt);
header("Location: index.html?mailsend");
}
?>

php Email form / if / else if / error messages

I have tried to create php email form. The form was basically working, but I wanted to add validation functions like I did on 'name'
However, It doesn't work when I empty 'name' and just sent an email.
Any help would be really appreciated.
htmlfile
<form action="php_mini.php" method="post">
Name:
<input type="text" name="name">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
Email:
<input type="text" name="email">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
Phone:
<input type="text" name ="phone">
<br><br>
Comment:
<!--<textarea name="comment"></textarea>-->
<!--<input type = "text" name = "comment">-->
<textarea name="comment" rows="5" cols="40"></textarea>
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
<input type="submit" value="Submit">
</form>
php
echo '<pre>';
print_r( $_POST );
echo '</pre>';
$headers = 'MIME-Version: 1.0' . "\r\n" .
'Content-type: text/html; charset=iso-8859-1' . "\r\n" .
'From: Design_customers <customers.com' . " \r\n" .
//'Reply-To: vader#deathstar.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
//error msg
$nameErr = $emailErr = $phoneErr = "";
$name = $email = $comment = $phone = "";
//receiver
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
}
$email = $_POST['email'];
/*$message = $_POST['comment'];*/
$message = 'You got a message from a customer.:
Name: '.$_POST['name'].'
Email: '.$_POST['email'].'
Phone: '.$_POST['phone'].'
Comment: '.$_POST['comment'];
//sender
$from = 'From: Customer';
$subject = 'Customer Inquiry';
mail( $email, $subject, $message, $from );
use if(isset($_POST["name"])) to stop the error. [Notice: Undefined index: name in C........]
better you have validate this form with java Script. i have attached a snipped part of you form with javascript.
<form action="" method="post" onsubmit="return(Validate());" name="myform">
Name:
<input type="text" name="name">
<span class="error">* <div id="name_error" style="color:red;"></div> </span>
<br><br>
</form>
<script type="text/javascript">
<!--
// Form validation code will come here.
function Validate()
{
if( document.myform.name.value == "" )
{
alert( "Please provide your name!" );
name_error.textContent = "Name is Required.!";
document.myform.name.focus() ;
return false;
}
return( true );
}
//-->
</script>

How to apply Google reCAPTCHA (2.0) to my current HTML/PHP contact form

I am trying to apply Google reCAPTCHA to my HTML/PHP contact form. The reCAPTCHA appears on the page and works, but I cannot get it to work with the form.
Can somebody please tell me how to add the appropiate PHP code to integrate the recaptcha into my contact form?
<form action="submit.php" method="POST">
<input class="textbox" type="text" name="fullname" placeholder="Your Name">
<input class="textbox" type="email" name="email" placeholder="E-Mail">
<input class="textbox" type="tel" name="telephone" placeholder="Telephone">
<input class="textbox" type="text" name="business" placeholder="Organization">
<textarea class="comments_box" name="visitor_message" placeholder="Message"></textarea>
<div class="g-recaptcha" data-sitekey="6LddnxAUAAAAAAi6jmmUcX8pPMfSELRgpNUAI2Ra"></div>
<br>
<input class="submitBtn" type="submit" value="SUBMIT">
</form>
<?php
if (isset($_POST['fullname']) && isset($_POST['email']) && isset($_POST['telephone']) && isset($_POST['business']) && isset($_POST['visitor_message'])) {
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$telephone= $_POST['telephone'];
$business = $_POST['business'];
$visitor_message = $_POST['visitor_message'];
if (!empty($fullname) && !empty($email) && !empty($telephone) && !empty($business) && !empty($visitor_message)) {
$to = 'acramirez38#gmail.com';
$subject = 'Trucker Radio Talk Visitor Message';
$body = "Full Name: " . $fullname ."\n". "E-Mail: " . $email ."\n". "Telephone: " . $telephone ."\n". "Business: " . $business ."\n". "Comments: " . $visitor_message;
$headers = 'From: ' .$email;
if (mail($to, $subject, $body, $headers))
echo 'Thank you for contacting us! Your message has been successfully delivered.';
} else {
echo 'All fields are required! Please return to the previous page and revise.';
}
}
?>

Contact form not sending to email [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 8 years ago.
Ok so my contact form was working fine when I first coded it into my website but now after a few months it has just stopped sending emails through to my address. I'm not sure if this is a problem with the code or with my email? Here is my current mail form...
<form action="contact.php" method="POST" value="form" >
<p>Name:</p><br /><input type="text" name="name" maxlength="25" /><br /><br />
<p>Email address:</p><br /><input type="text" name="email" maxlength="50" /><br /><br />
<p>Message:</p><br /><textarea name="contact_text" rows="6" cols="30" maxlength="1000" ></textarea><br /><br />
<input type="submit" name="submit" />
</form>
<?php
if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['contact_text'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$text = $_POST['contact_text'];
if (!empty($name) && !empty($email) && !empty($text)) {
if(strlen($name)>25 || strlen($email)>50 || strlen($text)>1000) {
echo 'Sorry, max length for some field has been exceeded.';
} else {
$to = 'my.email.address#gmail.com';
$subject = 'Contact form submitted';
$body = $text."\n".$name;
$headers = 'From: '.$email;
if (#mail ($to, $subject, $body, $headers)) {
echo '<h5>Thanks for the message, I will be in touch soon.</h5>';
} else {
echo'<h4>Sorry an error occured, please try again later.</h4>';
}
}
}
else {
echo '<h4>All fields are required.</h4>';
}
}
?>
Try with this mail header :
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

PHP MailScript - Unable to receive email

For some reason i cant receive any email from my submit form - is there a problem with my formmail script?
<form action="contact_process.php" method="post" enctype="application/x-www-form-urlencoded" class="three">
<legend><strong>Form</strong></legend>
<fieldset>
<p>
<label for="name">Your Name</label>
<input type="text" name="name"></p>
<p>
<label for="email">Your Email</label>
<input type="text" name="email"></p>
<p>
<label for="subject">Subject</label>
<input type="text" name="subject"></p>
<p>
<label for="EnquiryType">Enquiry Type</label>
<select type="text" name="EnquiryType">
<option value="general">General</option>
<option value="other">Other</option>
</select></p>
<p>
<label for="message">Message</label>
<textarea type="text" name="message" class="msg"></textarea></p>
</fieldset>
<input type="submit" name="submit" value="Submit">
<input type="reset" name="Submit2" value="Reset">
</form>
contact_process.php
<?php
# bool has_injection (String $var, [String $var, ...])
function has_injection () {
$values = func_get_args();
for ($i=0, $count=func_num_args(); $i<$count; $i++) {
if ( stristr($values[$i], "%0A") || stristr($values[$i], "%0D") || stristr($values[$i], "\\r") || stristr($values[$i], "\\n")
|| stristr($values[$i], "Bcc") || stristr($values[$i], "Content-Type") ) {
return true;
}
}
return false;
}
$error = '';
if (isset($_POST) && count($_POST)>0) {
# The form has been submitted
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$EnquiryType = $_POST['EnquiryType'];
$message = $_POST['message'];
if ($name && $email && $subject && $EnquiryType && $message) {
if (has_injection($name, $email, $subject, $EnquiryType, $message)) {
# You've got another spammer at work here
$error = 'No spamming';
exit(0);
}
else {
# It's safe to send the message
mail('my#email.com', $subject, $message, $EnquiryType, $message,"From: $name <$email>");
}
}
else {
$error = 'Please fill in all the forms';
}
}
?>
u send 6 parameters..
i checked that and i get elow error
mail() expects at most 5 parameters, 6 given in C:\xampp\htdocs\parixan\contact_process.php on line 31
see here
update
$message = $_POST['EnquiryType']."\r\n".$_POST['message'];
$headers = 'From: $name <$email>' . "\r\n" .
'Reply-To: $name <$email>' . "\r\n" ;
then use
mail('my#email.com', $subject, $message, $headers);
I made a pretty good PHP email script that sends. I've been programming a website of my own (PHP, HTML) and I'm trying to find a way to receive emails. I've heard about POP3, and others but I can't ever find much on them.
<?php
$to = $_POST['to'];
$from = $_POST['from'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$body = "This email was sent from a no-reply email service. \n\n $message";
$headers = "From: $from";
mail($to, $subject, $body, $headers);
?>
Hope that helps ^^

Categories