resetting form after php submit - php

I am using a simple html form as a contact, and when fields and submitted the form does not clear the fields.
this is my php
I read online in few places and I've learned that I have to use the .reset , but I am not familiar with php a lot. I am not sure where would I add the .reset and how.
<?
$name = $_REQUEST["name"];
$email = $_REQUEST["email"];
$msg = $_REQUEST["msg"];
$to = "example#example.com";
if (isset($email) && isset($name) && isset($msg)) {
$subject = "Message / Closure Film";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= "From: ".$name." <".$email.">\r\n"."Reply-To: ".$email."\r\n" ;
$msg = "Name: $name:<br/> Email: $email <br/> Message: $msg";
$mail = mail($to, $subject, $msg, $headers);
if($mail)
{
echo 'success';
}
else
{
echo 'failed';
}
}
?>
my html
<div id="contact">
<div class="container">
<div class="row-fluid PageHead">
<div class="span12">
<h3>CONTACT US<span> <img src="images/underline.png" alt="______"></span></h3>
</div>
</div>
<div class="row-fluid ContactUs">
<div class="span6 offset3">
<form class="form-horizontal" id="phpcontactform">
<div class="control-group">
<input class="input-block-level" type="text" placeholder="Full Name" name="name" id="name">
</div>
<div class="control-group">
<input class="input-block-level" type="email" placeholder="Email" name="email" id="email">
</div>
<div class="control-group">
<textarea class="input-block-level" rows="10" name="message" placeholder="Your Message" id="message"></textarea>
</div>
<div class="control-group">
<p>
<input class="btn btn-danger btn-large" type="submit" value="Send Message">
</p>
<span class="loading"></span> </div>
</form>
</div>
added this to the head of my html, but didnt get any result
<script type="javascript">
$('#phpcontactform').trigger("reset");
</script>

Try:
<script type="javascript">
$(document).ready(function() {
$('#phpcontactform')[0].reset();
});
</script>
I'm not sure if you're trying to do an ajax submit and keep the user on the page, or just submit the form. But the line you're looking for is $("#phpcontactform")[0].reset();, you could wrap that in a $(document).ready() if you needed to!

Related

Input radio not sending data

I have a simple contact form, the html is this part:
<!-- CONTACT FORM -->
<form id="contact-form" name="contactform" class="row">
<!-- CONTACT FORM IMPUT -->
<div id="input_name" class="col-md-12">
<input type="text" name="name" id="name" class="form-control" placeholder="Il tuo nome">
</div>
<div id="input_email" class="col-md-12">
<input type="text" name="email" id="email" class="form-control" placeholder="Email">
</div>
<div id="input_subject" class="col-md-12">
<input type="text" name="subject" id="subject" class="form-control" placeholder="Numero di telefono">
</div>
<div id="input_message" class="col-md-12">
<textarea class="form-control" name="message" id="message" rows="6" placeholder="Il tuo messaggio..."></textarea>
</div>
<div class="col-md-12 sinistra"><br>Quale servizio ti interessa?<br><br></div>
<div class="col-md-4 sinistra">
<input type="radio" name="tipologia" value="standard"> Standard<br>
</div>
<div class="col-md-4 sinistra">
<input type="radio" name="tipologia" value="avanzato"> Avanzato<br>
</div>
<div class="col-md-4 sinistra">
<input type="radio" name="tipologia" value="deluxe"> Deluxe<br>
</div>
<div class="col-md-12"><br></div>
<!-- CONTACT FORM SUBMIT BUTTON -->
<div id="form_btn" class="col-md-12">
<input type="submit" value="Invia" id="submit" class="btn btn-small btn-blue">
</div>
<!-- CONTACT FORM MESSAGE -->
<div class="col-md-12 contact-form-msg">
<span class="loading"></span>
</div>
</form>
with this php file
<?
$name = $_REQUEST["name"];
$email = $_REQUEST["email"];
$subject = $_REQUEST["subject"];
$msg = $_POST["msg"];
$tipologia = $_POST['tipologia'] ;
$to = "info#gmail.com";
if (isset($email) && isset($name) && isset($msg) ) {
$email_subject = "$name ha inviato una richiesta di ordine";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= "Da: ".$name." <".$email.">\r\n"."Reply-To: ".$email."\r\n" ;
$msg = "Da: $name<br/> Email: $email <br/> Telefono: $subject <br/>
Tipologia: $tipologia <br/> Messaggio: $msg";
$mail = mail($to, $email_subject, $msg, $headers);
if($mail)
{
echo 'success';
}
else
{
echo 'failed';
}
}
?>
but I have a problem with radio input, the form works but doesn't pass the value of radio to email, so the email has the Tipologia without the radio selected for the form... What could be the error?
<form> defaults to a GET method if POST isn't implied.
You're using two POST arrays.
So... use a POST method and all POST arrays.
Either way, everything must match.
You should also check if the radio buttons are set or not, or any other you wish to include.

