I'm trying to get a php/javascript dynamically populated combo box working through mysql queries and to the most part it works fine, that is until I hit the submit button. The scripts do exactly what I want up to this point.

From what I can see the replacement code is left out of the $_POST array. I assume this is happening because once the form loads it lists what fields to expect or creates the $_post array on the spot, therefore by inserting some new code it perhaps ignores it? Just a guess but seems logical, if so, how would one work around this? This has plagued me for a month or so now.

javascript:

<script type="text/javascript">
    $(document).ready(subpos_selectbox_change);
    function subpos_selectbox_change(){
        $('#subpos').change(update_position_number);
    }
    function update_position_number(){
        var subpos=$('#subpos').attr('value');
        $.get('get_list.php?subpos='+subpos, show_posnumbers);
    }
    function show_posnumbers(ss){
        $('#position_number').html(ss);
    }
</script>

get_list.php

switch($_REQUEST['subpos']){

    case 'AO2';
        $teamQuery = "SELECT * FROM establishment_positions WHERE position_filled='0' AND active='1' AND position_acrynom='AO2'";
        break;

    case 'AO3';
        $teamQuery = "SELECT * FROM establishment_positions WHERE position_filled='0' AND active='1' AND position_acrynom='AO3'";
        break;

    case 'AO4';
        $teamQuery = "SELECT * FROM establishment_positions WHERE position_filled='0' AND active='1' AND position_acrynom='AO4'";
        break;

    case 'AO5';
        $teamQuery = "SELECT * FROM establishment_positions WHERE position_filled='0' AND active='1' AND position_acrynom='AO5'";
        break;

    case 'AO6';
        $teamQuery = "SELECT * FROM establishment_positions WHERE position_filled='0' AND active='1' AND position_acrynom='AO6'";
        break;

    case 'AO7';
        $teamQuery = "SELECT * FROM establishment_positions WHERE position_filled='0' AND active='1' AND position_acrynom='AO7'";
        break;

    case 'AO8';
        $teamQuery = "SELECT * FROM establishment_positions WHERE position_filled='0' AND active='1' AND position_acrynom='AO8'";
        break;

    case 'SO1';
        $teamQuery = "SELECT * FROM establishment_positions WHERE position_filled='0' AND active='1' AND position_acrynom='SO1'";
        break;

}

echo '<select name="pos_number" id="pos_number">';
$ss = null;
$result = mysql_query($teamQuery);
while($row = mysql_fetch_array($result))
{
    $ss.= '<option value=' . $row['position_id'] . '>' . $row['position_number'] . '</option>';

}
echo $ss;
echo '</select>';

..and here id the html code before the javascript goes to work:

<div class="newusr">
    <table>
        <form id="newusr" action="includes/insertuser.php" method="post">
            <tr><td>First Name: </td><td><input type="text" name="firstname"></td></tr>
            <tr><td>Middle Name: </td><td><input type="text" name="middlename"></td></tr>
            <tr><td>Last Name: </td><td><input type="text" name="lastname"></td></tr>
            <tr><td>Username: </td><td><input type="text" name="usr"></td></tr>
            <tr><td>sex:</td><td><select name="sex"><option value="1">Male</option><option value="0">Female</option></select></td></tr>
            <tr><td>Position: </td><td><select name="subpos" id="subpos"><?php echo $subpos; ?></select><td></tr>
            <tr><td>Position No: </td><td id="position_number"><select name="pos_number"><option value="">choose a Position first</option></select></td></tr>
            <tr><td>Contractor: </td><td><input type="checkbox" name="contractor" value="1"></td></tr>
            <tr><td>Start date: </td><td><input type="date" name="empStrtDte"></td></tr>
            <tr><td>Active: </td><td><input type="checkbox" name="active" value="1"></td></tr>
            <tr><td><?php echo '<input type="submit"><input type="reset" value="Reset"></td></tr>'; ?>
        </form>
    </table>
</div>

HTML code after I change a value in subpos (from Chrome):

<select name="subpos" id="subpos">
    <option value="AO2">AO2</option>
    <option value="AO3">AO3</option>
    <option value="AO4">AO4</option>
    ...
</select></td>
<select name="pos_number" id="pos_number">
     <option value="2">805620121</option>
     <option value="10">805678998</option>
</select>

All looks good, does exactly what I want at this point. Then I print the array and boom, missing the pos_number value and I have no idea why.

Array
(
    [firstname] =&gt; test
    [middlename] =&gt; test
    [lastname] =&gt; test
    [usr] =&gt; 
    [sex] =&gt; 1
    [subpos] =&gt; AO3
    [empStrtDte] =&gt; 2012-02-19
)
share|improve this question
I see no need to switch on get_list.php. You can change to: $ teamQuery = "SELECT * FROM WHERE establishment_positions position_filled = '0 'AND active = '1' AND position_acrynom = '". $ _REQUEST [' Subpos ']. "'"; Additionally you should avoid sql injection. – alditis 1 hour ago
Does the HTML returned from get_list.php include the <select> element, or is it just the <option>s? – Barmar 1 hour ago
Why do this: <tr><td><?php echo '<input type="submit"><input type="reset" value="Reset"></td></tr>'; ?>, why not just <tr><td><input type="submit"><input type="reset" value="Reset"></td></tr>; Not that I think this is the answer, just curious. – FreudianSlip 1 hour ago
I would recommend you use ch2.php.net/pdo for executing queries to the database. – alditis 1 hour ago
If you just need position_number and position_id, avoids SELECT * FROM. – alditis 1 hour ago
feedback

1 Answer

before anything you must change your combobox For select menus, the change event occurs when an option is selected. For text fields or text areas, the change event occurs when the field loses focus.

share
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.