Jump to content

Recommended Posts

I'm just learning about PHP and I'm diving in head first by attempting to modify our web site's contact form.  I'm wishing to stop the form from erasing previously entered values upon "failed" data validation.  It's of course going to be annoying to our visitors if their hardworked entries disappear just because they left out a name or entered an illegal email address.  I regret that some potential solutions found on this forum and other locations on Google didn't seem to help on my page.

 

As you can see, the validation is done with PHP's version of if-elseif-else.  Later I'll be adding a reCAPTCHA and I hope that whatever I learn here might be useful for the potentially same problem when I am adding the CAPTCHA. 

 

The actual contact form is at http://www.woofwoofwoof.org/contact/index.php.  Thank you in advance for your taking the time to help me what originally appeared to be a simple problem.  I have read the rules and I hope I've followed the rules -- please let me know if I committed any sins of omission or commission.  Now, for your Sunday reading pleasure, here's the code:

 

CODE STARTS HERE ====================================

 

<?php include("../common/docType.php"); ?>

<?php include("../common/htmlOpen.php"); ?>

 

<head>

 

<title><?php include("../common/titleBar.php"); ?> - Contact us!</title>

        <meta name="description" content="Call or email us with questions or orders.">

        <meta name="keywords" content="Barking Dog Chocolatiers, Charlotte NC, chocolate, contact us, telephone, email, e-mail">

 

<meta name="geo.placename" content="Charlotte, North Carolina">

<meta name="geo.region" content="US-NC">

 

        <meta name="author" content="Joal Fischer">

 

 

<meta name="verify-v1" content="1aqZs7xrrfI3lp1RaWDkHHjY9UQZIbq2z/mIVdFeXiI=" />

 

<?php include("../common/headInclude.php"); ?>

<script src="../common/common.js" type="text/javascript"></script>

 

<link rel="Stylesheet" href="../css/contact.css" />

 

</head>

 

<?php

 

// Success/Fail message

$msg = "";

$brisket = "";

 

// Target Email

$targetEmail = "barkingdog@bellsouth.net";

 

 

// Process Submissions

if(isset($_POST['submitted'])) {

if($_POST['submitted']) { // process form.

 

$guestName = $_POST['guestName'];

$cityState = $_POST['cityState'];

$emailAddress = $_POST['emailAddress'];

$phone = $_POST['phone'];

$msgSubject = "Barking Dog Chocolatiers Inquiry";

 

$msgContent = "FROM: $guestName\n";

$msgContent .= "City/State: $cityState\n";

$msgContent .= "Phone & time to call: $phone\n";

$msgContent .= "\nInquiry:\n";

$msgContent .= stripslashes($_POST['msgContent']);

 

// Trial if then else reversed from Dan's original allowing maybe multiple tests in sequence -- in action

 

if($guestName == "") {

$keepvar = "1";

$msg = "<p style='color: red; font-weight: bold; font-size: 14px; margin: 12px 0px 0px 0px;'>Please enter a name!</p>";

 

}  elseif($emailAddress == "") {

$msg = "<p style='color: red; font-weight: bold; font-size: 14px; margin: 12px 0px 0px 0px;'>Please enter a valid email address!</p>";

 

} else {

mail($targetEmail, $msgSubject, $msgContent, "From: $guestName <$emailAddress>\nX-Mailer:PHP/" . phpversion());

$msg = "<p style='color: darkgreen; font-weight: bold; font-size: 14px; margin: 12px 0px 0px 0px;'>Thank you! Your message has been sent.</p>";

$brisket = "<img src='../images/Brisket180x180web.jpg' width='180px' height='180px' alt='Brisket Says Hi' />";

}

 

 

// Original if then else all commented out

 

// if($emailAddress != "") {

// mail($targetEmail, $msgSubject, $msgContent, "From: $guestName <$emailAddress>\nX-Mailer:PHP/" . phpversion());

// $msg = "<p style='color: darkgreen; font-weight: bold; font-size: 14px; margin: 12px 0px 0px 0px;'>Thank you! Your message has been sent.</p>";

// $brisket = "<img src='../images/Brisket180x180web.jpg' width='180px' height='180px' alt='Brisket Says Hi' />";

// } else {

// $msg = "<p style='color: red; font-weight: bold; font-size: 14px; margin: 12px 0px 0px 0px;'>Please enter a valid email address!</p>";

// }

}

}

 

