1

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">
<form>
    <label>Select a Client:</label>
    <select id="combobox" >
    <option value="">Select...</option>
    <?php

include "db_conn.php";

callDB();   

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>
        <?php
}} ?>
    </select>
    </form>
</div>
</div>
<br>
<div id="content">

</div>
</div>
<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 ?>"
  break;

}
}

</script>
3
  • what is the problem at the moment Commented Aug 29, 2012 at 12:26
  • The problem is that nothing is being passed into the php variables that are in the javascript code. Commented Aug 29, 2012 at 12:29
  • 1
    Your javascript will only be executed on load of the page. Not when you select an option. You should add an onchange event to the select component Commented Aug 29, 2012 at 12:29

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, I'm using a jquery library autocomplete which i have place into another document, so change() is being called.
1

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!

6 Comments

He's got bigger problems than that. $client is declared inside his while loop and won't be accessible when he tries to print out to JS. He also has no event handlers.
@InGodITrust For your information, a semicolon before closing a PHP block is not required. See php.net/manual/en/… : "The closing tag of a block of PHP code automatically implies a semicolon; you do not need to have a semicolon terminating the last line of a PHP block."
@RemcoOverdijk thanks for that, didnt know that. Will update answer accordingly
@WayneWhitty is it possible for me to create a global variable for $client?
@Janey It's being declared inside a while loop. With every iteration of the loop, the previous client will be overwritten by the current one, meaning all you'll end up with is the last client. You should store your clients in a PHP array. You'll also need to add the onchange event handler to your select box.
|
0

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'
break;"

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

2 Comments

I've been trying to accomplish something like this. Would I need to declare $javascript as a global var? because it's still not picking up in the javascript.
Yes, silly me. You wrapped the PHP inside a function, so declare $javascript as global or remove the function(... line and run the php directly.
0

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};
                    $.ajax({
                        type: 'POST',
                        url: url,
                        data: data,
                        success: function(data) 
                        { 
                            append the values in div here using div.innerhtml
                    });
            });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.