Viewed   135 times

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>

 Answers

4

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; ?>
}
Sunday, October 23, 2022
 
paullb
 
3

Top tip for developing your own calendar: don't. Calendars are such a time sink, and non-trivial to write. If you need to ask for tips you'd be better off with an existing library component like you'll find in jQuery, dojo, etc..

This question has been asked many times here, links abound on other questions.

  • Best JavaScript Calendar control

  • What is the best calendar pop-up for populating a web form?

  • Creating A Javascript Calendar (Full, not pop up)

etc, etc...

Friday, September 2, 2022
 
xsari3x
 
3

I would request a video feed and make use of the author parameter. This should give you all the videos of a specific user.

In your example: http://gdata.youtube.com/feeds/api/videos?author=stevesattlerfilms

Tuesday, December 6, 2022
 
1

Here is the algorithm:

You add the new file input fields to your form. Each of this field MUST have a unique name. Then, on the server side, you loop through the $_FILES array looking how many files have been uploaded and handling them.

Saturday, September 17, 2022
 
1

You definitely want to redesign the database. Putting multiple values into a single column like that is a huge violation of the rules of normalization and will cause lots of headaches down the road. A single column should always hold a single piece of information.

You should also be using proper data types. Don't put times in a string, because you could end up with "foo" as a time and then what do you do?

Instead, what you probably want is:

CREATE TABLE Restaurants
(
    restaurant_id    INT            NOT NULL,
    restaurant_name  VARCHAR(40)    NOT NULL,
    CONSTRAINT PK_Restaurants PRIMARY KEY CLUSTERED (restaurant_id)
)
CREATE TABLE Restaurant_Hours
(
    restaurant_id    INT         NOT NULL,
    hours_id         INT         NOT NULL,
    day_of_week      SMALLINT    NOT NULL,
    start_time       TIME        NOT NULL,  -- Depends on your RDBMS and which date/time datatypes it supports
    end_time         TIME        NOT NULL,
    CONSTRAINT PK_Restaurant_Hours PRIMARY KEY CLUSTERED (restaurant_id, hours_id)
)

You can then easily check for restaurants open at a given time:

SELECT
    R.restaurant_id,
    R.restaurant_name
FROM
    Restaurants R
WHERE
    EXISTS
    (
        SELECT *
        FROM
            Restaurant_Hours RH
        WHERE
            RH.restaurant_id = R.restaurant_id AND
            RH.start_time <= @time AND
            RH.end_time >= @time AND
            RH.day_of_week = @day_of_week
    )

If you have a time slot that spans midnight you would need to have two rows - one for the first day, and one for midnight - "x" for the next day. Also, remember to keep time zones in mind when using from a GUI.

Friday, September 16, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :