I am working on a contact form at the minute which will spit out some errors if certain fields are left blank, and if the form submits successfully the user will see a successful message.
At the moment I have an errors class and a success class - these are below:
.errors {
font-family: 'Open Sans', sans-serif;
font-size: 13px;
padding: 7px 5px;
color: #A94442;
font-weight: 400;
width: 100%;
border-radius: 2px;
border: 1px solid #EBCCD1;
background-color: #F2DEDE;
margin: 0 0 10px 0;
}
.success {
font-family: 'Open Sans', sans-serif;
font-size: 13px;
padding: 7px 5px;
color: #3C763D;
font-weight: 400;
width: 100%;
border-radius: 2px;
border: 1px solid #D6E9C6;
background-color: #DFF0D8;
margin: 0 0 10px 0;
}
As far as the php validation goes, the errors/success messages are working as expected, however the problem I have is that the background-color and borders of these classes are showing blank when the users initially goes to the contact page.
You can get to the page here Contact Page by viewing the page you will see the issue straight away.
Please could you give me some advice/suggestions on how to hide these until they are needed? I could remove the background-color and borders from the classes and just have the text, but ideally I would like to keep the styling as it is.
Here is the php to go with this and also the html form
$errors = array();
$success = "";
$name = "";
$email = "";
$website = "";
$budget = "";
$message = "";
if (isset($_POST['contact_submit'])) {
if (isset($_POST['name'])) {
$name = $_POST['name'];
}
if (isset($_POST['email'])) {
$email = $_POST['email'];
}
if (isset($_POST['website'])) {
$website = $_POST['website'];
}
if (isset($_POST['budget'])) {
$budget = $_POST['budget'];
}
if (isset($_POST['message'])) {
$message = $_POST['message'];
}
if (empty($name)) {
$errors[] = "Please enter your name.";
}
if (empty($email)) {
$errors[] = "Please enter a valid email address.";
}
elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors[] = "Please enter a valid email address.";
}
if (empty($message)) {
$errors[] = "Don't forget your message!";
}
if (count($errors) == 0) {
$headers = "From: email here...\r\n";
$headers .= "Content-type: text/html\r\n";
$myaddress = "email here...";
$subject = "Website Enquiry";
$emailmessage = "<b><u>Name:</u></b><br>$name<br><br><b><u>Email:<br></u></b>$email<br><br><b><u>Message:<br></u></b> $message";
if (!mail($myaddress, $subject, $emailmessage, $headers)) {
$errors[] = "A problem occurred sending your email.";
}
else {
$success = "Your message was sent successfully!";
$name = "";
$email = "";
$message = "";
}
}
}
<form method="POST" action="">
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<?php echo "<div class=\"errors\">" . implode("<br>", $errors) . "</div>"; ?>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<?php echo "<div class=\"success\">" . $success . "</div>"; ?>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
<input type="text" name="name" placeholder="Your Name *" value="<?php echo htmlentities($name); ?>">
</div>
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
<input type="text" name="email" placeholder="Your Email *" value="<?php echo htmlentities($email); ?>">
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
<input type="text" name="website" placeholder="Website (optional)" value="<?php echo htmlentities($website); ?>">
</div>
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
<input type="text" name="budget" placeholder="Budget (optional)" value="<?php echo htmlentities($budget); ?>">
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<textarea type="text" name="message" placeholder="Your Message *"><?php echo htmlentities($message); ?></textarea>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<button type="submit" name="contact_submit" class="btn-one">Send</button>
</div>
</div>
</form>
Thanks in advance!!
You have to do some thing like this
<?php
if(!empty($errors))
{
?>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<?php echo "<div class=\"errors\">" . implode("<br>", $errors) . "</div>"; ?>
</div>
</div>
<?php
}
else
{
}
if(!empty($success))
{
?>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<?php echo "<div class=\"success\">" . $success . "</div>"; ?>
</div>
</div>
<?php
}
else
{
}
?>
It would be helpful if you could share your PHP code, but without that I would guess that you're doing something like:
<div class="errors">
<?php
foreach ($errors as $error) {
echo $error . '<br>';
}
?>
</div>
When viewing the HTML for your website, the <div class="errors"></div> HTML is already present.
You should wrap that <div> with an isset() statement, so the HTML is only output when there is something to be shown.
For example:
<?php if (isset($errors) && !empty($errors)): ?>
<div class="errors">
<?php
foreach ($errors as $error) {
echo $error . '<br>';
}
?>
</div>
<?php endif; ?>
Simplest way to do this :
// your php code goes here
<?php
$http_post = ('POST' == $_SERVER['REQUEST_METHOD']);
if($http_post)
{
if(empty($error))
$class = 'error';
else
$class = 'success';
}
?>
// your html code
<div class="<?php echo $class?>"></div>
Related
contactfrm.php
<?php
// Server side validation
$name = $_POST['name'];
$surname = $_POST['surname'];
$bsubject = $_POST['subject'];
$email= $_POST['email'];
$message= $_POST['message'];
$to = "sample#email.com";
$subject = "Mail From sample 1";
$cc = "sample#email.com";
$txt ="";
$txt .="<html><body>";
$txt .= "<table style='border-collapse: collapse; width: 30%; background: #ddd7d769;'><tr>
<th style='border: 1px solid #0000004f; text-align: left; padding: 8px;'>Name</th>";
$txt .= "<td style=' border: 1px solid #0000004f;text-align: left;padding: 8px;'>".$name."</td></tr>";
$txt .= "<tr><th style='border: 1px solid #0000004f; text-align: left; padding: 8px;'>Surname</th>";
$txt .= "<td style='border: 1px solid #0000004f; text-align: left; padding: 8px;'>".$surname."</td></tr>";
$txt .= "<tr><th style='border: 1px solid #0000004f; text-align: left; padding: 8px;'>Subject</th>";
$txt .= "<td style='border: 1px solid #0000004f; text-align: left; padding: 8px;'>".$bsubject."</td></tr>";
$txt .= "<tr style='background: lightgrey;'><th style='border: 1px solid #0000004f; text-align: left; padding: 8px;'>Email</th>";
$txt .= "<td style='border: 1px solid #0000004f; text-align: left; padding: 8px;'>".$email."</td></tr>";
$txt .= "<tr><th style='border: 1px solid #0000004f; text-align: left; padding: 8px;'>Message</th>";
$txt .= "<td style='border: 1px solid #0000004f; text-align: left; padding: 8px;'>".$message."</td></tr>";
$txt .= "</table>";
$txt .="</body></html>";
$headers = "From: Sample 1 . "\r\n" ;
$headers .= 'Cc: '.$cc . "\r\n";
$headers .= "MIME-Version: 1.0'.PHP_EOL'\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if(!empty($email))
{
mail($to,$subject,$txt,$headers);
$success_output = "Your message sent successfully, We will contact you soon. Thank You";
}
else
{
$error_output = "Something went wrong. Please try again later";
}
$output = array(
'error' => $error_output,
'success' => $success_output
);
// Output needs to be in JSON format
echo json_encode($output);
?>
Contact Form.html
<section id="contact" class="contact">
<div class="container" data-aos="fade-up">
<div class="section-title">
<h2>Contact</h2>
<p>Feel Free to contact</p>
</div>
<div class="row">
<div class="col-lg-5 d-flex align-items-stretch">
<div class="info">
<div class="address">
<a class="skype" href="skype:live:.cid.123456789?chat" target = "_blank"><i class="fa fa-skype" aria-hidden="true"></i></a>
<a class="skype" href="skype:live:.cid.123456789?chat" target = "_blank"><h4>Skype</h4></a>
<p>Click here to message us directly on Skype</p>
</div>
<div class="address">
<a class="whatsapp" href="https://wa.me/+123456789" target="_blank"><i class="fa fa-whatsapp" aria-hidden="true"></i></a>
<a class="whatsapp" href="https://wa.me/+123456789" target="_blank"><h4>WhatsApp</h4></a>
<p>Click here to message us directly on WhatsApp </p>
</div>
<div class="email">
<i class="fas fa-envelope"></i>
<h4>Email:</h4>
<p>sample#email.com</p>
</div>
<div class="phone">
<i class="fa fa-phone" aria-hidden="true"></i>
<h4>Call:</h4>
<p>+123456789</p>
</div>
</div>
</div>
<div class="col-lg-7 mt-5 mt-lg-0 d-flex align-items-stretch">
<form id="contactfrm" onsubmit="validateCaptcha()" method = "POST" role="form" class="php-email-form">
<div class="form-row">
<div class="form-group col-md-6">
<label for="name">First Name</label>
<input type="text" name="name" placeholder="Enter Your First Name" class="form-control" id="name" data-rule="minlen:4" data-msg="Please enter at least 4 chars" required />
<div class="validate"></div>
</div>
<div class="form-group col-md-6">
<label for="name">Surname</label>
<input type="text" class="form-control" placeholder="Enter Your Surname" name="surname" id="surname" data-rule="minlen:4" data-msg="Please enter a valid email" />
<div class="validate"></div>
</div>
</div>
<div class="form-group">
<label for="name">Your Email</label>
<input type="email" class="form-control" name="email" placeholder="example#example.com" id="email" data-rule="email" data-msg="Please enter a valid email" />
<div class="validate"></div>
</div>
<div class="form-group">
<label for="name">Subject</label>
<input type="text" class="form-control" name="subject" id="subject" data-rule="minlen:4" data-msg="Please enter at least 8 chars of subject" />
<div class="validate"></div>
</div>
<div class="form-group">
<label for="name">Message</label>
<textarea class="form-control" name="message" rows="10" data-rule="required" data-msg="Please write something for us" required></textarea>
<div class="validate"></div>
</div>
<div id="captcha">
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="Captcha" data-rule="required" data-msg="Please Enter the Captcha" id="cpatchaTextBox"/>
</div>
<div class="text-center" id="btnSend"><button type="submit">Send Message</button></div>
<div id="alert"></div>
</form>
</div>
</div>
</div>
</section><!-- End Contact Section -->
ajax script
<script async = "true">
var code;
function createCaptcha() {
document.getElementById('captcha').innerHTML = "";
var charsArray =
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ#";
var lengthOtp = 6;
var captcha = [];
for (var i = 0; i < lengthOtp; i++) {
var index = Math.floor(Math.random() * charsArray.length + 1);
if (captcha.indexOf(charsArray[index]) == -1)
captcha.push(charsArray[index]);
else i--;
}
var canv = document.createElement("canvas");
canv.id = "captcha";
canv.width = 100;
canv.height = 50;
var ctx = canv.getContext("2d");
ctx.font = "25px Georgia";
ctx.strokeText(captcha.join(""), 0, 30);
code = captcha.join("");
document.getElementById("captcha").appendChild(canv);
}
function validateCaptcha() {
event.preventDefault();
debugger
if (document.getElementById("cpatchaTextBox").value == code) {
// alert("Valid Captcha")
readCap();
}else{
alert("Invalid Captcha. try Again");
createCaptcha();
}
}
function readCap(){
$.ajax({
url: 'contactfrm.php',
type: 'post',
data: $('#contactfrm').serialize(),
dataType: 'json',
success: function( _response ){
var error = _response.error;
var success = _response.success;
console.log(success);
if(error != null) {
$('#alert').html(error);
}
else {
$('#alert').html(success);
$('#btnSend').remove();
}
},
error: function(jqXhr, json, errorThrown){
var error = jqXhr.responseText;
$('#alert').html(error);
}
});
}
</script>
I have 30 different websites with this same contact form at the bottom.
As shown in the image, When I receive an email, it says "Mail From (website Name)"
So, if I have to upload same php for 30 different websites, I'll have to make 30 different php forms with different titles and headers.
How can I add if else loop where I can target respective website and only its header will be displayed. Because right now I have created 10 different 'contactfrm.php' in which I have changed headers to respective website.
Replace this variable
$subject = "Mail From sample 1";
Into this code you will get your solution .
$regexp = "/([\w]{0,}).([\w]{2,3}|[\w]{2,3}.[\w]{2,3})$/";
$url= $_SERVER['SERVER_NAME'];
$input = parse_url($url, PHP_URL_HOST);
preg_match($regexp, $input, $matches);
$subject = "Mail From ".$matches[1]; //out put domain name
I have a products table where I have products being listed into. I want to create a peice of code that will show the same style but different price, name, description, picture for each product. I have create a peice of code that sort of does this. My code displays only the first in the row. I need to list all of them. My code is :
function grabProducts($con) {
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="col-md-12" id="please_wait">
<div class="panel panel-warning">
<div class="panel-heading">
<h3 class="panel-title"><i>Loading, Please Wait...</i></h3>
</div>
<div class="panel-body">
</div>
</div>
</div>
<script>
$(document).ready(function() {
$('#please_wait').fadeOut(7000);
$('#content_purchase').hide(0).delay(5000).fadeIn("slow");
});
</script>
<?php
$users = $this->grabUserInfos($con);
foreach ($users as $user) {
$username = $user[1];
$email = $user[3];
}
$site_config = new site_config();
$member_config = new member_config();
$result = mysqli_query($con, "SELECT * FROM products");
$count = mysqli_num_rows($result);
// See if there are any products in the database
if ($count > 0) {
// While loop for each product element
while ($row = mysqli_fetch_array($result)) {
$id = $row['id'];
$name = $row['name'];
$description = $row['description'];
$picture = $row['picture_location'];
$price = $row['price'];
$stock_monitering = $row['stock_monitering'];
$stock = $row['stock'];
$new_stock = $stock - 1;
$url_path = 'http' . (empty($_SERVER['HTTPS']) ? '' : 's') . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$url_path_naked = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH).'/pizza/products.php';
$getValidatedCheck_ = mysqli_query($con, "SELECT txn_id, hasValidated, item_name, amount, currency, payment_date FROM payment_logs WHERE userId = '$username' ORDER BY id DESC LIMIT 1");
$count_success_payment = mysqli_num_rows($getValidatedCheck_);
// While loop for our successful payment_logs for each user
while ($row = mysqli_fetch_array($getValidatedCheck_)) {
$txn_id = $row['txn_id'];
$validated_check = $row['hasValidated'];
$item_name = $row['item_name'];
$item_price = $row['amount'];
$item_currency = $row['currency'];
$payment_date = $row['payment_date'];
$now = strtotime("-10 minutes");
// If there is not enough stock, show out of stock
if($stock <= 0) {
$display = 'out_of_stock';
} else if($stock > 0 && $now > strtotime($payment_date)) {
$display = 'show_products';
}
// If there are no transactions for that user show the products like normal
if($count_success_payment == 0) {
// If they've not already seen the success message, show them it now and update their hasValidated from 0 to 1 so they don't see it again
if ($validated_check == '0') {
// If stock monitering is on, update our stock to 1 less than we had before the purchase
if($stock_monitering == '1') {
mysqli_query($con, "UPDATE products SET stock = '$new_stock' WHERE name = '$name'");
}
$display = 'show_success';
}
}
}
switch($display) {
case "show_products":
?>
<div class="col-md-3" id="content_purchase" style="display: none;">
<div class="panel panel-danger">
<div class="panel-heading">
<h3 class="panel-title">Price: <i>$<?php echo $price; ?></i>
<div class="fRight" style="float: right;"><i><?php echo $name; ?></i> Stock
(<?php echo $stock; ?>)
</div>
</h3>
</div>
<center>
<div class="panel-body">
<img src="<?php echo $picture; ?>"
style="width: 85%; height: 100px; margin: 15px 0px 15px 0px; border-radius: 5px; border: 2px solid #ED4949;">
<form name="paypal_form" action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business"
value="<?php $site_config->grabSiteSettings($con, 'paypal_address'); ?>">
<input type="hidden" name="item_name" value="<?php echo $name; ?>">
<input type="hidden" name="item_number" value="<?php echo $id; ?>">
<input type="hidden" name="amount" value="<?php echo $price; ?>">
<input type="hidden" name="quantity" value="1">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="custom" value="username=<? echo $username; ?>&status=<? echo $checkout_status; ?>&product=<? echo $name; ?>">
<input type="hidden" name="notify_url" value="https://benzaofficial.com/pizza/includes/checkout.php">
<input type="hidden" name="return"
value="<?php $site_config->grabSiteSettings($con, 'site_url'); ?>/pizza/products.php?status=complete">
<input type="hidden" name="cancel_return"
value="<?php $site_config->grabSiteSettings($con, 'site_url'); ?>/pizza/products.php?status=canceled">
<button type="submit" class="btn btn-danger"
style="vertical-align : bottom; margin-bottom: 15px; display: block; width: 85%;">
<i class="fa fa-paypal"></i>aypal
</button>
<form>
</div>
</center>
</div>
</div>
<?php
break;
case "show_success":
?>
<div class="col-md-12" id="content_purchase" style="display: none;">
<div class="panel panel-success">
<div class="panel-heading">
<h3 class="panel-title"><i>PAYMENT SUCCESS!</i></h3>
</div>
<div class="panel-body">
<p>You've successfully purchased <i><?php echo $item_name; ?></i> for <i><?php echo $item_price; ?> <i><?php echo $item_currency; ?></i></i> We have emailed you your receipt to <?php echo $email; ?>. You can click here to download your purchased files.<br>Click here to purchase again.</p>
</div>
</div>
</div>
<?php
mysqli_query($con, "UPDATE payment_logs SET hasValidated = '1' WHERE userId = '$username' ORDER BY id DESC LIMIT 1");
break;
case "no_products":
?>
<div class="col-md-12" id="content_purchase" style="display: none;">
<div class="panel panel-danger">
<div class="panel-heading">
<h3 class="panel-title"><i>Currently No Products For Sale</i></h3>
</div>
<div class="panel-body">
<p>There are currently no products up for sale.</p>
</div>
</div>
</div>
<?php
break;
case "out_of_stock":
?>
<div class="col-md-3" id="content_purchase" style="display: none;">
<div class="panel panel-danger">
<div class="panel-heading">
<h3 class="panel-title">Price: <i>$<?php echo $price; ?></i>
<div class="fRight" style="float: right;"><i><?php echo $name; ?></i> Stock
(<?php echo $stock; ?>)
</div>
</h3>
</div>
<center>
<div class="panel-body">
<img src="<?php echo $picture; ?>"
style="width: 85%; height: 100px; margin: 15px 0px 15px 0px; border-radius: 5px; border: 2px solid #ED4949;">
<p><i><?php echo $name; ?></i> is currently out of stock. Please come back later and try again.</p>
</div>
</center>
</div>
</div>
<?php
break;
}
}
} else {
$display = 'no_products';
}
}
first you need a function dat select all of your products like this one
function showproducts($conn,$id){
$req="SELECT * FROM products ";
$liste=$conn->query($req);
return $liste->fetchAll();
}
than you should call this function :
$list=$cc->showproducts($cc->conn);
and foreach row you can show all columns
<?php
foreach ($list as $l){ ?>
<tr>
<td><?php echo $l[0] ;?> </td>
<td><?php echo $l[1] ;?> </td>
<td><?php echo $l[2] ;?> </td>
<td><?php echo $l[3] ;?> </td>
<td><?php echo $l[7] ;?> </td>
</tr>
<?php } ?>
I've been following a WordPress contact form tutorial on how to make a custom WordPress contact form.
I've checked, re-checked, and re-checked again, but still I can't get this working. When I submit the form with ALL fields completed,I just get 'Message was not sent. Try Again.'
I can't work out if something has changed maybe since this was written in WordPress. My code stands like this: Just to be sure I didn't make a mistake, I've copied exactly from the tutorial.
<?php
/*
Template Name: Contact Page
*/
?>
<?php
//response generation function
$response = "";
//function to generate response
function my_contact_form_generate_response($type, $message){
global $response;
if($type == "success") $response = "<div class='success'>{$message}</div>";
else $response = "<div class='error'>{$message}</div>";
}
//response messages
$not_human = "Human verification incorrect.";
$missing_content = "Please supply all information.";
$email_invalid = "Email Address Invalid.";
$message_unsent = "Message was not sent. Try Again.";
$message_sent = "Thanks! Your message has been sent.";
//user posted variables
$name = $_POST['message_name'];
$email = $_POST['message_email'];
$message = $_POST['message_text'];
$human = $_POST['message_human'];
//php mailer variables
$to = get_option('admin_email');
$subject = "Someone sent a message from ".get_bloginfo('name');
$headers = 'From: '. $email . "\r\n" .
'Reply-To: ' . $email . "\r\n";
if(!$human == 0){
if($human != 2) my_contact_form_generate_response("error", $not_human); //not human!
else {
//validate email
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
my_contact_form_generate_response("error", $email_invalid);
else //email is valid
{
//validate presence of name and message
if(empty($name) || empty($message)){
my_contact_form_generate_response("error", $missing_content);
}
else //ready to go!
{
$sent = wp_mail($to, $subject, strip_tags($message), $headers);
if($sent) my_contact_form_generate_response("success", $message_sent); //message sent!
else my_contact_form_generate_response("error", $message_unsent); //message wasn't sent
}
}
}
}
else if ($_POST['submitted']) my_contact_form_generate_response("error", $missing_content);
?>
<?php Starkers_Utilities::get_template_parts( array( 'parts/shared/html-header', 'parts/shared/header' ) ); ?>
<div id="primary" class="site-content">
<div id="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<h1 class="entry-title"><?php the_title(); ?></h1>
</header>
<div class="entry-content group">
<?php the_content(); ?>
<style type="text/css">
.error{
padding: 5px 9px;
border: 1px solid red;
color: red;
border-radius: 3px;
}
.success{
padding: 5px 9px;
border: 1px solid green;
color: green;
border-radius: 3px;
}
form span{
color: red;
}
</style>
<div id="respond">
<?php echo $response; ?>
<form action="<?php the_permalink(); ?>" method="post">
<p><label for="name">Name: <span>*</span> <br><input type="text" name="message_name" value="<?php echo esc_attr($_POST['message_name']); ?>"></label></p>
<p><label for="message_email">Email: <span>*</span> <br><input type="text" name="message_email" value="<?php echo esc_attr($_POST['message_email']); ?>"></label></p>
<p><label for="message_text">Message: <span>*</span> <br><textarea type="text" name="message_text"><?php echo esc_textarea($_POST['message_text']); ?></textarea></label></p>
<p><label for="message_human">Human Verification: <span>*</span> <br><input type="text" style="width: 60px;" name="message_human"> + 3 = 5</label></p>
<input type="hidden" name="submitted" value="1">
<p><input type="submit"></p>
</form>
</div>
</div><!-- .entry-content -->
</article><!-- #post -->
<?php endwhile; // end of the loop. ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php Starkers_Utilities::get_template_parts( array( 'parts/shared/footer','parts/shared/html-footer' ) ); ?>
After checking the logs, i found on the contact page i get:
Notice: Undefined index: message_name in C:\xampp\htdocs\wordpress\wp-content\themes\starkers-master\page-contact-us.php on line 33
Notice: Undefined index: message_email in C:\xampp\htdocs\wordpress\wp-content\themes\starkers-master\page-contact-us.php on line 34
Notice: Undefined index: message_text in C:\xampp\htdocs\wordpress\wp-content\themes\starkers-master\page-contact-us.php on line 35
Notice: Undefined index: message_human in C:\xampp\htdocs\wordpress\wp-content\themes\starkers-master\page-contact-us.php on line 36
Notice: Undefined index: submitted in C:\xampp\htdocs\wordpress\wp-content\themes\starkers-master\page-contact-us.php on line 66
Notice: get_current_theme is deprecated since version 3.4! Use wp_get_theme() instead. in C:\xampp\htdocs\wordpress\wp-includes\functions.php on line 2841
Hi I have a "Sticky" form that after successful completion I want to clear the fields
I tried placing the $_POST = array(); in different sections of the IF statements,
This is the entire file, it is a form that get's sent to itself and records what you wrote in a file outside the webroot
Thanks for any help
<!-- BEGIN CHANGABLE CONTENT -->
<?php require('templates/header.html') ?>
<div id="main" role="main">
<h1>Welcome to the site</h1>
<h2>Please fill out the form</h2>
<p>Register and become a member!<br />Members enjoy newsletters and free swag courtesy of Tony Browns Design</p>
<form action="index.php" method='post' id='login_form'>
<legend><h2>Registration Form</h2></legend>
<fieldset>
<div>
<label for="fname">First Name: </label>
<input type="text" name='fname' id='fname' size='20'
value="<?php if (isset($_POST['fname'])){echo htmlspecialchars($_POST['fname']);} ?>" />
</div>
<div>
<label for="lname">Last Name: </label>
<input type="text" name='lname' id='lname' size='20'
value="<?php if(isset($_POST['lname'])){echo htmlspecialchars($_POST['lname']);} ?>" />
</div>
<div>
<label for="email">Email: </label>
<input type="text" name='email' id='email' size='20'
value="<?php if(isset($_POST['fname'])) {echo htmlspecialchars($_POST['email']);} ?>" />
</div>
<div>
<label for="quotes" class='move-top'>Quote: </label>
<textarea name="quotes" id="quote" cols="22" rows="8">Enter your quotation here.</textarea><br />
</div>
<div>
<input type="submit" value='submit' />
</div>
</fieldset>
</form>
<?php
$file = '../quotes_from_users.txt';
//Check if form is submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (empty($_POST['fname']) && (empty($_POST['lname']) && (empty($_POST['email'])))) {
echo '<p style="color: #d00; text-shadow: -1px -1px 0 #200; font-size: 1.4em;">You need to fill in the all the fields!</p>';
}
if (!empty($_POST['quotes']) && ($_POST['quotes'] != 'Enter your quotation here.') ) {
if (is_writable($file)) {
file_put_contents($file, $_POST['quotes'] . PHP_EOL, FILE_APPEND | LOCK_EX);
echo '<p style="color= #cf5 font-size: 1.4em;">Your quote has been stored.</p>';
$_POST = array();
} else {
echo '<p style="color: #d00; text-shadow: -1px -1px 0 #200;">Your quote could not be stored due to a systems error, sorry about that!</p>';
}
} else {
echo '<p style="color: #d00; text-shadow: -1px -1px 0 #200; font-size: 1.4em;">Please enter a quote!</p>';
}
}
?>
<?php require('templates/footer.html'); ?>
</div>
<!-- END CHANGABLE CONTENT -->
Your logic seems flawed, if you only post a quote, it will get saved with this code.
It should be something like:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// the next line has changed
if (empty($_POST['fname']) || (empty($_POST['lname']) || (empty($_POST['email'])))) {
echo '<p style="color: #d00; text-shadow: -1px -1px 0 #200; font-size: 1.4em;">You need to fill in the all the fields!</p>';
}
else // added
{
if (!empty($_POST['quotes']) && ($_POST['quotes'] != 'Enter your quotation here.') ) {
if (is_writable($file)) {
file_put_contents($file, $_POST['quotes'] . PHP_EOL, FILE_APPEND);
echo '<p style="color= #cf5 font-size: 1.4em;">Your quote has been stored.</p>';
// only empty $_POST on a successful submission
$_POST = array();
} else {
echo '<p style="color: #d00; text-shadow: -1px -1px 0 #200;">Your quote could not be stored due to a systems error, sorry about that!</p>';
}
} else {
echo '<p style="color: #d00; text-shadow: -1px -1px 0 #200; font-size: 1.4em;">Please enter a quote!</p>';
}
}
}
That is assuming that your form gets added in templates/footer.html
I have an Html site.In that there have form for sending mail in Each page.The form action is done in another page that is process.php page.I want to redirect from php page to html page after sending mail.
This is my html code
<form name="mail" id="mail" method="post" action="process.php" onSubmit="return valid_form();">
<div class="form_bg11">
<div class="free_call_back">Free Callback Request</div>
<input name="txtname" id="txtname" type="text" class="free_cal_field" value="Your Name" onClick="(this.value='')" >
<input name="txtphone" id="txtphone" type="text" class="free_cal_field" value="Your Phone Number" onClick="(this.value='')">
<input type="submit" name="Submit" style="background-image: url('images/submit.png'); border: 0px none; height: 19px; margin-left: 85px; width: 56px; margin-top:5px;" value=" ">
</div>
</form>
Thi is my process.php page
<?php
if(isset($_POST['Submit']))
{
$name = $_POST['txtname'];
$phone = $_POST['txtphone'];
$to = "mariyadavis90#gmail.com";
$subject = "New Request Come !";
$message = '<div style="background-color: #EEEEEE;border-bottom: 1px solid #DCDCDC;padding: 10px 0 20px;width: 400px;">
<div style="width:400px;">
<div style="width:400px;background-color:#eeeeee;padding: 10px 0 20px;border-bottom:1px solid #dcdcdc;">
<div style="width:300px; text-align:center;color:#666666;margin-left:20px;font-size:12px;font-weight:bold;">
Now you got a new request from :
</div>
</div> <!--end of div_form_main-->
<div style="width:400px;background-color:#eeeeee;padding: 10px 0 20px;border-bottom:1px solid #dcdcdc;">
<div style="float:left;width:150px;color:#666666;margin-left:20px;font-size:12px;font-weight:bold;"> Name:</div>
<div style="float:left;width:auto;color:#000000;font-size:12px;font-weight:bold;">'.$name.'</div>
</div> <!--end of div_form_main-->
<!--end of div_form_main-->
<div style="width:400px;background-color:#eeeeee;padding: 10px 0 20px;border-bottom:1px solid #dcdcdc;">
<div style="float:left;width:150px;color:#666666;margin-left:20px;font-size:12px;font-weight:bold;"> Phone Number:</div>
<div style="float:left;width:auto;color:#000000;font-size:12px;font-weight:bold;">'.$phone.'</div></div> <!--end of div_form_main-->
</div> <!--end of div_password_main-->';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .="Content-Transfer-Encoding: 8bit";
if(!mail($to, $subject, $message, $headers))
{
$msg = "Sending mail is failed !";
}
else
{
$msg = "Mail send Successfully !";
}
print "<script type=\"text/javascript\">";
print "alert('".$msg."')";
print "</script>";
}
?>
how can i do this?
I am a bit confused by your wording. Why can't you just use the PHP header() function?
header('Location: http://www.example.com/');
you can modify your procss.php (in your javascript block)
print "<script type=\"text/javascript\">";
print "alert('".$msg."')";
print "window.location.href='http://www.yourdomain/yourhtmlpage.html'";
print "</script>";
A quick write up
if(!mail($to, $subject, $message, $headers))
{
$msg = "Sending mail is failed !";
}
else
{
$msg = "Mail send Successfully !";
print "<script type=\"text/javascript\">";
print "window.location.href= 'www.example.com' ";
print "</script>";
}
print "<script type=\"text/javascript\">";
print "alert('".$msg."')";
print "</script>";