Smiley
Smiley

Reputation: 79

Display data after selected item

First Id like to thank everyone for such positive response on my previous question.

Now I got another question I need help with.

I have a dropdown menu with a list of items. The list is generated inside while loop. Here is the code:

$query = "SELECT Key, Short FROM product WHERE Active = 1 OR Short LIKE 'Blue%'";
$run = mysql_query($query) or die(mysql_error());

echo 'Product: <br />';
?>

<select id="select2" name="select2">
<?php
$ids = 0;
echo "<option selected='selected'>-Select product-</option>";
while($rows = mysql_fetch_assoc($run)) {
    echo "<option value=$ids>".$rows['Short']."</option>";
    $ids++;
}
?>
</select><br /><br />

Now what I need to do is create another dropdown menu below this one and show contracts depending on which option they selected from dropdown menu. Each item they select also have a number called Key. Now inside another table called contracts I have stored all contracts with the same value Key. So...in the second dropdown menu I have to show the contracts based on the key they selected with the item in the first dropdown menu.

I really hope it is clear enough to understand, I am a little confused.

Update: Ok, here is new code:

index.php

$("select#select2").change(function(){
    $.ajax({
        type: "GET",
        url: "process.php",
        data: "selected_key=" + $(this).val(),
        success: function(result) {
            $("select#text2").html(result);
        }
    });
});

</script>

<select id="text2" name="text2">

</select>

And here is my process.php

<?php ## URL_TO_GET_CONTRACTS_FOR_KEY ##
$selectedKey = $_GET['selected_key'];
$query = "SELECT * FROM contacts WHERE Key = '".$selectedKey."'";
$run = mysql_query($query);
while($row = mysql_fetch_assoc($run)) {
    echo "<option value='..'>..</option>";
    } ?>

But I cant see anything displayed in my text2 dropdown menu.

Upvotes: 0

Views: 4820

Answers (3)

ennovativemedia
ennovativemedia

Reputation: 361

Think you best use option value $rows['Key'] in first dropdown menu and add an jQuery selector that takes the selected value and gets te corresponding contracts for that key.

Should be something like this..

PHP CODE

$query = "SELECT Key, Short FROM product WHERE Active = 1 OR Short LIKE 'Blue%'";
$run = mysql_query($query) or die(mysql_error());
echo 'Product: <br />'; ?>
<select id="select2" name="select2">
<?php
echo "<option selected='selected'>-Select product-</option>";
while($rows = mysql_fetch_assoc($run)) {
    echo "<option value='".$rows['Key']."'>".$rows['Short']."</option>";
}
?>
</select>

jQUERY CODE

$("select#select2").change(function(){
    $.ajax({
        type: "GET",
        url: "URL_TO_GET_CONTRACTS_FOR_KEY",
        data: "selected_key=" + $(this).val(),
        success: function(result) {
            $("select#NEWSELECT").html(result);
        }
    });
});

Explanation: URL_TO_GET_CONTRACTS_FOR_KEY is an url to a PHP file you have to write. In that file you have access to $_GET['selected_key'], use that value to get the contracts for that key. In that file you should echo the "" tags for the second select. Like this:

<?php ## URL_TO_GET_CONTRACTS_FOR_KEY ##
$selectedKey = $_GET['selected_key'];
$query = ..
$run = ..
while($row = mysql_fetch_assoc($run)) {
    echo "<option value='..'>..</option>";
} ?>

NEWSELECT should be replaced by the id of the select where the returned options should be placed in.

Upvotes: 2

cjds
cjds

Reputation: 8426

There are ways to do this but they will be cumbersome and annoying compared to AJAX. (what I mean by ways is get all required data first but this is a bad idea).

Why AJAX

AJAX can send and receive data from other pages while not reloading your whole page. The PHP you've written above will output only HTML to the browser. So to run more PHP you need to store additional code in a file and run that.

How to use it

A simple way would be with jQuery. This will go with your jQuery on client side

$("select#select2").change(function(){
$.ajax({
    type: "POST",
    url: "url_to_contacts",
    data: "reqd_key=" + $(this).val(),
    success: function(result) {
        $("select#select3").html(result);
      }
   });
});

There will be the seperate PHP file mentioned above. Variable can be picked up using $_POST This will contain the query and its will output HTML. This output HTML will be put as the new selects HTML by the line $("select#select3").html(result);

Suggestion do some research on AJAX before trying this and understand how to dynamically edit pages

Upvotes: 0

NullPoiиteя
NullPoiиteя

Reputation: 57322

php is server side you can not do this with only php for that you need ajax (if you want to so without refresh page)

just make ajax call on select drop down and show another


Note

  1. The entire ext/mysql PHP extension, which provides all functions named with the prefix mysql_, is officially deprecated as of PHP v5.5.0 and will be removed in the future. So use either PDO or MySQLi

Good read

  1. The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead

Upvotes: 1

Related Questions