I am trying to display php error message on same page as form but unable to print this.
Form is working and error message is also showing up but the problem is only form redirect on another page.
Here is form.php
<form action="contact_mail.php" method="post" name="contact_Form" id="contactform">
<div class="row">
<div class="col-md-12 page_subtitle">Get in Touch</div>
</div>
<div class="row mt-30">
<div class="col-md-12 form-group"><input type="text" value="<?php if (isset($errors['name_co'])): echo $errors; endif; ?>" placeholder="Name" class="form-control" name="name_co" ></div>
</div>
<div class="row">
<div class="col-md-6 form-group"><input type="text" id="txtEmail" value="" placeholder="Email ID" class="form-control" name="email_co" pattern="[a-z0-9._%+-]+#[a-z0-9.-]+\.[a-z]{2,63}$" required /></div>
<div class="col-md-6 form-group"><input type="text" value="" placeholder="Phone Number" class="form-control" name="mobile" pattern="[789][0-9]{9}" required /></div>
</div>
<div class="row">
<div class="col-md-12 form-group"><textarea cols="45" rows="6" placeholder="Message" class="form-control" name="comments_co" required/></textarea></div>
</div>
<div class="row">
<div class="col-md-12 form-group"><?php
$a_con=rand(0,9);
$b_con=rand(0,9);
?>
Human Test <?php echo $a_con." + ".$b_con?> =
</div>
</div>
<div class="row">
<div class="col-md-12 form-group"><input type="text" value="" placeholder="Human Test" class="form-control" name="value_num" required/>
<input class="input" name="captcha_num_hidden_contact" id="captcha_num_hidden_contact" type="hidden" value="<?php echo $a_con+$b_con;?>"/>
</div>
</div>
<div class="row">
<div class="col-md-12 form-group"><input type="submit" name="submit" class="btn btn-submit" /></div>
</div>
</form>
And here is
contact_mail.php
<?php
$errors = array(); // array to hold validation errors
$data = array(); // array to pass back data
//error_reporting(0);
// validate the variables ======================================================
// if any of these variables don't exist, add an error to our $errors array
if (isset($_POST['submit'])){
if (empty($_POST['name_co']))
$errors['name_co'] = 'Name is required.';
if (empty($_POST['email_co']))
$errors['email_co'] = 'Email is required.';
if (empty($_POST['mobile'])){
$errors['mobile'] = 'Mobile no is required.';
} else if(strlen((string)$_POST['mobile']) < 10){
$errors['mobile'] = 'Please enter a valid Mobil No.';
}
if (empty($_POST['comments_co']))
$errors['comments_co'] = 'Comments is required.';
if (empty($_POST['value_num'])) {
$errors['value_num'] = 'Human Test is required.';
}
else if ($_POST['captcha_num_hidden_contact'] != $_POST['value_num']) {
$errors['captcha_num_hidden_contact'] = 'Please enter the correct result.';
}
}
// return a response ===========================================================
// if there are any errors in our errors array, return a success boolean of false
if (!empty($errors)) {
// if there are items in our errors array, return those errors
$data['success'] = false;
$data['errors'] = $errors;
} else {
// if there are no errors process our form, then return a message
// DO ALL YOUR FORM PROCESSING HERE
// THIS CAN BE WHATEVER YOU WANT TO DO (LOGIN, SAVE, UPDATE, WHATEVER)
$email_subject = 'Query From Contact Us';
// $message = '<div><strong>Query From Contact Us : </strong></div>';
// $message .= '<div>---------------------------------------- </div>';
// $message .= '<div><strong>Name: </strong>' . $_POST['name_co'] . '</div>';
// $message .= '<div><strong>Email: </strong>' . $_POST['email_co'] . '</div>';
// $message .= '<div><strong>Service: </strong>' . $_POST['option_type'] . '</div>';
// $message .= '<div><strong>Subject: </strong>' . $_POST['subject_co'] . '</div>';
// $message .= '<div><strong>Mobile: </strong>' . $_POST['mobile'] . '</div>';
// $message .= '<div><strong>Comments: </strong>' . $_POST['comments_co'] . '</div>';
$email_subject1 = 'Acknowledgement';
$headers1 = "MIME-Version: 1.0\r\n";
$headers1 .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers1 .= "From: "."\r\n";
$mailstatus1 = mail($_POST['email_co'], $email_subject1, $message1, $headers1, "");
// show a message of success and provide a true success variable
$data['success'] = true;
$data['message'] = 'Your Query has been successfully submit. We will contact you very soon!!';
header("location:thank-you.php");
exit;
}
//echo json_encode($data);
// return all our data to an AJAX call
My main problem is that I want to print the error message on the form page but I was unable to print it.
As mentioned in comments I implemented your code on one page.
changed your
<form action="contact_mail.php" method="post" name="contact_Form" id="contactform">
into
<form target="_self" method="post" name="contact_Form" id="contactform" enctype="multipart/form-data">
and added printing errors as in #Bernhard Answer
echo implode('<br>', $data['errors']);
Also changed pattern in this line
<div class="col-md-6 form-group"><input type="text" value="" placeholder="Phone Number" class="form-control" name="mobile" pattern="[123456789]{9}" required /></div>
I am just going to say that when you will be ready consider user input validation in php not just html and using library for sending email such as PHPMailer.
Here is whole code
<?php
$errors = array(); // array to hold validation errors
$data = array(); // array to pass back data
//error_reporting(0);
// validate the variables ======================================================
// if any of these variables don't exist, add an error to our $errors array
if (isset($_POST['submit'])){
if (empty($_POST['name_co']))
$errors['name_co'] = 'Name is required.';
if (empty($_POST['email_co']))
$errors['email_co'] = 'Email is required.';
if (empty($_POST['mobile'])){
$errors['mobile'] = 'Mobile no is required.';
} else if(strlen((string)$_POST['mobile']) < 10){
$errors['mobile'] = 'Please enter a valid Mobil No.';
}
if (empty($_POST['comments_co']))
$errors['comments_co'] = 'Comments is required.';
if (empty($_POST['value_num'])) {
$errors['value_num'] = 'Human Test is required.';
}
else if ($_POST['captcha_num_hidden_contact'] != $_POST['value_num']) {
$errors['captcha_num_hidden_contact'] = 'Please enter the correct result.';
}
}
// return a response ===========================================================
// if there are any errors in our errors array, return a success boolean of false
if (!empty($errors)) {
// if there are items in our errors array, return those errors
$data['success'] = false;
$data['errors'] = $errors;
echo implode('<br>', $data['errors']);
} else {
// if there are no errors process our form, then return a message
// DO ALL YOUR FORM PROCESSING HERE
// THIS CAN BE WHATEVER YOU WANT TO DO (LOGIN, SAVE, UPDATE, WHATEVER)
$email_subject = 'Query From Contact Us';
// $message = '<div><strong>Query From Contact Us : </strong></div>';
// $message .= '<div>---------------------------------------- </div>';
// $message .= '<div><strong>Name: </strong>' . $_POST['name_co'] . '</div>';
// $message .= '<div><strong>Email: </strong>' . $_POST['email_co'] . '</div>';
// $message .= '<div><strong>Service: </strong>' . $_POST['option_type'] . '</div>';
// $message .= '<div><strong>Subject: </strong>' . $_POST['subject_co'] . '</div>';
// $message .= '<div><strong>Mobile: </strong>' . $_POST['mobile'] . '</div>';
// $message .= '<div><strong>Comments: </strong>' . $_POST['comments_co'] . '</div>';
$email_subject1 = 'Acknowledgement';
$headers1 = "MIME-Version: 1.0\r\n";
$headers1 .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers1 .= "From: "."\r\n";
$mailstatus1 = mail($_POST['email_co'], $email_subject1, $message1, $headers1, "");
// show a message of success and provide a true success variable
$data['success'] = true;
$data['message'] = 'Your Query has been successfully submit. We will contact you very soon!!';
//header("location:thank-you.php");
}
?>
<form target="_self" method="post" name="contact_Form" id="contactform" enctype="multipart/form-data">
<div class="row">
<div class="col-md-12 page_subtitle">Get in Touch</div>
</div>
<div class="row mt-30">
<div class="col-md-12 form-group"><input type="text" value="<?php if (isset($errors['name_co'])): echo $errors; endif; ?>" placeholder="Name" class="form-control" name="name_co" ></div>
</div>
<div class="row">
<div class="col-md-6 form-group"><input type="text" id="txtEmail" value="" placeholder="Email ID" class="form-control" name="email_co" pattern="[a-z0-9._%+-]+#[a-z0-9.-]+\.[a-z]{2,63}$" required /></div>
<div class="col-md-6 form-group"><input type="text" value="" placeholder="Phone Number" class="form-control" name="mobile" pattern="[123456789]{9}" required /></div>
</div>
<div class="row">
<div class="col-md-12 form-group"><textarea cols="45" rows="6" placeholder="Message" class="form-control" name="comments_co" required/></textarea></div>
</div>
<div class="row">
<div class="col-md-12 form-group"><?php
$a_con=rand(0,9);
$b_con=rand(0,9);
?>
Human Test <?php echo $a_con." + ".$b_con?> =
</div>
</div>
<div class="row">
<div class="col-md-12 form-group"><input type="text" value="" placeholder="Human Test" class="form-control" name="value_num" required/>
<input class="input" name="captcha_num_hidden_contact" id="captcha_num_hidden_contact" type="hidden" value="<?php echo $a_con+$b_con;?>"/>
</div>
</div>
<div class="row">
<div class="col-md-12 form-group"><input type="submit" name="submit" class="btn btn-submit" /></div>
</div>
</form>
The solution is simple. You only need to output the Array with the errors below
if (!empty($errors)) {
You can do this like so:
echo implode('<br>', $data['errors']);
implode() will put the array together to a string. is put between each array-element when it is imploded.
Related
<!-- THIS IS USING GOOGLES reCaptcha V2 -->
<?php
if(isset($_POST['ContactButton'])) {
$url = "https://www.google.com/recaptcha/api/siteverify";
$privateKey = "##########################";
$response = file_get_contents($url."?secret=".$privateKey."&response=".$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR']);
$data = json_decode($response);
if (isset($data->success) AND $data->success==true) {
$error = "";
$successMsg = "";
if ($_POST) {
if ($_POST['email'] && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false) {
$error .= "The email is invalid!<br>";
}
if (!$_POST['email']) {
$error .= "An email address is required!<br>";
}
if (!$_POST['subject']) {
$error .= "A subject is required!<br>";
}
if (!$_POST['body']) {
$error .= "Content in the body is required!<br>";
}
if ($error != "") {
$error = '<div class="alert alert-danger" role="alert"><strong>There is an error with your form!</strong><br>' . $error . '</div>';
} else {
$emailTo = 'contactform#########.com';
$subject = $_POST['subject'];
$body = $_POST['body'];
$headers = "From: ".$_POST['email'];
if (mail($emailTo, $subject, $body, $headers)) {
$successMsg = '<div class="alert alert-success" role="alert">The message has successfully been sent. We will contact you ASAP!</div>';
} else {
$error = '<div class="alert alert-danger" role="alert">There was a problem sending your message, please try again later!</div>';
}
}
}
} else {
$captchaFail = '<div class="alert alert-danger" role="alert"><strong>There is an error with your form!</strong><br>reCaptcha Verification Failed, Please Try Again.</div>';
}
}
?>
<div class="jumbotron" id="contact-us-co" style="display: none">
<p>Contact Us:
<br><br>
<form method="POST" class="container">
<h2 style="text-align:center;">***Contact Us***</h2>
<br>
<div id="error"><?php echo $successMsg ?><?php echo $error ?><?php echo $captchaFail ?></div>
<div class="form-row">
<div class="form-group col-md-12">
<label for="inputEmail">Email</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Email">
</div>
</div>
<div class="form-group">
<label for="inputSubject">Subject</label>
<input type="text" class="form-control" id="subject" name="subject" placeholder="Subject">
</div>
<label for="inputBody">Message</label>
<div class="form-group input-group">
<textarea class="form-control" aria-label="With textarea" placeholder="Body" name="body" id="body"></textarea>
</div>
<div class="g-recaptcha" data-sitekey="###################"></div>
<br>
<button type="submit" class="btn btn-primary" name="ContactButton" id="ContactButton">Submit</button>
</form> </p>
</div>
Solution #1 (not sure how to add this code)
code to prevent refresh:
document.getElementById('ContactButton').addEventListener('click', function(event) {
event.preventDefault();
}, false);
Solution #2
After form refresh reset back to the Contact Page: (I used onClick for behaviors)
Contact Us
It's a default html form behavior. If you want to send form data without page reloading you need to looking for AJAX (or AjaxForm plugin):
F.e:
jQuery AJAX submit form
I have a form that I want to add ReCaptcha to, however, somewhere along the line something isnt working correctly because my form will still submit whether or not reCaptcha is verified.
This is the form:
<?php if(isset($_GET[ 'CaptchaPass'])){ ?>
<div>Thank you! Your Form was Successfully Submitted</div>
<?php } ?>
<?php if(isset($_GET[ 'CaptchaFail'])){ ?>
<div>Captcha Error. Please verify that you are human!</div>
<?php } ?>
<form action="http://vmobileautoglass.com/php/func_contact.php">
<label>Name</label> <span class="color-red">*</span>
<div class="row margin-bottom-20">
<div class="col-md-6 col-md-offset-0">
<input class="form-control" type="text" name="name">
</div>
</div>
<label>Email
<span class="color-red">*</span>
</label>
<div class="row margin-bottom-20">
<div class="col-md-6 col-md-offset-0">
<input class="form-control" type="text" name="email">
</div>
</div>
<label>Phone
</label>
<div class="row margin-bottom-20">
<div class="col-md-6 col-md-offset-0">
<input class="form-control" type="text" name="phone">
</div>
</div>
<label>Message</label> <span class="color-red">*</span>
<div class="row margin-bottom-20">
<div class="col-md-8 col-md-offset-0">
<textarea rows="8" class="form-control" name="message"></textarea>
</div>
</div>
<div class="g-recaptcha" data-sitekey="MYSITEKEYFROMGOOGLE"></div>
<p>
<button type="submit" class="btn btn-primary" name="ContactButton">Send Message</button>
</p>
</form>
This goes at the very top of the page where the form is located:
<?php
if (isset($_POST['ContactButoon'])) {
$url = 'https://google.com/recaptcha/api/siteverify';
$privatekey = "MYPRIVATEKEYFROMGOOGLE";
$response = file_get_contents($url . "?secret=" . $privatekey . "&response=" . $_POST['g-recaptcha-response'] . "&remoteip=" . $_SERVER['REMOTE_ADDR']);
$date = json_decode($response);
if (isset($data->success) AND $data->success == true) {
header('Location: contact.php?CaptchaPasss=True');
} else {
header('Location: contact.php?CaptchaFail=True');
}
}
?>
And this is the forms functionality:
// Receiving variables
#$pfw_ip = $_SERVER['REMOTE_ADDR'];
#$name = addslashes($_GET['name']);
#$email = addslashes($_GET['email']);
#$phone = addslashes($_GET['phone']);
#$message = addslashes($_GET['message']);
// Validation
if (strlen($name) == 0) {
header("Location: http://vmobileautoglass.com/php/err_name.php");
exit;
}
if (strlen($email) == 0) {
header("Location: http://vmobileautoglass.com/php/err_email.php");
exit;
}
if (strlen($message) == 0) {
header("Location: http://vmobileautoglass.com/php/err_message.php");
exit;
}
//Sending Email to form owner
$pfw_header = "From: $email\n"
. "Reply-To: $email\n";
$pfw_subject = "vMobile Contact Form";
$pfw_email_to = "vmobileag#gmail.com";
$pfw_message = "Visitor's IP: $pfw_ip\n"
. "name: $name\n"
. "email: $email\n"
. "phone: $phone\n"
. "message: $message\n";
#mail($pfw_email_to, $pfw_subject, $pfw_message, $pfw_header);
header("Location: http://vmobileautoglass.com/php/successform.php");
You have a typo:
$date = json_decode($response);
if(isset($data->success) AND $data->success==true){
$date should be $data.
I considered myself fairly adept at PHP and form processing before this. Now I've run into an issue and can't figure it out despite having going over my code time and time again. Naturally I've browsed StackOverflow and followed many examples as closely as I could, but nothing has fixed my issue, so it seems I'm missing something.
The form retrieves checked materials from the user as an array and puts them in an email body, or at least it should.
The HTML form with a checkbox array:
<div id="contact" class="form-container">
<fieldset>
<div id="message"></div>
<form method="post" action="get-quote.php" name="contactform" id="contactform">
<div class="form-group">
<input name="name" id="name" type="text" value="" placeholder="Name" class="form-control">
</div>
<div class="form-group">
<input name="email" id="email" type="text" value="" placeholder="Email" class="form-control">
</div>
<div class="form-group">
<input name="phone" id="phone" type="text" value="" placeholder="Phone" class="form-control">
</div>
<div class="form-group">
<label>Job Type / Material</label>
<div class="checkbox">
<label>
<input type="checkbox" name="material[]" value="sand">
Sand
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="material[]" value="select_fill">
Select Fill
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="material[]" value="common_fill">
Common Fill
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="material[]" value="top_soil">
Top Soil
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="material[]" value="stabilized_sand">
Stabilized Sand
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="material[]" value="crushed_concrete">
Crushed Concrete
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="material[]" value="milled_asphalt">
Milled Asphalt
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="material[]" value="bull_rock">
Bull Rock
</label>
</div>
</div>
<div class="form-group">
<textarea name="comments" id="comments" class="form-control" rows="3" placeholder="Message"></textarea>
<div class="editContent">
<p class="small text-muted"><span class="guardsman">* All fields are required.</span> Once we receive your message we will respond as soon as possible.</p>
</div>
</div>
<div class="form-group">
<button class="btn btn-primary" type="submit" id="cf-submit" name="submit">Send</button>
</div>
</form>
</fieldset>
</div><!-- /.form-container -->
And the PHP:
<?php
if(!$_POST) exit;
// Email address verification, do not edit.
function isEmail($email) {
return(preg_match("/^[-_.[:alnum:]]+#((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i",$email));
}
if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$comments = $_POST['comments'];
if(trim($name) == '') {
echo '<div class="error_message">Please enter your name.</div>';
exit();
} else if(trim($email) == '') {
echo '<div class="error_message">Please enter a valid email address.</div>';
exit();
} else if(trim($phone) == '') {
echo '<div class="error_message">Please enter a valid phone number.</div>';
exit();
} else if(!is_numeric($phone)) {
echo '<div class="error_message">Phone number can only contain digits.</div>';
exit();
} else if(!isEmail($email)) {
echo '<div class="error_message">You have entered an invalid e-mail address, try again.</div>';
exit();
}
if(trim($comments) == '') {
echo '<div class="error_message">Please enter your message.</div>';
exit();
}
if(get_magic_quotes_gpc()) {
$comments = stripslashes($comments);
}
$materials = isset($_POST['material']) ? implode(', ', $_POST['material']) : 'none';
// Configuration option.
// Enter the email address that you want to emails to be sent to.
// Example $address = "yourname#yourdomain.com";
$address = "myemail#gmail.com";
// Configuration option.
// i.e. The standard subject will appear as, "You've been contacted by John Doe."
// Example, $e_subject = '$name . ' has contacted you via Your Website.';
$e_subject = 'You\'ve been contacted by ' . $name . '.';
// Configuration option.
// You can change this if you feel that you need to.
// Developers, you may wish to add more fields to the form, in which case you must be sure to add them here.
$e_body = "You have been contacted by $name from your website, their message is as follows." . PHP_EOL . PHP_EOL;
$e_content = "\"$comments\"" . PHP_EOL . PHP_EOL;
$e_materials = "They specified the following materials: " . $materials . "." . PHP_EOL . PHP_EOL;
$e_reply = "You can contact $name by email, $email or by phone $phone";
$msg = wordwrap( $e_body . $e_content . $e_material . $e_reply, 70 );
$headers = "From: $email" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;
if(mail($address, $e_subject, $msg, $headers)) {
// Email has sent successfully, echo a success page.
echo "<fieldset>";
echo "<div id='success_page'>";
echo "<h2>Email Sent Successfully.</h2>";
echo "<p>Thank you <strong>$name</strong>, your message has been sent to us.</p>";
echo "</div>";
echo "</fieldset>";
} else {
echo 'ERROR!';
}
I didn't build the whole form or email script, I was just asked to get the checkboxes to work. And everything BUT the checkboxes work. Performing a var_dump on $_POST['material'] returns NULL regardless of whether or not boxes are checked.
Any help would be appreciated, as I've gone over this many times without seeing an issue with the code.
this is my code in HTML5
whenever i am clicking on send button ...
phpfiles open rather mailing on that mail id which I mentioned can anybody help me out please ???
<div class="col-md-6">
<div class="alert alert-success hidden" id="contactSuccess">
<strong>Success!</strong> Your message has been sent to us.</div>
<div class="alert alert-danger hidden" id="contactError">
<strong>Error!</strong> There was an error sending your message.</div>
<h2 class="short">
<strong>Contact</strong> Us</h2>
<form action="php/contact-form.php" id="contactForm" type="post">
<div class="row">
<div class="form-group">
<div class="col-md-6">
<label>Your name *</label>
<input type="text" value="" data-msg-required="Please enter your name." maxlength="100" class="form-control"
name="name" id="name" /></div>
<div class="col-md-6">
<label>Your email address *</label>
<input type="email" value="" data-msg-required="Please enter your email address."
data-msg-email="Please enter a valid email address." maxlength="100" class="form-control" name="email"
id="email" /></div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-12">
<label>Subject</label>
<input type="text" value="" data-msg-required="Please enter the subject." maxlength="100" class="form-control"
name="subject" id="subject" /></div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-12">
<label>Message *</label>
<textarea maxlength="5000" data-msg-required="Please enter your message." rows="10" class="form-control" name="message"
id="message"></textarea></div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<input type="submit" value="Send Message" class="btn btn-primary btn-lg" data-loading-text="Loading..." />
</div>
</div>
</form>
</div>
======================================================
and dis are my codes in php file...
<?php
session_cache_limiter('nocache');
header('Expires: ' . gmdate('r', 0));
header('Content-type: application/json');
// Enter your email address below.
$to = 'info#webppulse.com';
$subject = $_POST['subject'];
if($to) {
$name = $_POST['name'];
$email = $_POST['email'];
$fields = array(
0 => array(
'text' => 'Name',
'val' => $_POST['name']
),
1 => array(
'text' => 'Email address',
'val' => $_POST['email']
),
2 => array(
'text' => 'Message',
'val' => $_POST['message']
)
);
$message = "";
foreach($fields as $field) {
$message .= $field['text'].": " . htmlspecialchars($field['val'], ENT_QUOTES) . "<br>\n";
}
$headers = '';
$headers .= 'From: ' . $name . ' <' . $email . '>' . "\r\n";
$headers .= "Reply-To: " . $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
if (mail($to, $subject, $message, $headers)){
$arrResult = array ('response'=>'success');
} else{
$arrResult = array ('response'=>'error');
}
echo json_encode($arrResult);
} else {
$arrResult = array ('response'=>'error');
echo json_encode($arrResult);
}
?>
whenever i am clicking on send button ...
phpfiles open rather mailing on that mail id can someone help me out please ?
Change the following code
<form action="php/contact-form.php" id="contactForm" type="post">
with
<form action="php/contact-form.php" id="contactForm" method="post">
You Must have install localhost server Like xampp or wampp.
Then put files in
For Xampp : htdocx/ Put your file in htdocx folder and run.
Foe wampp: www/ put your file in www folder and run.
Turn your type="post" into a method="post". Also, you need to have your project placed into the htdocs folder of your WAMP directory and ensure that the WAMP server has been turned on.
You should always try a "Hello World" test first to ensure that everything is running correctly. Check out tutorials on the Web for this.
This question gets asked all the time, but I can't figure out why mine isn't working. I have a form that redirects to itself. If PHP decides it is submitted, there is a success/failure message and it displays the user input as the default value and disables the fields: using phpinfo I can see that the form is being submitted, but this first conditional doesn't work. I've tried a couple of versions, but no luck. It's weird because it sends the email
Specifically, the result and disable functions don't display their code after the form has been sent.
<?php
function clean($data) {
$data = trim(stripslashes(strip_tags($data)));
return $data;
}
function result(){
if($sent) echo $result;
}
function disable($field){
if($sent){
if($field != null){
$ret .= $field . '", disabled, placeholder!="';
}
$ret .= '", disabled, placeholder!="';
echo $ret;
}
}
function option($item){
$ret = "<option>";
if($sent){
if($eventType == $item){
$ret = "<option selected>";
}
}
$ret .= $item . "</option>";
echo $ret;
}
if(isset($_POST['name'])){
$sent = TRUE;
$result = null;
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$eventDate = $_POST['eventDate'];
$eventTime = $_POST['eventTime'];
$eventLength = $_POST['eventLength'];
$eventLocation = $_POST['eventLocation'];
$eventType = $_POST['eventType'];
$message = $_POST['message'];
$recipient = "";
$subject = " Form Submission";
$mailheader = "From: \r\n";
$formcontents = "You received this e-mail message through your website: \n\n";
$formcontents .= "Name: " . clean($name) . "\r\n";
$formcontents .= "Phone: " . clean($phone) . "\r\n";
$formcontents .= "Email: " . clean($email) . "\r\n";
$formcontents .= "Event Date: " . clean($eventDate) . "\r\n";
$formcontents .= "Event Time: " . clean($eventTime) . "\r\n";
$formcontents .= "Event Length: " . clean($eventLength) . "\r\n";
$formcontents .= "Event Location: " . clean($eventLocation) . "\r\n";
$formcontents .= "Event Type: " . clean($eventType) . "\r\n";
$formcontents .= "Message: " . clean($message) . "\r\n";
$formcontents .= "\r\n";
$formcontents .= 'IP: '.$_SERVER['REMOTE_ADDR']."\r\n";
$formcontents .= 'Browser: '.$_SERVER['HTTP_USER_AGENT']."\r\n";
// Send mail
if(mail($recipient, $subject, $formcontents, $mailheader)){;
$result = '<h3 class="alert alert-success"> Thank you, your form was successfully sent and I will contact you shortly.</h3>';
} else {
$result = '<h3 class="alert alert-error"> Your mail could not be sent at this time.</h3>';
}
}
?>
<form action="contact.php" method="POST" class="form-horizontal span4">
<fieldset>
<legend>
<h2>Or send me a message. </h2>
</legend>
<p class="help-block">None of the fields are required, but the more information I have about your event, the more detailed I can be in my response.</p>
<legend class="help-block">Your Details</legend>
<div class="control-group">
<label for="name" class="control-label">Your Name</label>
<div class="controls">
<input id="name" type="text" name="name" placeholder="<?php disable($name); ?>" class="input-xlarge"/>
</div>
</div>
<div class="control-group">
<label for="phone" class="control-label">Your Contact Number</label>
<div class="controls">
<input id="phone" type="tel" name="phone" placeholder="<?php disable($phone); ?>" class="input-xlarge"/>
</div>
</div>
<div class="control-group">
<label for="email" class="control-label">Your Email</label>
<div class="controls">
<input id="email" type="email" name="email" placeholder="<?php disable($email); ?>" class="input-xlarge"/>
</div>
</div>
<legend class="help-block">Your Event </legend>
<div class="control-group">
<label for="eventDate" class="control-label">Your Event's Date</label>
<div class="controls">
<input id="eventDate" type="date" name="eventDate" placeholder="<?php disable($eventDate); ?>" class="input-xlarge"/>
</div>
</div>
<div class="control-group">
<label for="eventTime" class="control-label">Your Event's Start Time</label>
<div class="controls">
<input id="eventTime" type="time" name="eventTime" placeholder="<?php disable($eventTime); ?>" class="input-xlarge"/>
</div>
</div>
<div class="control-group">
<label for="eventLength" class="control-label">Your Event's Length</label>
<div class="controls">
<input id="eventLength" type="text" name="eventLength" placeholder="<?php disable($eventLength); ?>" class="input-xlarge"/>
</div>
</div>
<div class="control-group">
<label for="eventLocation" class="control-label">Your Event's Location</label>
<div class="controls">
<input id="eventLocation" type="text" name="eventLocation" placeholder="<?php disable($eventLocation); ?>" class="input-xlarge"/>
</div>
</div>
<div class="control-group">
<label for="eventType" class="control-label">What Kind of Event</label>
<div class="controls">
<select id="eventType" name="eventType" placeholder="<?php disable($eventType); ?>"><?php option("Charity Event"); option("Expo/Trade Show"); option("Personal Event"); option("Other"); ?></select>
</div>
</div>
<div class="control-group">
<label for="message" class="control-label">Other comments or the best time to reach you.</label>
<div class="controls">
<textarea id="message" name="message" rows="10" placeholder="<?php disable($message); ?>" class="input-xxlarge"></textarea>
</div>
</div>
<div class="form-actions">
<button type="submit" name="submit" placeholder="<?php disable(null); ?>" class="btn btn-primary">Send Message</button>
</div>
</fieldset>
</form>
You have to import your global variables into function scope, like:
function result(){
global $sent, $result;
if($sent) echo $result;
}
..in functions disable() and option(), too.
if(isset($_POST['name'])){
should be
if(isset($_POST['submit'])){