?>

 

 

<body onload="loadMe('contact')">

 

 

<div id="container">

<div id="header">

<a href="../home/index.php" style="border:none"><div id="logo"></div></a>

 

<div id="navBG"><?php include("../common/mainNav.php"); ?></div>

</div><!--End Header-->

 

<div id="contentArea">

<br /><img class="sideBox" src="../images/sideBox.jpg" width="12px" height="12px"><p class="sideItemSelected">contact us</p><br style="clear:both" />

<p class="sideItem"><a class="sideLink" href="./shipping.php">useful q & a</a></p>

 

<div id="mainContent">

 

<h1>contact us</h1><br />

 

<h2>Have questions? Ready to order?<br />

<span style="color:#b5a072">Call 704.333.1595 Mon-Fri 9am - 5pm EST</span></h2>

<p id="contactText" class="text">It's not business, it's personal! For instant answers to many questions about ordering, shipping, and other good stuff, please check the <a href="shipping.php">useful q&a</a> page.  Nonetheless, we'll always be happy to answer your questions by phone or through the form below. We look forward to hearing from you.</p><br />

 

<div id="contactFormContainer">

 

<form method="post" id="contactForm" action="<?php echo($_SERVER['PHP_SELF']); ?>" >

<div id="formContainer">

<input type="hidden" name="submitted" value="true" />

<input type="hidden" name="formname" value="contact" />

 

 

<div class="formRow">

 

<div class="itemSet">

<p class="itemLabel">Name</p>

<input tabindex="1" class="itemContent" type="text" name="guestName" value='<?php echo $_POST[Name]; ?>' />

 

</div>

 

 

<div class="itemSet">

<p class="itemLabel">City & state</p>

<input tabindex="2" class="itemContent" type="text" name="cityState" />

</div>

 

<br class="clearMe" />

</div>

 

<div class="formRow">

<div class="itemSet">

<p class="itemLabel">Email Address</p>

<input tabindex="3" class="itemContent" type="text" name="emailAddress" />

</div>

 

<div class="itemSet">

<p class="itemLabel">Phone & best local time to call</p>

<input tabindex="4" class="itemContent" type="text" name="phone" />

</div>

 

 

</div>

 

<div class="formRow">

<div>

<p class="itemLabel">Your question</p>

<textarea tabindex="5" id="msgContent" type="text" name="msgContent">Please telephone us to submit an order</textarea>

</div>

</div>

 

<div class="formRow">

<div style="position: relative;">

<div style="float: left; margin-right: 40px;">

<input tabindex="6" id="submitButton" type="submit" name="submit" value="Submit" />

</div>

 

<div style="float: left; margin-top: -10px;">

<?php //echo($msg); ?>

</div>

 

<br style="clear: both;" />

 

<div style="clear: both; position: absolute; top: -350px; left: -217px; width: 180px;">

<?php

echo($msg);

echo($brisket);

?>

</div>

 

 

<br style="clear: both;" />

</div>

</div>

 

</div> <!-- End Form Container -->

 

</form>

 

 

</div> <!-- End Contact Form -->

</div>

 

</div><!-- End Content Area-->

 

<div id="bottomBar"></div>

 

 

 

 

<?php include("../common/footer.php"); ?>

 

</div><!-- End Container -->

 

</body>

 

</html>

 

CODE STOPS HERE=====================================

 

 

 

 

 

 

Please use the code tags next time. :)

 

For all of your input, you need to use the actual input name when echoing the value to retain the input value. So, your

<input tabindex="1" class="itemContent" type="text" name="guestName" value='<?php echo $_POST['Name']; ?>' />

becomes

<input tabindex="1" class="itemContent" type="text" name="guestName" value='<?php echo $_POST['guestName']; ?>' />

 

Or, your

 <input tabindex="2" class="itemContent" type="text" name="cityState" />

becomes

<input tabindex="2" class="itemContent" type="text" name="cityState" value='<?php echo $_POST['cityState']; ?>' />

Dear brthl, 

 

Not only are your suggestions working perfectly, but also the clarity of your writing is really good.  I hope one of your jobs is writing textbooks or instructions!

 

