sawade Posted July 29, 2009 Share Posted July 29, 2009 [pre]Hello, I am creating a self processing php form. I have been working on this form for sometime now, updating and revising it nearly daily. We are now looking to make the form "sticky" whereby should a client not input a required field the form will tell them so and still retain all previously input information. I have no problem doing this with text and textarea fields. My issue is with radio, checkbox, and dropdown menu fields. I will include an example of the coding for the text and textarea fields. <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <table> <tr> <th><label for="name">Name:</label> </th> <td><input type="text" id="name" name="name" value="<?php if (!empty($name)) echo $name; ?>" /> </td> </tr> <tr> <th><label for="phone">Phone Number:</label> </th> <td><input type="text" id="phone" name="phone" value="<?php if (!empty($phone_number)) echo $phone_number; ?>" /> </td> </tr> <tr> <th><label for="email">E-mail Address:</label> </th> <td><input type="text" id="email" name="email" value="<?php if (!empty($email_address)) echo $email_address; ?>" /> </td> </tr> <tr> <th><label for="comment">Question or Comment:</label> </th> <td><textarea name="comment" id="comment" rows="10" cols="40"><?php if (!empty($question_comment)) echo $question_comment; ?></textarea> </td> </tr> <tr> <td colspan="2"><label for="verify">Verification: </label><em>(Input <strong>EXACTLY</strong> as shown)</em> <input type="text" id="verify" name="verify" value="" /> <img src="captcha.php" alt="Verification pass-phrase" /> </td> </tr> <tr> <td colspan="2"><input type="submit" value="Submit" name="submit" /> </td> </tr> </table> </form> Here is an example of a radio field from the form we are working: <td colspan="2"><em>*</em> Is this your legal name?<br /> <input type="radio" name="islegalname" id="islegalyes" value="This is my legal name." /><label for="islegalyes">Yes</label> <input type="radio" name="islegalname" id="islegalno" value="This is NOT my legal name." /><label for="islegalno">No</label> </td> Here is an example of a dropdown menu from the form: <td><label for="suffix">Suffix</label><br /> <select name="suffix" id="suffix"> <option value=""></option> <option value="Sr">Sr</option> <option value="Jr">Jr</option> <option value="II">II</option> <option value="III">III</option> <option value="IV">IV</option> </select> </td> And finally an example of a checkbox section of the form: <td>Allergic reaction to: <ul> <li><input type="checkbox" name="allergy[]" id="allergy01" multiple="multiple" value="None" /> <label for="allergy01">None</label></li> <li><input type="checkbox" name="allergy[]" id="allergy02" multiple="multiple" value="Aspirin, Ibuprofen, Acetaminophen" /> <label for="allergy02">Aspirin, Ibuprofen, Acetaminophen</label></li> <li><input type="checkbox" name="allergy[]" id="allergy03" multiple="multiple" value="Penicillin" /> <label for="allergy03">Penicillin</label></li> <li><input type="checkbox" name="allergy[]" id="allergy04" multiple="multiple" value="Erythromycin" /> <label for="allergy04">Erythromycin</label></li> <li><input type="checkbox" name="allergy[]" id="allergy05" multiple="multiple" value="Tetracycline" /> <label for="allergy05">Tetracycline</label></li> <li><input type="checkbox" name="allergy[]" id="allergy06" multiple="multiple" value="Codeine" /> <label for="allergy06">Codeine</label></li> <li><input type="checkbox" name="allergy[]" id="allergy07" multiple="multiple" value="Local anesthetic" /> <label for="allergy07">Local anesthetic</label></li> <li><input type="checkbox" name="allergy[]" id="allergy08" multiple="multiple" value="Fluoride" /> <label for="allergy08">Fluoride</label></li> <li><input type="checkbox" name="allergy[]" id="allergy09" multiple="multiple" value="Metals" /> <label for="allergy09">Metals (gold, stainless steel, etc.)</label></li> <li><input type="checkbox" name="allergy[]" id="allergy10" multiple="multiple" value="Latex" /> <label for="allergy10">Latex</label></li> <li><label for="allergy11">Any other medications </label> <input type="text" name="allergy[]" id="allergy11" multiple="multiple" value="" /></li> </ul> </td> Thank you for any help! [/pre] Link to comment https://forums.phpfreaks.com/topic/168010-solved-php-form-w-sticky-fields/ Share on other sites More sharing options...
soycharliente Posted July 29, 2009 Share Posted July 29, 2009 Basically you need to capture the state and put a PHP test inside the input tag. Here's some example code from my personal site that does what you're asking. Hope this helps you. The section I give is just for someone to choose a month, day, and year from three different drop-down menus. Once you start plugging it in, if you run into problems, come back and show some code. <?php if (isset($_POST['Submit']) && $_POST['Submit']=="Submit") { $y = $_POST['Year']; $m = $_POST['Month']; $d = $_POST['Day']; $ymd = array($y,$m,$d); $when = implode('-',$ymd); } ?> <html> <body> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <p> <select name="Month"> <?php $i = 1; while ($i <= 12) { $selected = ($m == $i) ? ' selected="selected"' : ''; echo '<option value="'.$i.'"'.$selected.'>'.date('F', mktime(0,0,0,$i++)).'</option>'; } ?> </select> <select name="Day"> <?php $i = 1; while ($i <= 31) { $selected = ($d == $i) ? ' selected="selected"' : ''; echo '<option value="'.$i.'"'.$selected.'>'.$i++.'</option>'; } ?> </select> , <select name="Year"> <?php $i = 2005; while ($i <= date('Y')) { $selected = ($y == $i) ? ' selected="selected"' : ''; echo '<option value="'.$i.'"'.$selected.'>'.$i++.'</option>'; } ?> </select> <label for="When" id="When">when</label> </p> <p> <input type="submit" name="Submit" value="Submit" /> </p> </form> </body> </html> Link to comment https://forums.phpfreaks.com/topic/168010-solved-php-form-w-sticky-fields/#findComment-886133 Share on other sites More sharing options...
cleibesouza Posted July 29, 2009 Share Posted July 29, 2009 For the checkboxes and radio buttons you could do this: if($_POST['islegalname']){ echo " checked='checked' "; }//end if A word on validation: I always validate on the client side before validating on the server side. It's my belief that it avoids unnecessary hits on the server, but both validations are important in case javascript is turned off. There are some neat ways of validating using Ajax out there. Link to comment https://forums.phpfreaks.com/topic/168010-solved-php-form-w-sticky-fields/#findComment-886138 Share on other sites More sharing options...
sawade Posted July 29, 2009 Author Share Posted July 29, 2009 [pre]I have seen example codes like this... is this a good way to go?? 1.$name = $_POST['name']; 2.$gender = $_POST['gender']; 3.$howlong = $_POST['howlong']; 4. 5.**************** 6. 7.function sticky($field) { 8.if(isset($field)) 9. return $field; 10.} 11.function stickyradio($name,$value) { 12.if ($name == $value) { 13. return ' checked="checked" '; 14.} 15.} 16. 17.function stickymenu($name,$value) { 18.if ($name == $value) { 19. return ' selected="selected" '; 20.} 21.} 22. 23.*************** 24. 25.<input type="text" name="name" value="'.sticky($name).'" /> 26.<input type="radio" name="gender"'.stickyradio($gender,'male').'value="male" /> 27.<option value="1y"'.stickymenu($howlong,'1y').'>1 year</option> [/pre] Link to comment https://forums.phpfreaks.com/topic/168010-solved-php-form-w-sticky-fields/#findComment-886140 Share on other sites More sharing options...
soycharliente Posted July 29, 2009 Share Posted July 29, 2009 That's basically it except they wrote functions to do it. I never wrote a function. Link to comment https://forums.phpfreaks.com/topic/168010-solved-php-form-w-sticky-fields/#findComment-886143 Share on other sites More sharing options...
sawade Posted July 29, 2009 Author Share Posted July 29, 2009 Yeah, the company that wants the form is not interested in javascript, they want everything in php. Thanks for the thought though. Would that if statement be input just like the text fields? Link to comment https://forums.phpfreaks.com/topic/168010-solved-php-form-w-sticky-fields/#findComment-886146 Share on other sites More sharing options...
cleibesouza Posted July 29, 2009 Share Posted July 29, 2009 It can go inside your input field <input type="radio" name="islegalname" id="islegalyes" value="This is my legal name." <? if($_POST['islegalname']){ echo " checked='checked' "; }//end if ?> > Link to comment https://forums.phpfreaks.com/topic/168010-solved-php-form-w-sticky-fields/#findComment-886149 Share on other sites More sharing options...
sawade Posted August 4, 2009 Author Share Posted August 4, 2009 [pre]Still trying to get this working. I have put together a few codes using pieces of the examples given here and some I have seen elsewhere. This is what I have put together. Are these workable? Radio: <td><em>*</em> Sex:<br /> <input type="radio" name="sex" id="male" value="male" <?php if(!empty($gender)) echo ' checked="checked"'; ?> /><label for="male">M</label> <input type="radio" name="sex" id="female" value="female" <?php if(!empty($gender)) echo ' checked="checked"'; ?> /><label for="female">F</label></td> Checkbox: <td> <input type="checkbox" name="history[]" id="history01" multiple="multiple" value="Hospitalization for illness or injury" <?php if (isset ($_POST['history']) && in_array ("Hospitalization for illness or injury", $_POST['history'])) echo ' checked="checked"'; ?>/> <label for="history01">Hospitalization for illness or injury</label> </td> Dropdown menu: <td><label for="prefix">Prefix</label><br /> <select name="prefix" id="prefix" <?php if(!empty($prefix)) echo ' selected="selected"'; ?>> <option value=""></option> <option value="Mr.">Mr</option> <option value="Miss.">Miss</option> <option value="Mrs.">Mrs</option> <option value="Ms.">Ms</option> </select></td> Link to comment https://forums.phpfreaks.com/topic/168010-solved-php-form-w-sticky-fields/#findComment-890774 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.