问题描述:

i have a drop down list and a text box. there is names of animals in drop down list. problem is where, there may not a name for choosing in drop down list so when the requested name does not exit, user should can type his/her favorite animal in text box after checked check box(when check box checked, drop down list should be disabled and when unchecked check box should disabled ). and in last step, send the value of drop down list or text box to data base.

<form name="f" action="" method="post">

echo"choose an animal".'<br/>';

echo '<select name="animals">';

echo'<option> bear </option>';

echo'<option> cat </option>';

echo'<option> lion </option>';

echo'<option> monkey </option>';

echo'</select>';

echo"or write if is not in list".'<br/>';

echo'<input type="checkbox" name="checkbox">';

echo'<input type="textbox" name="animals">';

echo'<input type="submit" name="submit" value="submit">';

</form>

if(isset($_POST['submit']))

{

$db_host = 'localhost';

$db_name= 'site';

$db_table= 'animals';

$db_user = 'root';

$db_pass = '';

$con = mysql_connect($db_host,$db_user,$db_pass) or die("خطا در اتصال به پايگاه داده");

$selected=mysql_select_db($db_name, $con) or die("خطا در انتخاب پايگاه داده");

$ins = "INSERT INTO $db_table (animal_name)

VALUES ('" . mysql_escape_string($_POST['animals']) . "')";

$saved=mysql_query($ins );

mysql_close($con);

}

网友答案:
<?php
    echo"choose an animal".'<br/>';
    echo '<select name="animals" id="animalDropdown">';
    echo'<option> bear </option>';
    echo'<option> cat </option>';
    echo'<option> lion </option>';
    echo'<option> monkey </option>';
    echo'</select>';

    echo"or write if is not in list".'<br/>';
    echo'<input type="checkbox" name="checkbox" id="checkbox" onclick="onClickCheckbox()">';
    echo'<input type="textbox" name="animalstext" id="animals">';
    ?>

    <script>
    onClickCheckbox();

    function onClickCheckbox( )
    {
        if( document.getElementById("checkbox").checked == true )
        {
            document.getElementById("animalDropdown").disabled = true;
            document.getElementById("animals").disabled = false;
        }
        else
        {
            document.getElementById("animalDropdown").disabled = false;
            document.getElementById("animals").disabled = true;
        }
    }
    </script>

This does the active/deactive job. Use the rest of your PHP code in order to insert the data in database. By the way I guess you need a submit button as well.

网友答案:

Use the checked attribute to enable or disable given fields.

<script>
function onClickCheckbox( )
{
    if( document.getElementById("checkbox").checked == true )
        document.getElementById("animalDropdown").disabled = true;
    else
        document.getElementById("animalDropdown").disabled = false;
}
</script>

<select name="animalDropdown"> ... </select>
<input type="checkbox" id="checkbox" onclick="onClickCheckbox()" value="1" checked="" />

Don't use the same ID or Name for two different inputs. Use the PHP script to differentiate between the two.

相关阅读:
Top