PHP , Javascript and SQL code mix up

Hi I have this code that will pull the client name and address from the database. It echo's out the client name for each entry into a dropdown (<option value="<?php echo "$client" ?>"><?php echo "$client" ?></option>) which is done in a while loop. Then i have a Javascript that will change the innerHTML of a DIV named 'content' when you select a option in the dropdown - this is unique based on what is pulled from the database. - This is where I can't get it to work.. below is my code any help is much appreciated.

<div id="selectBox">
<div class="ui-widget">
    <label>Select a Client:</label>
    <select id="combobox" >
    <option value="">Select...</option>

include "db_conn.php";


function callDB(){      
$sql = 'SELECT * FROM clientlist';
   $query = mysql_query($sql) or die(mysql_error());
   while ($row = mysql_fetch_array($query))
       $ID= $row['id'];
       $client= $row["client"];
       $postal_add= $row["postal_add"];

<option value="<?php echo "$client" ?>"><?php echo "$client" ?></option>
}} ?>
<div id="content">

<script type="text/javascript">

// Script for changing Div
function change()
switch (document.getElementById("combobox").value)
  case "<?php echo $client ?>":
  document.getElementById("content").innerHTML = "<h2><?php echo $client ?></h2><b>Postal Address:</b> <?php echo $postal_add ?>"





By the time the Javascript is executed (Client), PHP already finished (Server). You will need to build the Javascript from PHP inside the While-Loop and send it to the browser later in the page:

// in while
$javascript .= "case $client:
document.getElementById('content').innerHTML = '<h2>$client</h2>
    <b>Postal Address:</b>$postal_add'

// later in page
switch (document.getElementById("combobox").value)
<?php echo $javascript; ?>

You created the function to populate the div element. However, at this time, that function is only defined, it is never being executed.

you need to setup an onchange handler for the select box to call your javascript function.

Also, the php that is being output into your javascript is outside of your while loop.


You are missing a semi-colon in the words <?php echo "$client" ?>. It should be <?php echo "$client"; ?>. EDIT: This is actually not needed, the ending ?> automatically closes the statement.

To add some more, as @Witty said you are not storing the $client variable outside of your loop so you cannot access it! Change that!


use ajax to update your div on th ebasis of the selected option from dropdown

 $('#dropdown').change(function() {
                    var id = $("#option").val();
                    var url = ajax action from where u will get the data for the selected option 
                    var data = {OptionID:id};
                        type: 'POST',
                        url: url,
                        data: data,
                        success: function(data) 
                            append the values in div here using div.innerhtml


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.