Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
|
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 25th, 2009, 12:12 PM
Authorized User
 
Join Date: Nov 2007
Posts: 54
Thanks: 1
Thanked 0 Times in 0 Posts
Default populating one drop down with another

I want to populate one drop down with another.
The two drop downs are Projects, and Tasks. If a user select a project, the tasks should populate according to the project selected.
I can populate task list on the basis of project id, but I don't know how to refresh that list every time a different project is selected. Any help? direction?

my project drop down:
Code:
<select name="pid"><?
                                    while ($row=mysql_fetch_array($resultProject)) {
    
                                        $pid=$row["pid"];
                                        $value=$row["project_name"];
                                ?>
                                    
                                    <option value="<?=$pid?>" ><?=$value?></option> 
                                <?     } 
                                ?>
                                </select>
and my tasks drop down:
Code:
<select name="tid">
                                <?
                                    $sqlTask="SELECT * from tasks where pid=$pid ;";
                                    $resultTask=mysql_query($sqlTask);
                                    $optionsTask="";
                                                                    
                                    while ($row=mysql_fetch_array($resultTask)) {
    
                                        $pid=$row["tid"];
                                        $value=$row["task_name"];
                                    
                                ?>
                                    <option value="<?=$tid?>"><?=$value?></option> 
                                <?     } 
                                ?>
                                </select>
 
Old March 25th, 2009, 12:31 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Default

You will need to get which project they selected as a separate variable, then pass that to the task select. Something like:

Code:
<select name="tid">
<?
// see if a project was chosen
if(isset($_POST['pid']) && ($_POST['pid'] != ""))
{
    // get the project they chose
    $selectedProj = $_POST['pid'];
    // .. do some validation here to stop sql injection ...
    $sqlTask="SELECT * from tasks where pid=$selectedProj ;";
    $resultTask=mysql_query($sqlTask);
    $optionsTask="";

    while ($row=mysql_fetch_array($resultTask)) {
      $tid=$row["tid"];
      $value=$row["task_name"];
?>
  <option value="<?=$tid?>"><?=$value?></option> 
<?
    } // end while
} // end if
?>
</select>
HTH
Phil
 
Old March 25th, 2009, 01:25 PM
Authorized User
 
Join Date: Nov 2007
Posts: 54
Thanks: 1
Thanked 0 Times in 0 Posts
Default

thanks Phil.
I understand what you explained to me, but I am still struggling getting the "pid" selected by the user. $_POST['pid'] is not returning the right one, actually returning null always(I tried thorugh var_dump).
Consequently when I use the if clause for checking post variables it is not displaying task list at all. If I changed the if condition to set it null then I can see the task list populated based on the last entry on the project list(project list selects the first value but the while loop contains the last name of project when it exits.
If I could get the selected value, then I can run the query for tasks based on that ID and accordingly populate the task list.
any help ? could it be done using php at all ? or do i need to rely on javascript or else ?
Code:
<tr>
                            <td>Project:</td>
                            <td>
                                <select name="pid"> 
                                <?
                                    while ($row=mysql_fetch_array($resultProject)) {
    
                                        $pid=$row["pid"];
                                        $value=$row["project_name"];
                                ?>
                                >    
                                    <option value="<?=$pid?>" ><?=$value?></option>
                                    
                                <?     
                                    }
                                ?>
                                </select>
                            </td>
                        </tr>
                        <?
                        // see if a project was chosen
                        var_dump($_POST['pid']) ;//returning null
                        
                        if(isset($_POST['pid']) && ($_POST['pid'] != "")){ 
                        ?>

                        <tr>
                            <td>Task:</td>
                            <td>
                                <select name="tid">
                                <?
                                    $sqlTask="SELECT * from tasks where pid=$pid ;";
                                    $resultTask=mysql_query($sqlTask);
                                    $optionsTask="";
                                    $row=mysql_fetch_array($resultTask) ;
                                    var_dump($row) ;                                
                                    while ($row=mysql_fetch_array($resultTask)) {
    
                                        $pid=$row["tid"];
                                        $value=$row["task_name"];
                                    
                                ?>
                                    <option value="<?=$tid?>"><?=$value?></option> 
                                <?     } 
                                ?>
                                </select>
                            </td>
                        </tr>
                        <? }?>
 
Old March 25th, 2009, 01:48 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Default

The first time you load the page, then yes this will be null. Once they choose a project from the list and click go or whatever, the selected value should be posted to the server.

1) Are you using method="post" in the containing form? If you are using get, change the $_POSTs to $_GET.
2) Can you post any code which shows how the form is posted back once they have chosen a project.

If you are wanting to do this via Javascript+AJAX instead of normal form posts, you may be better off sending the chosen pid to a separate page, and just sending back the html for the task select by itself.
 
Old March 25th, 2009, 02:17 PM
Authorized User
 
Join Date: Nov 2007
Posts: 54
Thanks: 1
Thanked 0 Times in 0 Posts
Default

sure thing Phil.
1)I am using "post" method
2)the page is to present a user with entering data, once done the control is given to another page where I am basically inserting values in the database. The insert page is getting correct values for project but for task it is getting null(for obvious reason). I have taken out the if statement, b'cause I have to find a way to store the selected value of a drop down list for projects in a php variable somehow. Is there anyway of storing the selected value of drop down box ? I mean something like that:

