Ok i will show you code, you should understand from it what i want, problem is it doesnt recognize variable from one php tags inside other php tags on same page which is contact.php...
<form action="contact.php" method="POST">
<p>Name: <input type="text" name="name"><?php $nameErr=' '; echo $nameErr; ?></p>
<p>Email: <input type="email" name="email"><?php $emailErr=' '; echo $emailErr; ?></p>
<p>Message:</p> <textarea name="message" rows="10" cols="50"></textarea><br />
<p><?php $messageErr= ' '; echo $messageErr; ?></p>
<button type="submit" value="Submit" name="Submit1">Submit</button>
</form>
<?php
if (isset($_POST['Submit1'])) {
$name = $_POST['name'];
$email= $_POST['email'];
$message= $_POST['message'];
$formcontent= "From: $name \n Message: $message";
$recipient= "konstantin91#gmail.com";
$subject= "Contact Form";
$mailheader= "From: $email \r\n";
if(strlen($name)==0) {
$nameErr = "Name is required <br>";
die();
}
elseif(strlen($email)==0) {
$emailErr = "Email is required";
die();
}
elseif(strlen($message)==0) {
$messageErr "Message is required <br>";
die();
}
else {
echo "$name, thank you for submiting message";
}
}
?>
I made all $varErr variables empty strings by default so when you first enter contact.php you see nothing, but when you submit form IF checks if field is empty, if it is empty i want inline with form field to echo $varErr for that field ($nameErr or $emailErr, $messageErr will go under textarea field cause i want it like that)
Hope you understand what i want, i have finished everything just that form and i am done.. Ofcourse i can avoid all of this by echoing under form error for empty form fields but that is not what i want...
In your original code $nameErr, $emailErr and $messageErr are changed after showing them.
If you make something like:
<?php $something = "value1";
echo $something;
$something = "value2";
?>
it will show value1 on the screen.
It should be something like this:
<?php
$nameErr='';
$emailErr='';
$messageErr= '';
if ($_POST) {
$name = $_POST['name'];
$email= $_POST['email'];
$message= $_POST['message'];
$formcontent= "From: $name \n Message: $message";
$recipient= "konstantin91#gmail.com";
$subject= "Contact Form";
$mailheader= "From: $email \r\n";
if(strlen($name)==0) {
$nameErr = "Name is required <br>";
}
elseif(strlen($email)==0) {
$emailErr = "Email is required";
}
elseif(strlen($message)==0) {
$messageErr = "Message is required <br>";
}
else {
echo "$name, thank you for submiting message";
}
}
?>
<form action="" method="POST">
<p>Name: <input type="text" name="name"><?php echo $nameErr; ?></p>
<p>Email: <input type="email" name="email"><?php echo $emailErr; ?></p>
<p>Message:</p> <textarea name="message" rows="10" cols="50"></textarea><br />
<p><?php echo $messageErr; ?></p>
<button type="submit" value="Submit" name="Submit1">Submit</button>
</form>
Also I removed die() because else it makes an exitbefore showing the error. However you don't needdie()here because it sends the form only if server runs the lastelse` statement, only if all fields are completed.
Related
I have to validate a form and send an email..I copied this form from w3schools and attached mailto function.But i dont know how to display "Thank you for submitting" After the form is processed. Kindly Help.. The Thank you message should display on the same page below Submit Button.
Here is my code..
<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
if (empty($_POST["website"])) {
$website = "";
} else {
$website = test_input($_POST["website"]);
// check if URL address syntax is valid (this regular expression also allows dashes in the URL)
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&##\/%?=~_|!:,.;]*[-a-z0-9+&##\/%=~_|]/i",$website)) {
$websiteErr = "Invalid URL";
}
}
if (empty($_POST["comment"])) {
$comment = "";
} else {
$comment = test_input($_POST["comment"]);
}
if (empty($_POST["gender"])) {
$genderErr = "Gender is required";
} else {
$gender = test_input($_POST["gender"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<h2>PHP Form Validation Example</h2>
<p><span class="error">* required field.</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name: <input type="text" name="name" value="<?php echo $name;?>">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
E-mail: <input type="text" name="email" value="<?php echo $email;?>">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
Website: <input type="text" name="website" value="<?php echo $website;?>">
<span class="error"><?php echo $websiteErr;?></span>
<br><br>
Comment: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea>
<br><br>
Gender:
<input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?> value="female">Female
<input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?> value="male">Male
<span class="error">* <?php echo $genderErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
$to = "dheeraj.narayan1712#gmail.com";
$subject = "My subject";
$name = "Name: $name";
$email = "$email";
$headers = "From: webmaster#example.com" . "\r\n" .
"CC: somebodyelse#example.com";
mail($to,$subject,$name,$headers);
?>
</body>
</html>
It's rather simple;
if(isset($email)){
echo 'Success! Thanks for submitting';
}
And then just place it after the mail function? Notice that you can change $email to any of the POST variables that you want, and also change the echo'ed content. (if you want to place it inside of the form remember to wrap it in
<?php ?>
I have a form that sends info to php doc. When testing, I submit and array displays but won't go to email address.
<?php require_once("/includes/fbcheader.php");
?>
<div class="comments">
<h3> We Welcome All Comments </h3>
<form action="/php/contact-send.php" method="post"
id="contact-form" class="form">
<p class="input-block">
<label for="form-name" >Name</label>
<input type="text" value name="name" id="form-name"
autofocus placeholder="Please enter name" required>
</p>
<p class="input-block">
<label for="form-email" >Email</label>
<input type="email" value name="email" id="form-email"
required placeholder="Please enter E-Address">
<input type="hidden" value name="age" id="age">
</p>
<p class="input-block">
<label for="form-subject" >Subject</label>
<input type="text" value name="subject" id="form-subject"
required placeholder="Please enter subject">
</p>
<p class="textarea-block">
<label for="form-message">Comment</label>
<textarea name="message" id="form-message"
cols="70" rows="10"></textarea>
</p>
<div class="clear"></div>
<input type="hidden" name="firstname" id="firstname">
<input type="submit" value="Send" id="form-submit">
<p class="hide" id="response"></p>
<div class="hide">
<label for="spam-check">Do not fill out this field</label>
<input name="spam-check" type="text" value id="spam-check">
</div>
</form>
</div>
</body>
</html>
This is the sending php execute code
<html>
<head>
<title>Contact-Send</title>
</head>
<body>
<?php
if(null!==($_POST["name"])) {
$name = $_POST["name"];
/* echo "Please enter 'Name'<br />"; */
} else {
$name = "";
}
if(null!==($email = $_POST["email"])) {
$email = $_POST["email"];
/* echo "Please enter 'E_Mail'<br />"; */
} else {
$email = "";
}
if(null!==($email = $_POST["subject"])) {
$form_email = $_POST["subject"];
/* echo "Please enter 'Subject'<br />"; */
} else {
$subject = "";
}
if(null!==($message = $_POST["message"])) {
$message = $_POST["message"];
/* echo "Please enter your message<br />"; */
} else {
$message = "";
}
/*echo "{$name}, {$email}, {$message}";*/
$email_from = '$Email';
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
"Here is the message:\n $message".
$to = "me#myaddress.com";
?>
<br>
<?php
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $form_email \r\n";
mail($to,$email_subject,$email_body,$headers);
header("thank-you.html");
?>
</body>
</html>
On submit I simply get the array displaying the content of the input fields
A quick look at your PHP code I see the following:
This: if(null!==($email = $_POST["email"])) will fail because you are running a check against nothing. it should be: if(null!==($_POST["email"])).
You have an undefined variable here: $email_from = '$Email'; this will simply output "$Email". It should be $email_from = $email; (as per your code). However you are simply re-assigning the variable here which you dont really need to be doing at all.
Your concatenating the $email_body and $to address.
You header("thank-you.html"); will fail because as per your code, output has already started here <?php (line 6).
You should be aiming for something closer to this for your contact-send.php:
<?php
if(null!==($_POST["name"])) {
$name = $_POST["name"];
/* echo "Please enter 'Name'<br />"; */
} else {
$name = "";
}
if(null!==($_POST["email"])) {
$email = $_POST["email"];
/* echo "Please enter 'E_Mail'<br />"; */
} else {
$email = "";
}
if(null!==($_POST["subject"])) {
$form_email = $_POST["subject"];
/* echo "Please enter 'Subject'<br />"; */
} else {
$subject = "";
}
if(null!==($_POST["message"])) {
$message = $_POST["message"];
/* echo "Please enter your message<br />"; */
} else {
$message = "";
}
/*echo "{$name}, {$email}, {$message}";*/
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
"Here is the message:\n $message";
$to = "me#myaddress.com";
$headers = "From: $email \r\n";
$headers .= "Reply-To: $email \r\n";
mail($to,$email_subject,$email_body,$headers);
header("Location: thank-you.html");
?>
Remember to set $to to your email address!
I have big problem with sending easy html form with php.
My problem is when all fields are empty it still send message.
I don't why this code still send empty form??
<form id="form1" name="form1" method="post" action="forma.php">
<p>
<label for="ime">Ime:</label>
<input type="text" name="ime" id="ime" />
</p>
<p>
<label for="prezime">Prezime:</label>
<input type="text" name="prezime" id="prezime" />
</p>
<p>
<label for="email">e-mail:</label>
<input type="text" name="email" id="email" />
</p>
<p>
<label for="poruka">Poruka:</label>
<textarea name="poruka" cols="40" rows="10" id="poruka"></textarea>
</p>
<p>
<input type="submit" name="submit" value="submit" />
</p>
</form>
My php code:
<?php
if (isset($_POST['submit']))
{
$ime= $_POST['ime'];
$prezime= $_POST['prezime'];
$email= $_POST['email'];
$poruka= $_POST['poruka'];
$email_from = 'neki#email.com';
$email_subject = "forma sa sajta";
$email_body = "Ime: $ime. \n". "Prezime: $prezime \n". "email: $email \n". "poruka: $poruka" ;
$to = "myemail#gmail.com";
mail ($to, $email_subject, $email_body);
echo "Message is sent";
}
else {
echo "Message is not sent";
}
?>
So again, when i fill fields message is sent. It is ok, i received email.
But when i just click submit (without filling fields) it still send message to my email.
What is wrong with this code? I try everything i know, but without success.
Thank you.
The problem is that you are only checking to see if "submit" is set.
Your if statement should read something like:
if(isset($_POST['submit']) && all_other_fields_are_valid($_POST)){...}
function all_other_fields_are_valid($fields)
{
//logic to decide what fields and values you require goes here
}
You need to check all required field. only check submit it will attempt mailing:
if (isset($_POST['submit']
, $_POST['ime']
, $_POST['prezime']
, $_POST['email']
, $_POST['poruka']))
Additionally you can validate from the client side using new HTML5 required attribute:
<input type="text" name="ime" id="ime" required />
This way you don't waste server resources for bad formed requests.
Actually since they are sent as empty variables, they will still evaluate to true in isset(), even if there is no text in the input fields when they are submitted.
if($_POST['ime'] && $_POST['prezime'] && $_POST['email'] && $_POST['poruka']) {
// do stuff here
}
As long as all of these have values and none of the values are 'false' or '0', this will evaluate to true only if somebody puts text in all of the input fields.
This will check if all required POST vars are set, if they're empty and give an error if so:
<?php
if (isset($_POST['submit'], $_POST['ime'], $_POST['prezime'], $_POST['email'], $_POST['poruka']))
{
$error = "";
if($_POST['ime'] == ""){
$error .= "ima was empty!<br />";
}
if($_POST['prezime'] == ""){
$error .= "prezime was empty!<br />";
}
if($_POST['email'] == ""){
$error .= "email was empty!<br />";
}
if($_POST['poruka'] == ""){
$error .= "poruka was empty!<br />";
}
if($error == ""){
$ime= $_POST['ime'];
$prezime= $_POST['prezime'];
$email= $_POST['email'];
$poruka= $_POST['poruka'];
$email_from = 'neki#email.com';
$email_subject = "forma sa sajta";
$email_body = "Ime: $ime. \n". "Prezime: $prezime \n". "email: $email \n". "poruka: $poruka" ;
$to = "myemail#gmail.com";
mail ($to, $email_subject, $email_body);
echo "Message is sent";
} else {
echo $error;
}
} else {
echo "Message is not sent";
}
?>
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
My contact form will not submit and send to my email address..
Here is the PHP validation I am using to check required fields and then to send to my email:
<?php
if (isset($_GET['submit'])) {
$body = '';
$body .= 'Name: ' . $_POST['name'] . "\n\n";
$body .= 'Phone: ' . $_POST['phone'] . "\n\n";
$body .= 'Email: ' . $_POST['email'] . "\n\n";
$body .= 'Message: ' . $_POST['message'] . "\n\n";
mail('myemailaddress#gmail.com', 'Contact Form', $body, 'From: no-reply#mycompany.com');
}
// define variables and initialize with empty values
$nameErr = $addressErr = $emailErr = $messageErr = $spamcheckErr = "";
$name = $address = $email = $message = $spamcheck = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Please enter your name.";
}
else {
$name = $_POST["name"];
}
if (empty($_POST["email"])) {
$emailErr = "Please enter your email.";
}
else {
$email = $_POST["email"];
}
if (empty($_POST["message"])) {
$messageErr = "Cannot leave message box blank.";
}
else {
$message = $_POST["message"];
}
if (!isset($_POST["spamcheck"])) {
$spamcheckErr = "Verify you are not spam.";
}
else {
$spamcheck = $_POST["spamcheck"];
}
}
?>
Here is my HTML:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div id="contact_input" class="col1">
<input name="name" placeholder="Name*" type="text" class="text" value="<?php echo htmlspecialchars($name);?>">
<span class="error"><?php echo $nameErr;?></span><br />
<input name="email" placeholder="Email*" type="email" class="text" value="<?php echo htmlspecialchars($email);?>">
<span class="error"><?php echo $emailErr;?></span><br />
<input name="phone" placeholder="Phone #" type="tel" class="text" value="<?php echo $phone;?>" />
</div>
<div id="contact_input" class="col2">
<textarea name="message" placeholder="Message*" rows="10" cols="25"><?php echo $message?></textarea>
<span class="error"><?php echo $messageErr;?></span>
</div>
<div id="contact_input" class="col3">
<input id="spamcheck" type="checkbox" name="spamcheck" value="<?php echo htmlspecialchars($spamcheck);?>">I am human.*<br />
<span class="error"><?php echo $spamcheckErr;?></span>
<input id="submit" type="submit" name="submit" value="Send" class="button" /><br />
<span>*Required Field.</span>
</div>
</form>
When fields are empty I get the proper error message under each field but I cannot get it to send to my email. However it was emailing me every time I loaded the page, when I made these changes it stopped submitting.
Being new to contact forms with required fields, I can't seem to find the clear answer elsewhere.
I suspect it has something to do with if (isset($_GET['submit'])) Since that is where I made the change and started having issues.
You have to add ?submit to the action string in your form or else $_GET['submit'] will be unset.
<form method="post" action="?submit">
or you can change the isset function to check the $_POST var instead of the $_GET var
if (isset($_POST['submit'])) {
EDIT: Here's what you should do with your validation script
if (!empty($_POST['submit'])) {
$error = array();
if (empty($_POST['email'])) $error[] = 'Please enter your email';
// and so on...
if (empty($error)) {
// Send email script goes here
}
}
And then for your user display upon any errors:
if (!empty($error)) foreach ($error as $e) echo '<p class="error">'.$e.'</p>';
This allows you to add more error messages as often as you'd like with ease, and uses the empty property of an array to verify the lack of error in validation.
I tested your code and everything checked out, except for this line:
if (isset($_GET['submit'])) {
which just needs to be changed to:
if (isset($_POST['submit'])) {
The issue was in fact using $_GET instead of $_POST
EDIT
Added a few conditional statements:
if (($_POST['name'] && $_POST['email'] && $_POST['message'] !="")
&& isset($_POST["spamcheck"]) !="")
Full code (use the full version below):
<?php
if (isset($_POST['submit'])) {
$body = '';
$body .= 'Name: ' . $_POST['name'] . "\n\n";
$body .= 'Phone: ' . $_POST['phone'] . "\n\n";
$body .= 'Email: ' . $_POST['email'] . "\n\n";
$body .= 'Message: ' . $_POST['message'] . "\n\n";
if (($_POST['name'] && $_POST['email'] && $_POST['message'] !="") && isset($_POST["spamcheck"]) !="")
{
mail('myemailaddress#gmail.com', 'Contact Form', $body, 'From: no-reply#mycompany.com');
}
}
// define variables and initialize with empty values
$nameErr = $addressErr = $emailErr = $messageErr = $spamcheckErr = "";
$name = $address = $email = $message = $spamcheck = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Please enter your name.";
}
else {
$name = $_POST["name"];
}
if (empty($_POST["email"])) {
$emailErr = "Please enter your email.";
}
else {
$email = $_POST["email"];
}
if (empty($_POST["message"])) {
$messageErr = "Cannot leave message box blank.";
}
else {
$message = $_POST["message"];
}
if (!isset($_POST["spamcheck"])) {
$spamcheckErr = "Verify you are not spam.";
}
else {
$spamcheck = $_POST["spamcheck"];
}
}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div id="contact_input" class="col1">
<input name="name" placeholder="Name*" type="text" class="text" value="<?php echo htmlspecialchars($name);?>">
<span class="error"><?php echo $nameErr;?></span><br />
<input name="email" placeholder="Email*" type="email" class="text" value="<?php echo htmlspecialchars($email);?>">
<span class="error"><?php echo $emailErr;?></span><br />
<input name="phone" placeholder="Phone #" type="tel" class="text" value="<?php echo $phone;?>" />
</div>
<div id="contact_input" class="col2">
<textarea name="message" placeholder="Message*" rows="10" cols="25"><?php echo $message?></textarea>
<span class="error"><?php echo $messageErr;?></span>
</div>
<div id="contact_input" class="col3">
<input id="spamcheck" type="checkbox" name="spamcheck" value="<?php echo htmlspecialchars($spamcheck);?>">I am human.*<br />
<span class="error"><?php echo $spamcheckErr;?></span>
<input id="submit" type="submit" name="submit" value="Send" class="button" /><br />
<span>*Required Field.</span>
</div>
</form>
I don't understndand if (isset($_GET['submit'])) in fact. Why is it there?
$field1 = NULL;
$field2 = NULL;
if(isset($_POST["submit"])){
$field1 = $_POST["field1"];
$field2 = $_POST["field2"];
//etc
mail ("youremail", "yoursubject", "$field1 $field2 $field3 etc.");
}
I am new to php and I have post forms down but not I want some of my imput fields to be required.
I want this form to force the user to fill out the required fields but then be directed to my process.php page which will send me an email with the data the form collected. Right now the data is being posted at the bottom of the page. Please help me direct the data to an email.
<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if (empty($_POST["name"]))
{$nameErr = "Name is required";}
else
{$name = test_input($_POST["name"]);}
if (empty($_POST["email"]))
{$emailErr = "Email is required";}
else
{$email = test_input($_POST["email"]);}
if (empty($_POST["website"]))
{$website = "";}
else
{$website = test_input($_POST["website"]);}
if (empty($_POST["comment"]))
{$comment = "";}
else
{$comment = test_input($_POST["comment"]);}
if (empty($_POST["gender"]))
{$genderErr = "Gender is required";}
else
{$gender = test_input($_POST["gender"]);}
}
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<h2>PHP Form</h2>
<p><span class="error">* required field.</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name: <input type="text" name="name">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
E-mail: <input type="text" name="email">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
Website: <input type="text" name="website">
<span class="error"><?php echo $websiteErr;?></span>
<br><br>
Comment: <textarea name="comment" rows="5" cols="40"></textarea>
<br><br>
Gender:
<input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="male">Male
<span class="error">* <?php echo $genderErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
echo "<h2>Your Input:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $website;
echo "<br>";
echo $comment;
echo "<br>";
echo $gender;
?>
</body>
</html>
I have the code to collect that data and email it. i just don't know how to get the form to validate and then direct to the process.php page.
$name = $_POST['name'];
$email = $_POST['email'];
$website = $_POST['website'];
$comment = $_POST['comment'];
$gender = $_POST['gender'];
$to = 'str#xxxxxxxx.com';
$subject = 'Executive Plaza Contact Form Response';
$message.= "Name: $name \n";
$message.= "Email: $email \n";
$message.= "email: $email \n";
$message.= "comment: $comment \n";
$message.= "gender: $gender \n";
$headers = "From: Eleven55\r\n";
mail($to, $subject, $message, $headers);
header('Location: thank-you.php');
Read up on the PHP mail() function here. It is basic and should get you started.
$to='you#domain.com';
$subject='Form data';
$body = "{$name} says {$comment}. \n Contact {$name} at {$email} or {$website}. \n {$name} is a {$gender}";
mail($to, $subject, $body);
http://php.net/manual/en/function.mail.php
$to='youremail#yourdomain.com';
$subject='Form data';
$body = "Name :$name \n ...";
mail($to, $subject, $body);
Read the mail function documentation for more info
My bad. Do this
document.forms.onsubmit = function(){ //onsubmit event
//validate form data using getElementById().val and so on
if(formdataisnotvalid){
alert('descriptive error message');
return false; // this will prevent the form from submitting
}