Link to home
Start Free TrialLog in
Avatar of EICT
EICTFlag for United Kingdom of Great Britain and Northern Ireland

asked on

How do I make my dropdowns depended within a JQuery Modal Dialog box.

Is it possible to have depended drop down lists within a JQuery Dialog box such that when I pick value in the first drop down lists it changes the available values in the second list.
In my example the first list is a new project, when this is changed the accommodation changes to reflect those associated with the project.

I was trying to use autosubmit() within my dialogue box to refresh the depended drop down list but this submits the dialogue and the window closes.

The function which creates the modal is:
$(function() {

    $( "#addTicket" ).click(function() {

      var id = $(this).attr("file-id");
        var dataString =  id;

            $.ajax({
                type: "POST",
                url: "create_ticket.php",
                data: dataString,
                cache: false,
                success: function(data) {
                    $("#add-window").dialog(
                        {
                        autoOpen: true,
                        maxWidth:650,
                        maxHeight: 600,
                        width: 650,
                        height: 600,
                        draggable: true,
                        modal: true,
                        buttons: {
                          "Save": function () 
                          { 
                          $.post("create_ticket_process.php", $("#addForm").serialize(), function(response)
                            {
                            if(!response)
                            {
                            location.reload(true);
                            }
                            $("p#errors").html(response);            
                            });
                          $(this).dialog("destroy");
                         },
                          "Cancel": function () 
                            {
                            $(this).dialog("destroy");
                            }
                        } 
                    });
                  $("#addFormBody").html(data);
                }
            });

Open in new window



The section of the form (create_ticket.php) which has the depended drop downs and is loaded into the modal dialog box is:

</tr>
	<tr>
	<td><select name="project" onChange="autoSubmit();">        
        <option value=null></option>';
        
        
        //POPULATE DROP DOWN MENU WITH PROJECTS
        
        $sql1 = "SELECT project_name FROM project WHERE end_date IS NULL ;" ;
        $projects = mysqli_query($dbci, $sql1);
        
        while($row3 = mysqli_fetch_array($projects))
        {        
            echo ("<option value=\"$row3[project_name]\" " . ($project == $row3["project_name"] ? " selected" : "") . ">$row3[project_name]</option>");        
        }
        
echo '
        
    </select></td>
	</tr>
	<tr>
	<td><select name="accommodation" onChange="autoSubmit();">        
        <option value=null></option>';
        
        
        //POPULATE DROP DOWN MENU WITH ACCOMMODATION
        
        $sql2 = "SELECT site_name  FROM accommodation WHERE project = $project;" ;
        $properties = mysqli_query($dbci, $sql4);
        
        while($row4 = mysqli_fetch_array($properties))
        {        
            echo ("<option value=\"$row4[site_name]\" " . ($hostel == $row4["site_name"] ? " selected" : "") . ">$row4[site_name]</option>");        
        
        }
        
echo '
        
    </select></td>
	</tr>

Open in new window



Any pointers or examples would be gratefully received.
Thanks
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of EICT

ASKER

Thanks. Don't know why I didn't find Kartik before but this looks like a quicker solution to implement and I'm still learning about Ajax and Json. Your steps are helpful to.
Avatar of Julian Hansen