jay7981 Posted November 30, 2009 Share Posted November 30, 2009 i have a form that updates a mysql table and i currently only have a dropdown that is hard coded with options, what i would like to do is have it to where if i choose "other" in the dropdown it will create a textfield and use that as the information being passed on to the form processor, i know this can be done with DOM just not sure how. any examples would be great Thanks a ton for all the help! Quote Link to comment https://forums.phpfreaks.com/topic/183464-php-using-either-dropdown-or-text-field-if-otheris-in-dropdown/ Share on other sites More sharing options...
premiso Posted November 30, 2009 Share Posted November 30, 2009 Moved to Javascript as this is not a PHP question. Quote Link to comment https://forums.phpfreaks.com/topic/183464-php-using-either-dropdown-or-text-field-if-otheris-in-dropdown/#findComment-968443 Share on other sites More sharing options...
jay7981 Posted November 30, 2009 Author Share Posted November 30, 2009 thank you. Quote Link to comment https://forums.phpfreaks.com/topic/183464-php-using-either-dropdown-or-text-field-if-otheris-in-dropdown/#findComment-968453 Share on other sites More sharing options...
Psycho Posted November 30, 2009 Share Posted November 30, 2009 Here's a quick example of how the form might work: <html> <head> <title></title> <script language="javascript"> function showOther(fieldObj, otherFieldID) { var fieldValue = fieldObj.options[fieldObj.selectedIndex].value; var otherFieldObj = document.getElementById(otherFieldID); otherFieldObj.style.visibility = (fieldValue=='--other--') ? '' : 'hidden'; return; } </script> </head> <body> <form name="form1" method="post"> Choose a color: <select name="color" onchange="showOther(this, 'other_color');"> <option value="Red">Red</option> <option value="Green">Green</option> <option value="Blue">Blue</option> <option value="Purple">Purple</option> <option value="Yellow">Yellow</option> <option value="--other--">--Other--</option> </select> <input type="text" name="other_color" id="other_color" style="visibility:hidden;"> <br /><br /> Choose a number: <select name="number" onchange="showOther(this, 'other_number');"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="--other--">--Other--</option> </select> <input type="text" name="other_number" id="other_color" style="visibility:hidden;"> </form> </body> </html> Then in your processing page, you just check for the 'other' selection and set the value accordingly: <?php $color = ($_POST['color']=='--other--') ? $_POST['other_color'] : $_POST['color']; $number = ($_POST['number']=='--other--') ? $_POST['other_number'] : $_POST['number']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/183464-php-using-either-dropdown-or-text-field-if-otheris-in-dropdown/#findComment-968532 Share on other sites More sharing options...
jay7981 Posted December 1, 2009 Author Share Posted December 1, 2009 Firstly, thank you so much for the code, i will make sure to credit you in the code. also it works to show the textfield, however im not sure why but it is saving "--other--" in the db instead of the what i type the textfield. any ideas as to why? below is the code. The Form <tr> <td>Access Flags:</td> <td><select name='access' id='access' onchange="showOther(this, 'other_access');"> <option selected>Select Level</option> <option value='abcdefghijklmnopqrstu'>Leader/CoLeader</option> <option value='bcefijnprstu'>Tech</option> <option value='bcdefijmnopqrstu'>Upper Admin</option> <option value='bcefijnprstu'>Mid Admin</option> <option value='cfu'>Recruit Admin</option> <option value='u'>Member</option> <option value="--other--">--Other--</option> </select></td> </tr> <tr> <td><input type="text" name="other_access" id="other_access" style="visibility:hidden;"> </td> <td> </td> </tr> the processor <?php $auth = $_POST['auth']; $name = $row['name']; //$access = $_POST['access']; $access = ($_POST['access']=='--other--') ? $_POST['other_access'] : $_POST['access']; mysql_free_result($result); ......... Quote Link to comment https://forums.phpfreaks.com/topic/183464-php-using-either-dropdown-or-text-field-if-otheris-in-dropdown/#findComment-968737 Share on other sites More sharing options...
Psycho Posted December 1, 2009 Share Posted December 1, 2009 Where's your query to insert the record? I can only suspect that you are using the POST value ($_POST['access']) instead of the variable $access in your query. Also, since you have an option for "Select level" I hope you are implementing logic to prevent that value from being processed. Quote Link to comment https://forums.phpfreaks.com/topic/183464-php-using-either-dropdown-or-text-field-if-otheris-in-dropdown/#findComment-969078 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.