Bricktop Posted October 10, 2008 Share Posted October 10, 2008 Hello ppl!, OK, I have a simple form which is structured as follows: $content .= '<tr><td>Full Name</td><td><input name="Name" type="text" value="'.$_POST['Name'].'" size="32" maxlength="32" class="text" /></td></tr>'; $content .= '<tr><td>Telephone</td><td><input name="Telephone" type="text" value="'.$_POST['Telephone'].'" size="32" maxlength="32" class="text" /></td></tr>'; $content .= '<tr><td>Email Address</td><td><input name="Email" type="text" value="'.$_POST['Email'].'" size="32" maxlength="50" class="text" /></td></tr>'; echo $content; This works great and when the user presses submit and there is an error on the form, the page refeshes, flashes the error reason (extra PHP code I haven't included handles this), and the fields remember what has been typed into them (because of the value="'.$_POST['xxxx'].'" syntax) - this is great because on a form error the user doesn't have to retype the data. However, now I have added a drop-down box to the form and I cannot work out how I can get the drop-down box to remember the selection on a form error. If people fill in the form, press submit and the form errors, the select box resets back to the default selection. Essentially, the new code is: $content .= '<tr><td>Full Name</td><td><input name="Name" type="text" value="'.$_POST['Name'].'" size="32" maxlength="32" class="text" /></td></tr>'; $content .= '<tr><td>Telephone</td><td><input name="Telephone" type="text" value="'.$_POST['Telephone'].'" size="32" maxlength="32" class="text" /></td></tr>'; $content .= '<tr><td>Email Address</td><td><input name="Email" type="text" value="'.$_POST['Email'].'" size="32" maxlength="50" class="text" /></td></tr>'; $content .= '<tr><td>County</td><td<SELECT NAME="txtCounty" SIZE="1"> <option VALUE="Please select your county">Please select your county</option> <OPTION VALUE="Aberdeenshire">Aberdeenshire</OPTION> <OPTION VALUE="Angus">Angus</OPTION> <OPTION VALUE="Argyll">Argyll</OPTION> <OPTION VALUE="Avon">Avon</OPTION></SELECT></td></tr>'; echo $content; Hopefully my request makes sense, does anyone know how I can achieve this with PHP please? Or do I need to use JS? Quote Link to comment Share on other sites More sharing options...
budimir Posted October 10, 2008 Share Posted October 10, 2008 Try Google for "Sticky forms" Quote Link to comment Share on other sites More sharing options...
CroNiX Posted October 10, 2008 Share Posted October 10, 2008 something like this should work: <?php $options = generateOptions($_POST['txtCountry']); $content .= '<tr><td>Full Name</td><td><input name="Name" type="text" value="'.$_POST['Name'].'" size="32" maxlength="32" class="text" /></td></tr>'; $content .= '<tr><td>Telephone</td><td><input name="Telephone" type="text" value="'.$_POST['Telephone'].'" size="32" maxlength="32" class="text" /></td></tr>'; $content .= '<tr><td>Email Address</td><td><input name="Email" type="text" value="'.$_POST['Email'].'" size="32" maxlength="50" class="text" /></td></tr>'; $content .= '<tr><td>County</td><td<SELECT NAME="txtCounty" SIZE="1">'; $content .= $options; $content .= '</SELECT></td></tr>'; function generateOptions($curSelect) { $opts=array( 'Please select your country', 'Aberdeenshire', 'Angus', 'Argyll', 'Avon' ); $options=""; foreach($opts as $name) { $sel = ($name == $curSelect) ? ' selected="selected"' : ""; $options .= "<OPTION VALUE=\"$name\"$sel>$name</OPTION>\n"; } return $options; } Quote Link to comment Share on other sites More sharing options...
CroNiX Posted October 10, 2008 Share Posted October 10, 2008 although this: <?php $options = generateOptions($_POST['txtCountry']); should be changed to this: <?php $selOption = isset($_POST['txtCountry']) ? $_POST['txtCountry'] : ""; $options = generateOptions($selOption); in case it hasn't been submitted yet Quote Link to comment Share on other sites More sharing options...
Bricktop Posted October 11, 2008 Author Share Posted October 11, 2008 Excellent, thanks chaps! 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.