Jump to content

[SOLVED] Populate one textbox based upon another...


A JM

Recommended Posts

How do I populate and refresh a second listbox based upon the choice a user makes in the first listbox?

 

Currently this is what I'm using for my first listbox but I'm not sure how to complete the second listbox can someone help me out with this?

 

<?php
//Get list of Clients from database
$query_rstClients = "SELECT DISTINCT clnt_name FROM orders ORDER BY clnt_name DESC";
mysql_select_db($database_dbconn, $dbconn);
$rstClients = mysql_query($query_rstClients, $dbconn) or die(mysql_error());

$selectBox = "<select id='clientname' name = 'clientname' onchange='somefunction();'><option>Select Client</option>";
while($rec=mysql_fetch_array($rstClients))
{
$selectBox = $selectBox."<option value= $rec[clnt_name]>$rec[clnt_name]</option>";
}
$selectBox = $selectBox."</select>";
?>

Link to comment
Share on other sites

if (!empty($_POST)) {//something was posted
    if (!empty($_POST['first_select']) && empty($_POST['second_select'])) {//nothing selected in
        $query = 'SELECT * FROM table WHERE parent_id = ' . $_POST['first_select'];//don't use it like this just an example
        $result = mysql_query($query, $db);
        ..
     } else if (!empty($_POST['first_select']) && !empty($_POST['second_select'])) {
         $first_select = $_POST['first_select'];
         $second_select = $_POST['second_select'];
     }
}

   

       

Link to comment
Share on other sites

This would seem to me more of a JS/Ajax type issue unless you're planning on having your page refresh when something is picked from the first select box.  Please specify how you want this to behave.

 

Ultimately I'm planning on using the selected items from both select boxes in a query for the current page so I'm open to suggestions.

 

 

Ignace, does that mean that after the user selects the first item that the page needs to post? I'm still a bit fuzzy about how to correctly use $_POST to refresh the current page.

 

Not sure I'm reading the script correctly:

 

If  $_POST not empty then test for 'first_select' not empty and 'second_select' is empty run query to populate second select if true else if 'first_select' not empty and 'second_select' not empty set variables.

 

Could I then run my query for the page after both are filed? How do I refresh the page?

        ...

        $first_select = $_POST['first_select'];

        $second_select = $_POST['second_select'];

        $query = 'SELECT * FROM table WHERE parent_id = ' . $_POST['first_select'] AND parent_name = ' . $_POST['select_select'];//don't use it like this just an example

        $result = mysql_query($query, $db);

        ???refresh page???

 

 

 

 

 

Link to comment
Share on other sites

...

        $first_select = $_POST['first_select'];

        $second_select = $_POST['second_select'];

        $query = 'SELECT * FROM table WHERE parent_id = ' . $_POST['first_select'] AND parent_name = ' . $_POST['select_select'];//don't use it like this just an example

        $result = mysql_query($query, $db);

        ???refresh page???

 

 

No not refresh, fill.

 

$secondSelectData = array();
if (!empty($_POST)) {//something was posted
    // assuming first field is filled and second field is awaiting data
    if (!empty($_POST['first_select']) && empty($_POST['second_select'])) {//nothing selected in
        $query = 'SELECT id, name FROM table WHERE parent_id = ' . $_POST['first_select'];//don't use it like this just an example
        $result = mysql_query($query, $db);
        ..
        while (list($id, $name) = mysql_fetch_array($result, MYSQL_NUM)) {
            $secondSelectData[$id] = $name;
        }
    } else if (!empty($_POST['first_select']) && !empty($_POST['second_select'])) {
         $first_select = $_POST['first_select'];
         $second_select = $_POST['second_select'];
    }
}

Link to comment
Share on other sites

I'm sorry I don't understand what you mean?

 

No not refresh, fill.

 

You don't need to refresh the page, just fill the second select with the value from the first select once the first select contains a value.

 

$firstSelectData = array();
$query = 'SELECT id, name FROM categories';
$result = mysql_query($query, $db/*from mysql_connect()*/);
while (list($id, $name) = mysql_fetch_array($result, MYSQL_NUM)) {
    $firstSelectData[$id] = $name;
}

$secondSelectData = array();
if (!empty($_POST)) {//user selected something from the first select
    if (!empty($_POST['first_select']) && /*make sure something was selected*/
         empty($_POST['second_select'])/*second is empty, fill it!*/) {
        $query = 'SELECT id, name FROM subcategories WHERE category_id = ' . $_POST['first_select'];
        $result = mysql_query($query, $db);
        while (list($id, $name) = mysql_query($result, MYSQL_NUM)) {
            $secondSelectData[$id] = $name;
        }
    } else if (!empty($_POST['first_select']) && /*something was selected from the first select*/
                 !empty($_POST['second_select'])/*and the second select now also contains data*/) {
        $first_select_id = $_POST['first_select'];
        $second_select_id = $_POST['second_select'];
    }
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.