Php Contact Form issue - php

I'm having problems with this code. After submitting the filled form the display message (Email sent!) is not displaying next to the submit button. I need to display the Email sent message next to the submit button on the contact form. After submitting the form, I also need to clear the form fields even after a refresh or a back action. Kindly help me out with this code
Form Action Images:
Before Submitting the Form
After Submitting the form
<?php
$action=$_REQUEST['action'];
if ($action=="") /* display the contact form */
{
?>
<form name="contactform" method="post" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
<label for="your_name">Your Name <font color="red">*</font></label>
<input type="text" id="reset" name="your_name" placeholder="Enter Your Name" maxlength="20" size="40" >
<label for="email">Email Address <font color="red">*</font></label>
<input type="email" id="reset" name="email" placeholder=" Enter Your E-mail Address" maxlength="20" size="40" pattern="[a-z0-9._%+-]+#[a-z0-9.-]+\.[a-z]{2,3}$" required>
<label for="mobile_number">Mobile Number</label>
<input type="tel" id="reset" name="mobile_number" pattern="[0-9]{1}[0-9]{9}" placeholder="Enter Your Phone Number by Adding Country Code (eg: +91.,)" maxlength="30" size="40" >
<label for="message">Message <font color="red">*</font></label>
<textarea name="message" id="reset" placeholder="Your Message Goes Here" maxlength="1000" cols="62" rows="10" required></textarea>
<input type="submit" value="Submit">
</form>
<?php
}
else /* send the submitted data */
{
$your_name = $_REQUEST['your_name'];
$email = $_REQUEST['email'];
$mobile_number = $_REQUEST['mobile_number'];
$message = $_REQUEST['message'];
$formcontent="From: $your_name \n Email: $email \n Phone Number: $mobile_number \n Message: $message";
$recipient = "name#email.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
if (($your_name=="")||($email=="")||($message==""))
{
echo "All fields are required, please fill the form again.";
}
else{
#mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Email sent!";
}
}
?>