Everything worked when I used your ideas -- I even figured out how to make it work for the textarea. 

 

However, when I go to test the 5th content area (your question), I can't find a string name to represent what exists in the "your question" textarea.  If I can get a name for the string itself so I can test for a length (say, <5), then I can require a question prior to "Submit" being successful.

 

Here's the new code; the part in reference is marked in red as "// OH HELP!"  on the left margin.  Thank you again for your articular and ridiculously prompt response.

 

START CODE HERE =============

 

<?php include("../common/docType.php"); ?>

<?php include("../common/htmlOpen.php"); ?>

 

<head>

 

<title><?php include("../common/titleBar.php"); ?> - Contact us!</title>

        <meta name="description" content="Call or email us with questions or orders.">

        <meta name="keywords" content="Barking Dog Chocolatiers, Charlotte NC, chocolate, contact us, telephone, email, e-mail">

 

<meta name="geo.placename" content="Charlotte, North Carolina">

<meta name="geo.region" content="US-NC">

 

        <meta name="author" content="Joal Fischer">

 

 

<meta name="verify-v1" content="1aqZs7xrrfI3lp1RaWDkHHjY9UQZIbq2z/mIVdFeXiI=" />

 

<?php include("../common/headInclude.php"); ?>

<script src="../common/common.js" type="text/javascript"></script>

 

<link rel="Stylesheet" href="../css/contact.css" />

 

</head>

 

<?php

 

// Success/Fail message

$msg = "";

$brisket = "";

 

// Target Email

$targetEmail = "barkingdog@bellsouth.net";

 

 

// Process Submissions

if(isset($_POST['submitted'])) {

if($_POST['submitted']) { // process form.

 

$guestName = $_POST['guestName'];

$cityState = $_POST['cityState'];

$emailAddress = $_POST['emailAddress'];

$phone = $_POST['phone'];

$msgSubject = "Barking Dog Chocolatiers Inquiry";

 

$msgContent = "FROM: $guestName\n";

$msgContent .= "City/State: $cityState\n";

$msgContent .= "Phone & time to call: $phone\n";

$msgContent .= "\nInquiry:\n";

$msgContent .= stripslashes($_POST['msgContent']);

 

// Trial if then else reversed from Dan's original allowing multiple tests in sequence -- in action

 

if($guestName == "") {

$keepvar = "1";

$msg = "<p style='color: red; font-weight: bold; font-size: 14px; margin: 12px 0px 0px 0px;'>Please enter a name!</p>";

 

}  elseif(strlen($cityState) < 4) {

$msg = "<p style='color: red; font-weight: bold; font-size: 14px; margin: 12px 0px 0px 0px;'>Please enter a valid city & state!</p>";

 

}  elseif($emailAddress == "") {

$msg = "<p style='color: red; font-weight: bold; font-size: 14px; margin: 12px 0px 0px 0px;'>Please enter a valid email address!</p>";

 

// Oh HELP! }  elseif(strlen($msgContent) <5) {

// OH HELP! $msg = "<p style='color: red; font-weight: bold; font-size: 14px; margin: 12px 0px 0px 0px;'>Please enter a question!</p>";

 

 

} else {

mail($targetEmail, $msgSubject, $msgContent, "From: $guestName <$emailAddress>\nX-Mailer:PHP/" . phpversion());

$msg = "<p style='color: darkgreen; font-weight: bold; font-size: 14px; margin: 12px 0px 0px 0px;'>Thank you! Your message has been sent.</p>";

$brisket = "<img src='../images/Brisket180x180web.jpg' width='180px' height='180px' alt='Brisket Says Hi' />";

}

 

 

// Original if then else all commented out

 

// if($emailAddress != "") {

// mail($targetEmail, $msgSubject, $msgContent, "From: $guestName <$emailAddress>\nX-Mailer:PHP/" . phpversion());

// $msg = "<p style='color: darkgreen; font-weight: bold; font-size: 14px; margin: 12px 0px 0px 0px;'>Thank you! Your message has been sent.</p>";

// $brisket = "<img src='../images/Brisket180x180web.jpg' width='180px' height='180px' alt='Brisket Says Hi' />";

// } else {

// $msg = "<p style='color: red; font-weight: bold; font-size: 14px; margin: 12px 0px 0px 0px;'>Please enter a valid email address!</p>";

// }

}

}

 