Is there anything wrong with the code in my form?

I'm using this tutorial to create a contact form for my site and it doesn't seem to be working. After I fill in my form and press submit, nothing happens. I would like to know whats wrong with it and how I can fix it.
This is my code:
HTML:
<form action="mail/contact.php" method="post">
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Name:</label><br />
<input type="text" name="name" id="name" class="form-control" placeholder="Name"/>
</div></div>
<br />
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Email:</label><br />
<input type="text" name="email" id="email" class="form-control" placeholder="Email"/>
</div></div>
<br />
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Message:</label>
<textarea name="message" rows="5" id="message" class="form-control" placeholder="Message"></textarea>
</div></div>
<br />
<input type="submit" name="submit" value="Submit" class="btn btn-success btn-lg"/>
</form>
PHP:
<?php
// from the form
$name = trim(strip_tags($_POST['name']));
$email = trim(strip_tags($_POST['email']));
$message = htmlentities($_POST['message']);
// set here
$subject = "Contact form submitted!";
$to = 'name#mail.com';
$body = <<<HTML
$message
HTML;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
// send the email
mail($to, $subject, $body, $headers);
// popup success
echo '<script language="javascript">';
echo 'alert("Your message has been sent!")';
echo '</script>';
?>
It's not a problem actually, you hace to change $to to $email in the email() method because the variable that has the email you want to send to is $email.
Your contact.php code will be:
<?php
// from the form
$name = trim(strip_tags($_POST['name']));
$email = trim(strip_tags($_POST['email']));
$message = htmlentities($_POST['message']);
// set here
$subject = "Contact form submitted!";
$body = <<<HTML
$message
HTML;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
// send the email
mail($email, $subject, $body, $headers);
// popup success
echo '<script language="javascript">';
echo 'alert("Your message has been sent!")';
echo '</script>';
?>
Another recommendation is to check this tutorial for a secure contact form.
Also you should check out this two php frameworks:
Symfony 2 Mail
Zend 2 Mail

PHP Undefined index error with form

