Gainax Posted June 4, 2009 Share Posted June 4, 2009 Hi I have a form with 3 input fileds. I want to have them all be made mandatory. So If someone submits the form, if the fields are empty display an error. I want each error to display underneath the input field. How do I do this? Thanks Quote Link to comment Share on other sites More sharing options...
phpretard Posted June 4, 2009 Share Posted June 4, 2009 Post your HTML form Quote Link to comment Share on other sites More sharing options...
Gainax Posted June 4, 2009 Author Share Posted June 4, 2009 <div class="enquiry-form"> <h1>Enquiry Form</h1> <p> </p> <form action="contact_process.php" id="contactform" name="contactform" method="post" action=""> <label for="title">Title:*</label><br /> <input name="title" class="text" type="text" /><br /> <label for="firstname">First Name:*</label><br /> <input name="firstname" class="text" type="text" /><br /> <label for="comments">Title:</label><br /> <textarea name="comments" class="text" name="comments" cols="30" rows="5"></textarea><br /><br /> <p class="form-small-text">* denotes mandatory fields </p> <input id="time-input" type="image" src="images/send_request.jpg" alt="" title="" /> </form> <p> </p> </div> Quote Link to comment Share on other sites More sharing options...
kickstart Posted June 4, 2009 Share Posted June 4, 2009 Hi Something similar to this:- <?php $Error = false; if ($_REQUEST['submit']) { if (!isset($_REQUEST['field1])) { $Error = true; $field1Error = "Field is required"; } if (!isset($_REQUEST['field2])) { $Error = true; $field2Error = "Field is needed"; } if (!isset($_REQUEST['field3])) { $Error = true; $field3Error = "Field is must be entered"; } if (!$Error) { // Do something with data } else { ?><form> <input type='text' name='field1' /><br /> <?php echo (($isset($field1Error)) ? $field1Error."<br />":""); ?> <input type='text' name='field2' /><br /> <?php echo (($isset($field2Error)) ? $field2Error."<br />":""); ?> <input type='text' name='field3' /><br /> <?php echo (($isset($field3Error)) ? $field3Error."<br />":""); ?> <input type='submit' name='submit' /><br /> </form> ?> } } else { ?><form> <input type='text' name='field1' /><br /> <input type='text' name='field1' /><br /> <input type='text' name='field1' /><br /> <input type='submit' name='submit' /><br /> </form> ?> } This can easily be cleaned up should you want to (and there is almost certainly some typos in it), but left it like this to give you the basic idea. All the best Keith Quote Link to comment Share on other sites More sharing options...
xcoderx Posted June 4, 2009 Share Posted June 4, 2009 Rip the code from here http://www.4serendipity.com/form/form.phps and make it work with ur script Quote Link to comment Share on other sites More sharing options...
Gainax Posted June 4, 2009 Author Share Posted June 4, 2009 Where will my form go in that code xcoderx? Thanks Quote Link to comment Share on other sites More sharing options...
Gainax Posted June 4, 2009 Author Share Posted June 4, 2009 Ok, so I found a tutorial I have the following code: <div class="enquiry-form-obligation"> <?if( isset($_POST['send']) && (!validateName($_POST['name']) || !validateAddress($_POST['address']) || !validateEmail($_POST['email']) || !validateTelephone($_POST['telephone']) || !validateIncome($_POST['income']) ) ):?> <div id="error"> <ul> <?if(!validateName($_POST['name'])):?> <li><strong>Invalid Name:</strong>Your name must be at leasr 3 letters</li> <?endif?> <?if(!validateAddress($_POST['address'])):?> <li><strong>Invalid Address:</strong> Please enter your full address</li> <?endif?> <?if(!validateEmail($_POST['email'])):?> <li><strong>Ivalid email:</strong> Please enter a valid email address</li> <?endif?> <?if(!validateTelephone($_POST['telephone'])):?> <li><strong>Invalid number:</strong> You number must be at least 11 numbers</li> <?endif?> <?if(!validateIncome($_POST['income'])):?> <li><strong>Invalid income:</strong> Please enter your monthly income</li> <?endif?> </ul> </div> <?elseif(isset($_POST['send'])):?> <div id="error" class="valid"> <ul> <?php header('Location: thankyou.html'); ?> </ul> </div> <?endif?> <form action="" id="contactform" name="contactform" method="post" action=""> <label for="name">Name:*</label><br /> <input name="name" class="text" type="text" id="name" value="<?=$_POST['name'] ?>" /><br /> <label for="company">Company Name (if applicable)</label><br /> <input name="company" class="text" type="text" value="<?=$_POST['company'] ?>" /><br /> <label for="address">Address:*</label><br /> <textarea name="address" class="text" name="address" cols="30" rows="5" value="<?=$_POST['address'] ?>" ></textarea><br /><br /> <label for="postcode">Postcode:</label><br /> <input name="postcode" class="text" type="text" value="<?=$_POST['postcode'] ?>"/><br /> <label for="email">Email:*</label><br /> <input name="email" class="text" type="text" value="<?=$_POST['email'] ?>" /><br /> <label for="telephone">Tel:*</label><br /> <input name="telephone" class="text" type="text" value="<?=$_POST['telephone'] ?>" /><br /><br /> <p><strong>Your Status</strong></p><br /> <input type="radio" name="status" value="ltd"> <label for="postcode">Ltd company:</label><br /> <input type="radio" name="status" value="sole"> <label for="postcode">Sole Trader:</label><br /> <input type="radio" name="status" value="partnership"> <label for="postcode">Partnership:</label><br /> <input type="radio" name="status" value="individual"> <label for="postcode">Individual:</label><br /><br /> <p><strong>About your Liabilities</strong></p> <div style="width: 780px; height: 150px; "> <div style="width: 280px; float: left;"> <label for="credit1">Name of Creditor:</label><br /> <textarea name="credit1" class="text" name="credit1" cols="30" rows="5" style="float: left;" value="<?=$_POST['credit1'] ?>"></textarea><br /><br /> </div> <label for="owings">Total Amount Owings £'s:</label><br /> <textarea name="owings" class="text" name="owings" cols="30" rows="5" style="float: left;" value="<?=$_POST['owings'] ?>" ></textarea><br /><br /> </div> <p><strong>About your Assets </strong></p> <div style="width: 880px; height: 150px; "> <div style="width: 280px; float: left;"> <label for="credit2">Name of Creditor:</label><br /> <textarea name="credit2" class="text" name="credit2" cols="30" rows="5" style="float: left;" value="<?=$_POST['credit2'] ?>"></textarea><br /><br /> </div> <div style="width: 280px; float: left;"> <label for="valuation">Current Valuation:</label><br /> <textarea name="valuation" class="text" name="valuation" cols="30" rows="5" style="float: left;" value="<?=$_POST['valuation'] ?>"></textarea><br /><br /> </div> <div style="width: 280px; float: left;"> <label for="finance">Outstanding Finance:</label><br /> <textarea name="finance" class="text" name="finance" cols="30" rows="5" style="float: left;" value="<?=$_POST['finace'] ?>"></textarea><br /><br /> </div> </div> <label for="income">Monthly Income:*</label><br /> <input name="income" class="text" type="text" value="<?=$_POST['income'] ?>" /><br /><br /> <p class="form-small-text">* denotes mandatory fields </p> <input id="send" name="send" type="submit" value="Send" /> </form> <p> </p> </div> You can see, I want it to redirect to the thank you page when all the fields are filled out correctly. But I get the following error: Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\CRAIG\frazer\obligation_review.php:9) in C:\xampp\htdocs\CRAIG\frazer\obligation_review.php on line 152 Any ideas how to get around this? When all the fields Quote Link to comment Share on other sites More sharing options...
kickstart Posted June 4, 2009 Share Posted June 4, 2009 Hi Once you have sent anything (even a space) to the browser you cannot redirect to another script. You are outputting all the DIVs, etc, as you do the validation. You need to do the validation first and then output a screen based on the result or redirect to your thankyou script. All the best Keith Quote Link to comment Share on other sites More sharing options...
Gainax Posted June 4, 2009 Author Share Posted June 4, 2009 You need to do the validation first and then output a screen based on the result or redirect to your thankyou script. All the best Keith Ok, so how will I modify my script to have this redirect once validation is completed? Quote Link to comment Share on other sites More sharing options...
kickstart Posted June 4, 2009 Share Posted June 4, 2009 Hi Something like this:- <?php if( isset($_POST['send']) { $nameOK = validateName($_POST['name']); $addressOK = validateAddress($_POST['address']); $emailOK = validateEmail($_POST['email']); $telephoneOK = validateTelephone($_POST['telephone']); $incomeOK = validateIncome($_POST['income']); if ($nameOK AND $addressOK AND $emailOK AND $telephoneOK AND $incomeOK) { header('Location: thankyou.html'); } } else { $nameOK = true; $addressOK = true; $emailOK = true; $telephoneOK = true; $incomeOK = true; } ?> <form action="" id="contactform" name="contactform" method="post" action=""> <label for="name">Name:*</label><br /> <input name="name" class="text" type="text" id="name" value="<?=$_POST['name'] ?>" /><br /> <?php echo (($nameOK) ? "" : "<strong>Invalid Name:</strong>Your name must be at leasr 3 letters<br />"); ?> <label for="company">Company Name (if applicable)</label><br /> <input name="company" class="text" type="text" value="<?=$_POST['company'] ?>" /><br /> <label for="address">Address:*</label><br /> <textarea name="address" class="text" name="address" cols="30" rows="5" value="<?=$_POST['address'] ?>" ></textarea><br /><br /> <?php echo (($addressOK) ? "" : "<strong>Invalid Address:</strong> Please enter your full address<br />"); ?> <label for="postcode">Postcode:</label><br /> <input name="postcode" class="text" type="text" value="<?=$_POST['postcode'] ?>"/><br /> <label for="email">Email:*</label><br /> <input name="email" class="text" type="text" value="<?=$_POST['email'] ?>" /><br /> <?php echo (($emailOK) ? "" : "<strong>Ivalid email:</strong> Please enter a valid email address<br />"); ?> <label for="telephone">Tel:*</label><br /> <input name="telephone" class="text" type="text" value="<?=$_POST['telephone'] ?>" /><br /><br /> <?php echo (($telephoneOK) ? "" : "<strong>Invalid number:</strong> You number must be at least 11 numbers<br />"); ?> <p><strong>Your Status</strong></p><br /> <input type="radio" name="status" value="ltd"> <label for="postcode">Ltd company:</label><br /> <input type="radio" name="status" value="sole"> <label for="postcode">Sole Trader:</label><br /> <input type="radio" name="status" value="partnership"> <label for="postcode">Partnership:</label><br /> <input type="radio" name="status" value="individual"> <label for="postcode">Individual:</label><br /><br /> <p><strong>About your Liabilities</strong></p> <div style="width: 780px; height: 150px; "> <div style="width: 280px; float: left;"> <label for="credit1">Name of Creditor:</label><br /> <textarea name="credit1" class="text" name="credit1" cols="30" rows="5" style="float: left;" value="<?=$_POST['credit1'] ?>"></textarea><br /><br /> </div> <label for="owings">Total Amount Owings £'s:</label><br /> <textarea name="owings" class="text" name="owings" cols="30" rows="5" style="float: left;" value="<?=$_POST['owings'] ?>" ></textarea><br /><br /> </div> <p><strong>About your Assets </strong></p> <div style="width: 880px; height: 150px; "> <div style="width: 280px; float: left;"> <label for="credit2">Name of Creditor:</label><br /> <textarea name="credit2" class="text" name="credit2" cols="30" rows="5" style="float: left;" value="<?=$_POST['credit2'] ?>"></textarea><br /><br /> </div> <div style="width: 280px; float: left;"> <label for="valuation">Current Valuation:</label><br /> <textarea name="valuation" class="text" name="valuation" cols="30" rows="5" style="float: left;" value="<?=$_POST['valuation'] ?>"></textarea><br /><br /> </div> <div style="width: 280px; float: left;"> <label for="finance">Outstanding Finance:</label><br /> <textarea name="finance" class="text" name="finance" cols="30" rows="5" style="float: left;" value="<?=$_POST['finace'] ?>"></textarea><br /><br /> </div> </div> <label for="income">Monthly Income:*</label><br /> <input name="income" class="text" type="text" value="<?=$_POST['income'] ?>" /><br /><br /> <?php echo (($incomeOK) ? "" : "<strong>Invalid income:</strong> Please enter your monthly income<br />"); ?> <p class="form-small-text">* denotes mandatory fields </p> <input id="send" name="send" type="submit" value="Send" /> </form> Probably a few typos and no attempt at formatting there. Basic idea. Validate the input data. If it is all OK then process is and / or redirect to your next script. If it isn't, or entry to the script was not from pressing the send button (ie, first time in) then output the form, complete with required error messages. All the best Keith Quote Link to comment Share on other sites More sharing options...
Gainax Posted June 4, 2009 Author Share Posted June 4, 2009 That works great! The only thing I need to do now, is before the redirect insert the values into a db Any ideas about this? Quote Link to comment Share on other sites More sharing options...
kickstart Posted June 4, 2009 Share Posted June 4, 2009 Hi Just build up the INSERT sql string and execute it just before you do the header statement. The INSERT shouldn't output anything to the "screen" so shouldn't interfere with the header redirect. All the best Keith Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.