Code:
<? $storeSelected = ?><html selected value><? ;?>
the page is pretty much what I posted earlier, posting again.
Code:
<form name="addTime" method="post"  action="insertTime.php">
                    <h4>Add Time:</h4><br />
                    <table>
                        
                                       
                        <tr>
                            <td>Project:</td>
                            <td>
                                <select name="pid">
                                
                                <?
                                    while ($row=mysql_fetch_array($resultProject)) {
    
                                        $pid=$row["pid"];
                                        $value=$row["project_name"];
                                ?>
                                >    
                                    <option value="<?=$pid?>" ><?=$value?></option>
                                    
                                <?     
                                    }
                                ?>
                                </select>
                            </td>
                        </tr>
                    
                        <tr>
                            <td>Task:</td>
                            <td>
                                <select name="tid">
                                <?
                                    $sqlTask="SELECT * from tasks where pid=$pid ;";
                                    $resultTask=mysql_query($sqlTask);
                                    $optionsTask="";
                                                                
                                    while ($row=mysql_fetch_array($resultTask)) {
    
                                        $pid=$row["tid"];
                                        $value=$row["task_name"];
                                    
                                ?>
                                    <option value="<?=$tid?>"><?=$value?></option> 
                                <?     } 
                                ?>
                                </select>
                            </td>
                        </tr>
                        
                    
                        <tr>
                            <td>Comments:</td>
                            <td><textarea name="comment" rows="10" cols="20"></textarea></td>
                        </tr>
                    </table>
                    <input type="submit" value="Add" >
                    <input type="reset">        
                </form>
        </div>
    </body>
 
Old March 25th, 2009, 06:06 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Default

Aha! Seeing the comment box makes it clear. So it is a two-step process - they choose the project, then choose the task and enter a comment? This means you will need to get the form to submit when they choose the project, but not save anything to the database until they click Add.

This requires 3 main things:
1) when the project selection changes, run some javascript to submit the form, and set some field to say not to save to the db.
2) make sure the previously selected/entered values are maintained in the form between postbacks.
3) save details when they press Add

Here is one way you could do this:

Code:
<?php
$db = mysql_connect("localhost", "root", "password");
mysql_select_db("testing");

// set to 1 if we changed project
if(isset($_POST['projchanged']) && $_POST['projchanged'] != '1')
{
    // assume they clicked add so can save to db
    $projectid = $_POST['pid'];
    $taskid = $_POST['tid'];
    $comment = $_POST['comment'];
    // add to db
    mysql_query("insert into tasknotes VALUES ($projectid, $taskid, '$comment')");
}
?>
<html>
<head></head>

<body>

<form name="addTime" method="post" action="insertTime.php">
  <h4>Add Time:</h4><br />
  <table>

    <tr>
      <td>Project:</td>
      <td>

        <select name="pid" onchange="document.getElementById('projchanged').value = '1'; document.forms['addTime'].submit();">
            <!-- default "no project selected" option -->
            <option value=""></option>
            <?php
              $resultProject = mysql_query("SELECT pid, project_name FROM Projects;");
              while ($row=mysql_fetch_array($resultProject)) {
                $pid = $row["pid"];
                $value = $row["project_name"];
                // keep same entry selected
                $isSelected = ($_POST['pid'] == $pid);
            ?>
              <option value="<?php print $pid; ?>" <?php if($isSelected) print "selected"; ?>><?php print $value; ?></option>
            <?php } ?>
        </select>

      </td>
    </tr>


<?php if(isset($_POST['pid']) && $_POST['pid'] != "") {
  // check they have chosen a project - initially hidden
?>
    <tr>
      <td>Task:</td>
      <td>

        <select name="tid">
        <?php
          // get the project id they selected
          $projectID = $_POST['pid'];
          $sqlTask = "SELECT * from tasks where pid = $projectID;";
          $resultTask=mysql_query($sqlTask);
          while ($row=mysql_fetch_array($resultTask)) {
            $tid=$row["tid"];
            $value=$row["task_name"];
            // keep same entry selected if necessary
            $isSelected = ($_POST['tid'] == $tid);
          ?>
            <option value="<?php print $tid; ?>" <?php if($isSelected) print "selected"; ?>><?php print $value; ?></option>
          <?php } ?>
        </select>

      </td>
    </tr>
<?php } ?>

    <tr>
      <td>Comments:</td>
      <td><textarea name="comment" rows="10" cols="20"><?php print $_POST['comment']; ?></textarea></td>
    </tr>
    </table>
    <input type="submit" value="Add" />
    <input type="reset" value="Reset" />

<!-- use this so we know if they changed project or not -->
    <input type="hidden" name="projchanged" id="projchanged" value="0" />
</form>

</body>
</html>
If you are passing to a different page, this could be a bit trickier - maybe use javascript to change the action of the form when click add.

Hope this makes sense
Phil





Similar Threads
Thread Thread Starter Forum Replies Last Post
populating drop down through text field nasirmunir Javascript How-To 5 June 30th, 2008 11:01 AM
populating a drop down with a file list cjcd ASP.NET 1.0 and 1.1 Professional 2 April 18th, 2004 10:37 AM
populating a drop down with a file list cjcd BOOK: Beginning ASP.NET 1.0 1 April 17th, 2004 02:53 AM
drop down is not populating spm74 ASP.NET 1.0 and 1.1 Basics 4 March 16th, 2004 12:36 PM
Populating Drop Down based on other drop down jeffbarclay Java Databases 1 November 7th, 2003 12:14 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.