Jump to content

Making mandatory fields in contact form


Andy626

Recommended Posts

Hi All,

 

Please excuse the newbie question but I am struggling with this one.

 

I'm just after some advice on what code to put in and where to make these eight fields mandatory to fill out in the contact page of our site.

 

I've found a lot of sites with code to put in but none seem to work.

 

Apologies for the basic question and appreciate any help given!

 

Here is the current form...

 

(Please ignore the promo and Toybox area as this was used for our sister company in the states)

 

 

<form method="post" action="db_send-contact-form.php">
<p class="style17">Your Name: <br />
<textarea name="name" rows="1" cols="40" id="name"></textarea>
</p>
<p class="style22">Company: <br />
<textarea name="company" rows="1" cols="40" id="company"></textarea>
</p>
<p class="style22">Email:<br />
<textarea name="email" rows="1" cols="40" id="email"></textarea>
</p>
<p class="style22">When are you looking at having this installed?<br />
<textarea name="when" rows="1" cols="40" id="when"></textarea>
</p>
<p class="style22">Do you have a Product Code in mind?<br />
<input type='text' name="code" style='width: 300px' id="code" value="<?PHP 
if(isset($_REQUEST['code'])) {
echo $_REQUEST['code'];
} 
if(isset($_REQUEST['toybox'])) {
if($_REQUEST['toybox'] == 1) {
echo "ToyBox: ". $_COOKIE['ToyBox'];
} else {
$uID = '';
if(isset($_SESSION['uID'])) {
$uID = $_SESSION['uID'];
}
echo "ToyBox: ". db_getToybox($_REQUEST['toybox'], $uID); 
}
} 
if($_SESSION['Promotion'] == "PROMO2012APR") { 
echo " | MUST INCLUDE SALE PRICE";
}
?>" />
</p>
<p class="style22">Phone:<br />
<textarea name="phone" rows="1" cols="40" id="phone"></textarea>
</p>
<p class="style22">City & State:<br />
<textarea name="state" rows="1" cols="40" id="state"></textarea>
</p>
<p class="style22">Any other info?<br />
<textarea name="message" rows="15" cols="40" id="message"></textarea>
 </p>
 <p><br>
 <input type="submit">
 </p>
</form>
Edited by Zane
Link to comment
Share on other sites

You should seperate your pages logic from the HTML, meaning you shouldnt be processing PHP in your HTML, but merely displaying it. Try to move all those IF statements to the top, like so:

<?php

if () 
{
  (...)
}

?>
<html>
</html>
Link to comment
Share on other sites

With all due respect if you don't understand the most basic elements of the PHP language, there's not much anyone can do for you here. That gobbledegook of code you posted seems to indicate as much.

 

There is a lot to form handling and validation ... so much to it that in frameworks, form classes are typically some of the most complex components included.

 

For example, a simple way to do it would be to write a "validateForm" function that includes a whitelist of required form names.

 

function validateForm($required) {
    //$required = array('name', 'company', 'email');

    $errors = array();

    foreach ($required as $name) {
        if (!isset($_POST[$name])) {
            $errors[] = "$name is required";
        } elseif (trim($_POST[$name]) == '') {
            $errors[] = "$name can not be blank";
        }
    }
    return $errors;
}
You could call this function in your form processing section and check whether count($errors) > 0. If so, you output the form again, typically including a div with a list of errors.

 

Needless to say you could add much more code to validate specific fields in a particular way (check that email is in a valid format, check that names have at least a certain number of characters, etc) but I'm not going to go overboard in this reply. If you could integrate the snippet I provided you might be able to get this done, however if not, you really need to get the basics under your belt, or find someone more experienced to write it for you.

Link to comment
Share on other sites

  • 4 weeks later...
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.