I keep getting an error as follows when i submit my form. I'm not sure what it is. I'm running the site on WAMP (windows) and im writing it in dreamweaver CS6. Please note I'm fairly new to PHP. Thanks!
Here is the code:
Notice: Undefined index: message in S:\wamp\www\latech\contact.php on line 29
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<?php include 'includes/header.php'?>
<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.6.0/pure-min.css">
<title>Anise Technologys</title>
</head>
<body>
<div class="wrapper">
<?php include 'includes/nav.php'?>
<div class="content">
<h1 id="title-center">Contact</h1>
<?php
// Check for Header Injections
function has_header_injection($str) {
return preg_match( "/[\r\n]/", $str );
}
if (isset($_POST['contact_submit'])) {
// Assign trimmed form data to variables
// Note that the value within the $_POST array is looking for the HTML "name" attribute, i.e. name="email"
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$tel = trim($_POST['tel']);
$msg = $_POST['message']; // no need to trim message
// Check to see if $name or $email have header injections
if (has_header_injection($name) || has_header_injection($email)) {
die(); // If true, kill the script
}
if (!$name || !$email || !$msg) {
echo '<h4 class="error">All fields required.</h4>Go back and try again';
exit;
}
// Add the recipient email to a variable
$to = "brad#brightsidestudios.ca";
// Create a subject
$subject = "$name sent a message via your contact form";
// Construct the message
$message .= "Name: $name\r\n";
$message .= "Email: $email\r\n\r\n";
$message .= "Message:\r\n$msg";
// If the subscribe checkbox was checked
if (isset($_POST['subscribe']) && $_POST['subscribe'] == 'Subscribe' ) {
// Add a new line to the $message
$message .= "\r\n\r\nPlease add $email to the mailing list.\r\n";
}
$message = wordwrap($message, 72); // Keep the message neat n' tidy
// Set the mail headers into a variable
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "From: " . $name . " <" . $email . ">\r\n";
$headers .= "X-Priority: 1\r\n";
$headers .= "X-MSMail-Priority: High\r\n\r\n";
// Send the email!
mail($to, $subject, $message, $headers);
?>
<!-- Show success message after email has sent -->
<h5>Thanks for contacting Anise Technologys!</h5>
<p>Please allow 24 hours for a response.</p>
<p>« Go to Home Page</p>
<?php
} else {
?>
<form method="post" action="" id="contact-form" class="pure-form pure-form-aligned">
<div class="pure-control-group">
<label for="name">Your Name</label>
<input type="text" id="name" name="name" placeholder="Name">
</div>
<div class="pure-control-group">
<label for="email">Your Emal</label>
<input type="email" id="email" name="email" placeholder="you#website.com">
</div>
<div class="pure-control-group">
<label for="tel">Your Telephone</label>
<input type="tel" id="tel" name="tel" placeholder="(xxx)xxx-xxxx">
</div>
<div class="pure-control-group">
<label for="msg">Your Message</label>
<textarea id="msg" name="msg" spellcheck="true" ></textarea>
</div>
<div class="pure-control-group">
<label for="subscribe">
<input type="checkbox" id="cb" name="subscribe" value="Subscribe">
Subscribe to Newsletter</label>
</div>
<div class="pure-control-group">
<input type="submit" class="pure-button pure-button-primary" name="contact_submit" value="Send Message">
</div>
</form>
<?php
}
?>
</div>
</div>
</body>
</html><!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<?php include 'includes/header.php'?>
<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.6.0/pure-min.css">
<title>Anise Technologys</title>
</head>
<body>
<div class="wrapper">
<?php include 'includes/nav.php'?>
<div class="content">
<h1 id="title-center">Contact</h1>
<?php
// Check for Header Injections
function has_header_injection($str) {
return preg_match( "/[\r\n]/", $str );
}
if (isset($_POST['contact_submit'])) {
// Assign trimmed form data to variables
// Note that the value within the $_POST array is looking for the HTML "name" attribute, i.e. name="email"
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$tel = trim($_POST['tel']);
$msg = $_POST['message']; // no need to trim message
// Check to see if $name or $email have header injections
if (has_header_injection($name) || has_header_injection($email)) {
die(); // If true, kill the script
}
if (!$name || !$email || !$msg) {
echo '<h4 class="error">All fields required.</h4>Go back and try again';
exit;
}
// Add the recipient email to a variable
$to = "brad#brightsidestudios.ca";
// Create a subject
$subject = "$name sent a message via your contact form";
// Construct the message
$message .= "Name: $name\r\n";
$message .= "Email: $email\r\n\r\n";
$message .= "Message:\r\n$msg";
// If the subscribe checkbox was checked
if (isset($_POST['subscribe']) && $_POST['subscribe'] == 'Subscribe' ) {
// Add a new line to the $message
$message .= "\r\n\r\nPlease add $email to the mailing list.\r\n";
}
$message = wordwrap($message, 72); // Keep the message neat n' tidy
// Set the mail headers into a variable
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "From: " . $name . " <" . $email . ">\r\n";
$headers .= "X-Priority: 1\r\n";
$headers .= "X-MSMail-Priority: High\r\n\r\n";
// Send the email!
mail($to, $subject, $message, $headers);
?>
<!-- Show success message after email has sent -->
<h5>Thanks for contacting Anise Technologys!</h5>
<p>Please allow 24 hours for a response.</p>
<p>« Go to Home Page</p>
<?php
} else {
?>
<form method="post" action="" id="contact-form" class="pure-form pure-form-aligned">
<div class="pure-control-group">
<label for="name">Your Name</label>
<input type="text" id="name" name="name" placeholder="Name">
</div>
<div class="pure-control-group">
<label for="email">Your Emal</label>
<input type="email" id="email" name="email" placeholder="you#website.com">
</div>
<div class="pure-control-group">
<label for="tel">Your Telephone</label>
<input type="tel" id="tel" name="tel" placeholder="(xxx)xxx-xxxx">
</div>
<div class="pure-control-group">
<label for="msg">Your Message</label>
<textarea id="msg" name="msg" spellcheck="true" ></textarea>
</div>
<div class="pure-control-group">
<label for="subscribe">
<input type="checkbox" id="cb" name="subscribe" value="Subscribe">
Subscribe to Newsletter</label>
</div>
<div class="pure-control-group">
<input type="submit" class="pure-button pure-button-primary" name="contact_submit" value="Send Message">
</div>
</form>
<?php
}
?>
</div>
</div>
</body>
</html>
Your name attribute is msg your calling it message in PHP.
<label for="msg">Your Message</label>
<textarea id="msg" name="msg" spellcheck="true" ></textarea>
$msg = $_POST['message']; // no need to trim message
Also here...
$message .= "Name: $name\r\n";
$message .= "Email: $email\r\n\r\n";
$message .= "Message:\r\n$msg";
The first occurrence should be $message = because there is nothing to concatenate when it hasn't been initialized.
You are using
<textarea id="msg" name="msg" spellcheck="true" ></textarea>
Rather use
<textarea id="msg" name="message" spellcheck="true" ></textarea>

