Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic Drop downs - SQL backend

Status
Not open for further replies.

Spidy6123

Technical User
May 30, 2002
227
CA
Hi,

I'm trying to make a Province and City form with select lists.

For example, based on the Province selection.. will change the available values in City list.

I have the lists boxes pulling from SQL backend .. I just dont know how to apply the criteria.. I understand I have to use an onchange event to call the javascript that will repopulate and apply the criteria to the second box..

I was hoping that you guys could help me with the java part?

Here is my code:
Produce List:
Code:
$eclassf_Provincelist = "<select class='tbox' name='eclassf_province' onchange='javascript:location.reload()'>";
    $eclassf_arg = "select Distinct Country, Province from `rbidder_cities` where Country = 'Canada'";
    if ($sql->db_Select_gen($eclassf_arg, false))
    {
        $eclassf_current = "";
        while ($eclassf_row = $sql->db_Fetch())
        {
            if ($eclassf_current != $eclassf_row['Province'])
            {
                $eclassf_current = $eclassf_row['Province'];
                //$eclassf_Provincelist .= "<option value='0' disabled='disabled'>" . $eclassf_row['Province'] . "</option>";
            }
            $eclassf_Provincelist .= "<option value='" . $eclassf_row['Province'] . "'";
            if ($eclassf_row['Province'] == $eclassf_province)
            {
                $eclassf_Provincelist .= " selected='selected'";
            }

            $eclassf_Provincelist .= ">" . $eclassf_row['Province'] . "</option>";
            # print "<br>".$eclassf_current. "- " . $eclassf_row['eclassf_subname']  ;
        } // while
        $eclassf_Provincelist .= "</select>";
    }

    else
    {
        $eclassf_Provincelist .= "<option value='0' >".ECLASSF_51."</select>";
    }
	
	$eclassf_Citylist = "<select class='tbox' name='eclassf_City'>";
    $eclassf_arg = "select Province, City from `rbidder_cities` where Province = '$eclassf_province'";
    if ($sql->db_Select_gen($eclassf_arg, false))
    {
        $eclassf_current = "";
        while ($eclassf_row = $sql->db_Fetch())
        {
            if ($eclassf_current != $eclassf_row['City'])
            {
                $eclassf_current = $eclassf_row['City'];
                //$eclassf_Citylist .= "<option value='0' disabled='disabled'>" . $eclassf_row['City'] . "</option>";
            }
            $eclassf_Citylist .= "<option value='" . $eclassf_row['City'] . "'";
            if ($eclassf_row['City'] == $eclassf_City)
            {
                $eclassf_Citylist .= " selected='selected'";
            }

            $eclassf_Citylist .= ">" . $eclassf_row['City'] . "</option>";
            # print "<br>".$eclassf_current. "- " . $eclassf_row['eclassf_subname']  ;
        } // while
        $eclassf_Citylist .= "</select>";
    }

    else
    {
        $eclassf_Citylist .= "<option value='0' >".ECLASSF_51."</select>";
    }

To View:
Code:
	//province
	$eclassf_text .= "<tr><td class=\"forumheader3\" style='vertical-align:top;' >" . ECLASSF_108 . ":</td><td class=\"forumheader3\" style='width:80%;text-align:left;vertical-align:top;'>
		$eclassf_Provincelist
		&nbsp;<i>" . ECLASSF_27 . "</i></td></tr>";
	
			
	//City
	$eclassf_text .= "<tr><td class=\"forumheader3\" style='vertical-align:top;' >" . ECLASSF_109 . ":</td><td class=\"forumheader3\" style='width:80%;text-align:left;vertical-align:top;'>
		$eclassf_Citylist
		&nbsp;<i>" . ECLASSF_27 . "</i></td></tr>";

The above produce the lists correctly.. but I don't really even know where to start with the onchange event

Any help is appreciated
 
I am a little confused...are you trying to connect to the mysql backend from within java? If so, you will run into trouble because java is client side, the php/mysql is server side.

I would think you will need to make sure that all of the variables are present within the webpage upon loading so that the javascript can later select and display the appropriate values. Php will have to generate the javascript based upon what is in the database.

Hope this helps!

- flub
 
You are asking for help with Javascript (different thing to Java) in a PHP forum?

Anyway:
The basic sequence is that you need to supply the selected option in your fist drop down, to your back end so it generates the contents of the second. If you want this to happen the moment you click on the drop down, then yes, you need Javascript (again totally different to Java) to do it as it will need to use the Onchange event to submit the form, and get the values from the DB.

If on the other hand you can have the user click a button to submit the form then no Javascript is required.





----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
i'm not at all sure that you should be applying fists to back-ends.

i posted a script the other day in this forum about chatrooms. that script had a very basic ajax (javascript) interaction and showed how to use onchange/onclick/onblur events to fire off requests to a php server which would then return data.

you could do worse than start with this script and see whether it helps you.

check out thread434-1405137

 
Well it has to happen on the fly.. state/province is changed.. then city list changes.. all before user submits form..

Thanks guys I have reposted in AJAX forum

sorry for the trouble
 
spidy,

check the thread suggested by jpadie - It works great and it has the answer to your question.

I should know, I was the original poster. Using that code, I was able to do exactly what I was looking for.

Reading through it, I learned enough to start writing my own AJAX scripts (not claiming to be an expert but I am in my way).

Good luck!

Jose
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top