?>

 

 

<body onload="loadMe('contact')">

 

 

<div id="container">

<div id="header">

<a href="../home/index.php" style="border:none"><div id="logo"></div></a>

 

<div id="navBG"><?php include("../common/mainNav.php"); ?></div>

</div><!--End Header-->

 

<div id="contentArea">

<br /><img class="sideBox" src="../images/sideBox.jpg" width="12px" height="12px"><p class="sideItemSelected">contact us</p><br style="clear:both" />

<p class="sideItem"><a class="sideLink" href="./shipping.php">useful q & a</a></p>

 

<div id="mainContent">

 

<h1>contact us</h1><br />

 

<h2>Have questions? Ready to order?<br />

<span style="color:#b5a072">Call 704.333.1595 Mon-Fri 9am - 5pm EST</span></h2>

<p id="contactText" class="text">It's not business, it's personal! For instant answers to many questions about ordering, shipping, and other good stuff, please check the <a      href="shipping.php">useful q&a</a> page.  Nonetheless, we'll always be happy to answer your questions by phone or through the form below. We look forward to hearing from you.</p><br />

 

<div id="contactFormContainer">

 

<form method="post" id="contactForm" action="<?php echo($_SERVER['PHP_SELF']); ?>" >

<div id="formContainer">

<input type="hidden" name="submitted" value="true" />

<input type="hidden" name="formname" value="contact" />

 

 

<div class="formRow">

 

<div class="itemSet">

<p class="itemLabel">name (required)</p>

<input tabindex="1" class="itemContent" type="text" name="guestName" value='<?php echo $_POST['guestName']; ?>' />

 

</div>

 

 

<div class="itemSet">

<p class="itemLabel">city & state (required)</p>

<input tabindex="2" class="itemContent" type="text" name="cityState" value='<?php echo $_POST['cityState']; ?>' />

</div>

 

<br class="clearMe" />

</div>

 

<div class="formRow">

<div class="itemSet">

<p class="itemLabel">email address (required)</p>

<input tabindex="3" class="itemContent" type="text" name="emailAddress" value='<?php echo $_POST['emailAddress']; ?>' />

</div>

 

<div class="itemSet">

<p class="itemLabel">phone & best local time to call</p>

<input tabindex="4" class="itemContent" type="text" name="phone" value='<?php echo $_POST['phone']; ?>' />

</div>

 

 

</div>

 

<div class="formRow">

<div>

<p class="itemLabel">your question</p>

 

<textarea tabindex="5" id="msgContent" type="text" name="msgContent"><?php echo $_POST['msgContent']; ?> </textarea></>

 

</div>

</div>

 

<div class="formRow">

<div style="position: relative;">

<div style="float: left; margin-right: 40px;">

<input tabindex="6" id="submitButton" type="submit" name="submit" value="Submit" />

</div>

 

<div style="float: left; margin-top: -10px;">

<?php //echo($msg); ?>

</div>

 

<br style="clear: both;" />

 

<div style="clear: both; position: absolute; top: -350px; left: -217px; width: 180px;">

<?php

echo($msg);

echo($brisket);

?>

</div>

 

 

<br style="clear: both;" />

</div>

</div>

 

</div> <!-- End Form Container -->

 

</form>

 

 

</div> <!-- End Contact Form -->

</div>

 

</div><!-- End Content Area-->

 

<div id="bottomBar"></div>

 

 

 

 

<?php include("../common/footer.php"); ?>

 

</div><!-- End Container -->

 

</body>

 

</html>

 

STOP CODE HERE =====================

 

 

please use code tags.

 

and instead of using

strlen($msgContent)

 

why dont you try using

strlen($_POST['msgContent'])

 

the reason being you set $msgContent to a value that will almost always have a length larger than 5

 

in case you dont know, you can use code tags like so

[code]
php and html code here

[/code]

OR

[code=php:0]
just php code here

[/code]

Thanks to both of you -- both of you wrote to the point and in a manner that was easily comprehended even though that section of PHP is new.  It works perfectly and will help us get more coherent requests from customers as well as reduce spam.  I really appreciate your time.

 

-- Joal

 

P.S.  Please keep fingers & toes crossed as I gingerly approach adding a reCAPTCHA.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.