PHP form sends but "No Sender"

I am able to receive email with all the information except for the email address that is entered in the form, and when I receive the email, I get "No Sender" instead of the person's name or email.
This is my HTML:
<!-- Contact Form -->
<div class="col-xs-12 col-sm-8 col-md-8">
<div id="contant-form-bx" class="contant-form-bx">
<div class="contact-loader"></div>
<form action="mail.php" id="contact-form" class="contact-form" name="cform" method="post">
<div class="row">
<div class="col-md-6">
<label for="name" id="name_label">Name</label>
<span class="name-missing">Please enter your name!</span>
<input id="name" type="text" value="" name="name" size="30">
</div>
<div class="col-md-6 columns">
<label for="e-mail" id="email_label">Email</label>
<span class="email-missing">Please enter a valid e-mail!</span>
<input id="e-mail" type="text" value="" name="email" size="30">
</div>
<div class="col-md-12">
<label for="message" id="phone_label">Message</label>
<span class="message-missing">Say something!</span>
<textarea id="message" name="message" rows="7" cols="40"></textarea>
</div>
<div class="col-md-12 text-center">
<input type="submit" name="submit" class="button" id="submit_btn" value="Send Message">
</div>
</div>
</form>
</div>
</div>
This is my PHP:
<?php
// declare our variables
$name = $_POST['name'];
$email = $_POST['e-mail'];
$message = nl2br($_POST['message']);
// set a title for the message
$subject = "From $name via WEB";
$body = "From $name, \n\n$message";
$headers = 'From: '.$email.'' . "\r\n" .
'Reply-To: '.$email.'' . "\r\n" .
'Content-type: text/html; charset=utf-8' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
// put your email address here
mail("info#wziel.com", $subject, $body, $headers);
?>
<!--Display a Thank you message in the callback -->
<div class="mail_response">
<h4>Thank you <?php echo $name ?>!</h4>
<p>I'll be in touch real soon!</p>
</div>
According to your form this:
$email = $_POST['e-mail'];
Should be this:
$email = $_POST['email'];
You have e-mail as the id not the name.
The php looks at name and not id
You're using $_POST['e-mail']. e-mail is the ID of the <input> tag. You should be using $_POST['email'] because name="email".

Cannot POST /.php file when submitting form for email

I'm trying to get a HTML from to work together with PHP in order to make a form for sending a mail, but after submitting the form, PHP file is saying
Cannot POST /quotation.php
Here is my HTML code
<div id="quotation" class="reveal-modal" data-reveal>
<a class="close-reveal-modal">×</a>
<h3>Request Quotation</h3>
<p>Please fill out this information and we will contact you as soon as possilble.</p>
<form method="post" name="" action="quotation.php">
<div class="row">
<div class="large-6 columns">
<label>Name
<input type="text" name="name" placeholder="Name" />
</label>
</div>
<div class="large-6 columns">
<label>Lastname
<input type="text" name="lastname" placeholder="Lastname" />
</label>
</div>
</div>
<div class="row">
<div class="large-6 columns">
<label>Company name
<input type="text" name="company" value="" placeholder="Your company">
</label>
</div>
<div class="large-6 columns">
<label>E-mail
<input type="text" name="email" value="" placeholder="E-mail">
</label>
</div>
</div>
<div class="row">
<div class="large-12 columns">
<label>Additional details
<textarea name="message" placeholder="Additional details here"></textarea>
</label>
</div>
</div>
<input class="button radius" type="submit" name='submit' value="submit">
</form>
</div>
And this is the PHP that runs the form
<?php
if(!isset($_POST['submit']))
{
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$lastname = $_POST['lastname'];
$company = $_POST['company'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
$email_from = $visitor_email;//<== update the email address
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name, $lastname.\n".
"Here is the message:\n $message".
$to = "email#gmail.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
echo "Mail Sent. Thank you " . $name . ", we will contact you shortly.";
alert("yo");
//done. redirect to thank-you page.
header('Location: index.html');
?>
I added alert and echo for submit button, but none of them appears so looks like PHP is not even running.
It would be great if someone could guide me trough this problem or point out what am I doing wrong.

Categories