Jump to content

PHP Using either Dropdown or Text field if "other"is in dropdown


Recommended Posts

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!

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'];

?>

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);
.........

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.

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.