Your code should be like this. Notice isset($_POST['submit'], $msg , value="".
<?php
if(isset($_POST['submit']))
{
$your_name = $_REQUEST['your_name'];
$email = $_REQUEST['email'];
$mobile_number = $_REQUEST['mobile_number'];
$message = $_REQUEST['message'];
$formcontent="From: $your_name \n Email: $email \n Phone Number: $mobile_number \n Message: $message";
$recipient = "name#email.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
if (($your_name=="")||($email=="")||($message==""))
{
$msg = "All fields are required";
}
else{
#mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
$msg = "Email Sent";
}
}
?>
<form name="contactform" method="post" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
<label for="your_name">Your Name <font color="red">*</font></label>
<input type="text" id="reset" name="your_name" placeholder="Enter Your Name" maxlength="20" size="40" value="">
<label for="email">Email Address <font color="red">*</font></label>
<input type="email" id="reset" name="email" placeholder=" Enter Your E-mail Address" maxlength="20" size="40" pattern="[a-z0-9._%+-]+#[a-z0-9.-]+\.[a-z]{2,3}$" required value="">
<label for="mobile_number">Mobile Number</label>
<input type="tel" id="reset" name="mobile_number" pattern="[0-9]{1}[0-9]{9}" placeholder="Enter Your Phone Number by Adding Country Code (eg: +91.,)" maxlength="30" size="40" value="">
<label for="message">Message <font color="red">*</font></label>
<textarea name="message" id="reset" placeholder="Your Message Goes Here" maxlength="1000" cols="62" rows="10" required></textarea>
<input type="submit" name="submit" value="Submit">
<?php echo $msg; ?>
</form>

You can redirect the page after sent email or also can set a variable like $msg='email sent'; and call this variable before your submit button.
$action=$_REQUEST['action'];
if ($action!="") /* display the contact form */
{
$your_name = $_REQUEST['your_name'];
$email = $_REQUEST['email'];
$mobile_number = $_REQUEST['mobile_number'];
$message = $_REQUEST['message'];
$formcontent="From: $your_name \n Email: $email \n Phone Number: $mobile_number \n Message: $message";
$recipient = "name#email.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
if (($your_name=="")||($email=="")||($message==""))
{
header('location: yoururl?msg=error');
//$msg = "All fields are required, please fill the form again.";
}
else{
#mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header('location: yoururl?msg=succss');
//$msg = 'Email Sent!';
}
}
?>
<form name="contactform" method="post" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
<label for="your_name">Your Name <font color="red">*</font></label>
<input type="text" id="reset" name="your_name" placeholder="Enter Your Name" maxlength="20" size="40" >
<label for="email">Email Address <font color="red">*</font></label>
<input type="email" id="reset" name="email" placeholder=" Enter Your E-mail Address" maxlength="20" size="40" pattern="[a-z0-9._%+-]+#[a-z0-9.-]+\.[a-z]{2,3}$" required>
<label for="mobile_number">Mobile Number</label>
<input type="tel" id="reset" name="mobile_number" pattern="[0-9]{1}[0-9]{9}" placeholder="Enter Your Phone Number by Adding Country Code (eg: +91.,)" maxlength="30" size="40" >
<label for="message">Message <font color="red">*</font></label>
<textarea name="message" id="reset" placeholder="Your Message Goes Here" maxlength="1000" cols="62" rows="10" required></textarea>
<?php if(isset($_REQUEST['msg']) and $_REQUEST['msg']=='success'){echo "Email sent!";}if(isset($_REQUEST['msg']) and $_REQUEST['msg']=='error'){echo "All fields are required, please fill the form again.";} ?>
<?php echo $msg; ?>
<input type="submit" value="Submit">
</form>

Related

How to prevent form submission if message contain links?

can anyone help me? I'm trying to prevent spam in the contact form.
I would like to achieve this with PHP. I can't find any solution on the web. Thanks in advance for who can help me out.
HTML
<form name="myForm" id="myForm" method="post" action="contact-form.php">
<input type="text" name="name" value="" class="form-control mt-3" id="name" placeholder="Full Name" required>
<input type="email" name="email" value="" class="form-control mt-3" id="email" placeholder="Email" required>
<textarea class="form-control mt-3" name="message" id="message" placeholder="Type your message" rows="7" required></textarea>
<input type="text" id="Nomoboto" name="Nomoboto" value="800 800 1000" autocomplete="off" />
<input type="text" id="PostItBoto" name="PostItBoto" value="93940" autocomplete="off" />
<input type="text" id="Empty_Me" name="Empty_Me" value=""/>
<input type="text" id="Empty_You" name="Empty_You" value=""/>
<button type="submit" class="btn btn-primary float-right mt-3">Send Message</button>
</form>
PHP
<?php
$name = $_POST ['name'];
$email = $_POST ['email'];
$message = $_POST ['message'];
$Empty_Me = $_POST ['Empty_Me'];
$Empty_You = $_POST ['Empty_You'];
$email_from = 'Contact Form';
$email_subject = "New Form Submission";
$email_body = "User Name: $name.\n".
"User Email: $email.\n".
"User Message: $message.\n".
"Empty_Me: $Empty_Me.\n".
"Empty_You: $Empty_You.\n";
function email_validation($str) {
return (!preg_match(
"^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$^", $str))
? FALSE : TRUE;
}
$to = "info#mail.com";
$headers = "From $email_from \r\n";
mail($to,$email_subject,$email_body,$headers);
header("Location: index.html");

PHP form redirecting to PHP file itself

I'm trying to get a contact form working, and I'm being redirected to the PHP file itself, and I'm not sure why. I'm fairly new to PHP and I'm not entirely sure what I'm doing wrong. I'd love to be pointed in the right direction. The code is below.
Thanks
HTML:
<p id='feedback'><?php echo $feedback; ?></p>
<form id="contact_us" enctype="text/plain" method="post" action="form.php">
<input class="form_field" type="text" name="Name" placeholder="Full Name">
<br>
<input class="form_field" type="text" name="Company" placeholder="Company Name">
<br>
<input class="form_field" type="email" name="Email_Address" placeholder="Email Address">
<br>
<textarea class="form_field" rows="10" cols="20" name="Description" wrap="hard" placeholder="Project Description"></textarea>
<br>
<p id="required"><i>Please fill in all the fields*</i></p>
<input class="submit" type="submit" value="SUBMIT">
</form>
PHP:
<?php
$to = 'zack#zfisch.com';
$subject ='Dropset Work Request';
$name = $_POST['Name'];
$company = $_POST['Company'];
$email = $_POST['Email_Address'];
$message = $_POST['Description'];
$message = <<<EMAIL
From: $name
$message
Email: $email
EMAIL;
$header = $subject;
if($_POST) {
mail($to, $subject, $message, $header);
$feedback = 'Email sent!';
}
?>
This line tells the form what PHP file to send the data to:
<form id="contact_us" enctype="text/plain" method="post" action="form.php">
In particular, it is this part that sends the data to a particular form:
action="form.php"
In fact, this is fine. You can process a form in the same PHP file that contains the form. Just put the PHP form processing at the top:
<?php
if (isset($_POST['Email_Address'] && $_POST['Email_Address'] != ''){
$to = 'zack#zfisch.com';
$subject ='Dropset Work Request';
$name = $_POST['Name'];
$company = $_POST['Company'];
$email = $_POST['Email_Address'];
$message = $_POST['Description'];
$message = <<<EMAIL
From: $name
$message
Email: $email
EMAIL;
$header = $subject;
if($_POST) {
mail($to, $subject, $message, $header);
$feedback = 'Email sent!';
}
}else{
>?
<p id='feedback'><?php echo $feedback; ?></p>
<form id="contact_us" enctype="text/plain" method="post" action="form.php">
<input class="form_field" type="text" name="Name" placeholder="Full Name">
<br>
<input class="form_field" type="text" name="Company" placeholder="Company Name">
<br>
<input class="form_field" type="email" name="Email_Address" placeholder="Email Address">
<br>
<textarea class="form_field" rows="10" cols="20" name="Description" wrap="hard" placeholder="Project Description"></textarea>
<br>
<p id="required"><i>Please fill in all the fields*</i></p>
<input class="submit" type="submit" value="SUBMIT">
</form>
<?php
}
?>
Just remove enctype="text/plain" otherwise your code is correct in HTML and try to echo feedback variable other way is to write all your code above the HTML like below:
<?php
if (isset($_POST['Email_Address'] && !empty['Email_Address']){
$to = 'zack#zfisch.com';
$subject ='Dropset Work Request';
$name = $_POST['Name'];
$company = $_POST['Company'];
$email = $_POST['Email_Address'];
$message = $_POST['Description'];
$message = <<<EMAIL
From: $name
$message
Email: $email
$header = $subject;
if($_POST) {
mail($to, $subject, $message, $header);
$feedback = 'Email sent!';
}
}else{
>?
<p id='feedback'><?php echo $feedback; ?></p>
<form id="contact_us" enctype="text/plain" method="post" action="form.php">
<input class="form_field" type="text" name="Name" placeholder="Full Name">
<br>
<input class="form_field" type="text" name="Company" placeholder="Company Name">
<br>
<input class="form_field" type="email" name="Email_Address" placeholder="Email Address">
<br>
<textarea class="form_field" rows="10" cols="20" name="Description" wrap="hard" placeholder="Project Description"></textarea>
<br>
<p id="required"><i>Please fill in all the fields*</i></p>
<input class="submit" type="submit" value="SUBMIT">
</form>
<?php
}
?>

PHP form validation

So I have created a form inside html the code -
<form method="post" action="hi.php">
<fieldset>
<label class="labelone" type="text" />
<input name="name" placeholder="Your Name"/>
<label class="email" for="email" />
<input name="email" placeholder="Email"/>
<label class="phonenumber" for="phonenumber" />
<input name="phonenumber" placeholder="Phone Number"/>
<label class="comments" for="comments" />
<textarea name="comments" placeholder="Comments"></textarea>
</fieldset>
<fieldset>
<input type="submit" value="Submit" class="btn" >
<input class="btn" type="reset" value="Reset">
</fieldset>
</form>
and this is the php -
<?php
$emailSubject = 'Test';
$webMaster = 'Test';
$name = $_GET['firstname'];
$email = $_GET['lastname'];
$message = $_GET['comment'];
$body = <<<EOD
<br><hr><br>
First Name: $firstname <br>
Last Name: $lastname <br>
Message: $comment <br>
EOD;
$headers = "From: $lastname\r\n";
$headers = "Content-type: text/html\r\n";
$success = mail($webMaster, $emailSubject, $body, $headers);
$theResults = <<<EOD
<?php
?>
<?php
$emailSubject = 'Kent Pest Control Services';
$webMaster = 'xxxxxxxxxEDITED#gmail.com';
$name = $_GET['firstname'];
$email = $_GET['email'];
$phonenumber = $_GET['phonenumber']
$message = $_GET['comments'];
$body = <<<EOD
<br><hr><br>
First Name: $firstname <br>
Email: $email <br>
Phone Number: $phonenumber <br>
Message: $comments <br>
EOD;
When I upload it to my hosting and fill in the php form, and click submit, it goes to a blank screen, and I dont get an email, I have used this before like a year ago, I dont know if the code has changed since then, but I cant get it to work, I hope someone can help. :) I changed the email so people cant email me ;)
you are missing a <form> tag in your HTML.
try this:
<form action="yourPHPfile.php" method="post">
<fieldset>
<label class="labelone" type="text" />
<input name="name" placeholder="Your Name"/>
<label class="email" for="email" />
<input name="email" placeholder="Email"/>
<label class="phonenumber" for="phonenumber" />
<input name="phonenumber" placeholder="Phone Number"/>
<label class="comments" for="comments" />
<textarea name="comments" placeholder="Comments"></textarea>
</fieldset>
<fieldset>
<input type="submit" value="Submit" class="btn" >
<input class="btn" type="reset" value="Reset">
</fieldset>
</form>
I would use POST, and not GET as you are passing potentially a lot of data through and it would create a very ugly url.
your php file then..
<?php
$emailSubject = 'Test';
$webMaster = 'Test';
$name = $_POST['firstname'];
$email = $_POST['lastname'];
$message = $_POST['comments']; // you have the wrong key in your original file (comment)
$body = <<<EOD
<br><hr><br>
First Name: $firstname <br>
Last Name: $lastname <br>
Message: $comment <br>
EOD;
$headers = "From: $lastname\r\n";
$headers = "Content-type: text/html\r\n";
$success = mail($webMaster, $emailSubject, $body, $headers);
$theResults = <<<EOD
?>
other points:
you should be validating the email address server side if youre going to do anything else with it, like send back a confirmation etc
I think this code can resolve you query
<form action="actionname.php" method="post">
<fieldset>
<label class="labelone" type="text" />
<input name="firstname" placeholder="Your firstName"/>
<label class="labeltwo" type="text" />
<input name="lastname" placeholder="Your LastName"/>
<label class="email" for="email" />
<input name="email" placeholder="Email"/>
<label class="phonenumber" for="phonenumber" />
<input name="phonenumber" placeholder="Phone Number"/>
<label class="comments" for="comments" />
<textarea name="comments" placeholder="Comments"></textarea>
</fieldset>
<fieldset>
<input type="submit" value="Submit" class="btn" >
<input class="btn" type="reset" value="Reset">
</fieldset>
</form>
Now this php code
<?php
$emailSubject = 'Test';
$webMaster = $_POST['email']; /* Pass your form email address here */
$firstname = $_POST['firstname']; /* Firstname from form */
$lastname = $_POST['lastname']; /* Lastname from form */
$message = $_POST['comments']; /* Comment from form */
$body = <<<EOD
<br><hr><br>
First Name: $firstname <br>
Last Name: $lastname <br>
Message: $message <br>
EOD;
$headers = "From: $lastname\r\n";
$headers = "Content-type: text/html\r\n";
$success = mail($webMaster, $emailSubject, $body, $headers);
$theResults = <<<EOD
?>

Send HTML Form data Via PHP

can anybody help me fix my php code so that it will send the form data to the email address? i am using MAMP Server to check wether it works the website works as planned however once i fill the form out and press submit it takes me to the PHP page however no email is sent.
PHP CODE
<?php
if(isset($_POST['submit'])){
$emailSubject = 'Customer Has a Question!';
$webMaster = 'r1bos#hotmail.com';
$nameField = $_POST ['Full_Name'];
$emailField = $_POST['E-Mail'];
$phoneNumber = $_POST['Phone_Number'];
$questionField = $_POST ['Query'];
$body = <<<EOD
<br><hr><br>
Name: $name <br>
Email: $email <br>
Questions: $question <br>
EOD;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
#mail($webMaster, $emailSubject, $body, $headers);
exit;
}
?>
HTML FORM
<form action="Php/form.php" method="post" name="Contact_Form" >
<label for="fullname">Full Name: </label>
<input type="text" name="Full_Name" id="name"><br>
<label for="E-Mail">E-Mail: </label>
<input type="text" name="E-Mail" id="email" ><br>
<label for="PhoneNumber">Phone Number: </label>
<input type="text" name="Phone_Number" id="phonenumber" ><br>
<label for="Query">Query: </label>
<textarea name="Query" rows="4" cols="21" id="query" ></textarea><br>
<input type="submit" name="submit" value="Send" >
</form>

how to add an attachment to a php contact form

I have made a contact form that allows users to send me messages, but what I couldn't add is the ability for them to send me a file with it. Here is my code:
<form action="mail.php" method="post" enctype='multipart/form-data'>
<input type="text" class="feedback-input" id="firstname" name="firstname" placeholder="First Name" size="30" required="">
<br/>
<input type="text" class="feedback-input" id="lastname" name="lastname" placeholder="Last Name" size="30" required="">
<br/>
<input type="email" class="feedback-input" id="title" name="title" placeholder="E-mail" size="30" required="">
<br/>
<textarea name="message" class="feedback-input" placeholder="What can I help you with?" style="height: 150px;" required=""></textarea><br/>
<br/>
<input type="file" name="file" id="file" placeholder=" " tabindex="1" required/><br/>
<input type="submit" name="submit" id="Submit" value="Send">
and this is the mail.phph file:
<?php
$firstname = $_POST['firstname'];
$title = $_POST['title'];
$lastname = $_POST['lastname'];
$message = $_POST['message'];
$ip = $_SERVER['REMOTE_ADDR'];
$formcontent=" From: $firstname $lastname \n Email Address: $title \n IP: $ip \n\n Description : $message";
$recipient = "myemail#yahoo.com";
$subject = "New Message!!";
$mailheader = "From: $title \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header("Location: www.mythankyoupage.com");
die();
?>

Categories