Autopopulate from Select box from database

Posted by Chris Spalton on Stack Overflow See other posts from Stack Overflow or by Chris Spalton
Published on 2012-10-01T15:25:14Z Indexed on 2012/10/01 15:37 UTC
Read the original article Hit count: 140

Filed under:
|

hope you can help, please forgive any poor coding or anytihng, I'm new to this and just hacking my way through to get things to work.

That said, on one of my projects I have this code, which successfully populates the dropdown from a database when the page is loaded:

<select name="Region" id="Region">
    <option value="">-- Select Region --</option>
    <?php                     
       $region=$POST['Region'];    
       if ($region); {
            $regionquery = "SELECT DISTINCT REGION FROM Sales_Execs ";    
            $regionresult = mysql_query($regionquery);    
            while($row = mysql_fetch_array($regionresult)) {
                echo "<option value=\"".$row['REGION']."\">".$row['REGION']."</option>\n  ";
            }       
       }
     ?>
        <script type="text/javascript">
document.getElementById('Region').value = <?php echo json_encode(trim($_POST['Region']));?>;
        </script>
    </select>

On my next project that I'm working on now, I need to do the same thing, so I copied the above code amended, and placed in my new project:

<select name="Sales_Exec" id="Sales_Exec">    
<option value="">-- Select SE --</option>
<?php                     
   $salesexec=$POST['Sales_Exec'];    
   if ($salesexec);    
   {
       $salesexecquery = "SELECT DISTINCT Assigned FROM Data ";    
       $salesexecresult = mysql_query($salesexecquery);    
       while($row = mysql_fetch_array($salesexecresult))
       {
            echo "<option value=\"".$row['ASSIGNED']."\">".$row['ASSIGNED']."</option>\n  ";
        }       
    }
?>
<script type="text/javascript">
 document.getElementById('Sales_Exec').value = <?php echo json_encode(trim($_POST['Sales_Exec']));?>;
</script>

</select>

This second chunk of code doesn't work... and I can't work out why as it seems I've copied it all and amended all the neccersary parts, can anyone spot what is wrong?

Thankyou!

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql