PLEASE HELP - Drop down list related question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cpptutor2000@yahoo.com

    PLEASE HELP - Drop down list related question

    Could some PHP guru please help me? I am creating a dynamic dropdown
    list using a code snippet(Section A) as below:
    Section A:
    $sql_query=mysq l_query("SELECT DISTINCT semester, year from
    schoolproject_p ics ORDER BY year
    DESC");
    echo "<select name=\"semester \" onchange=\"GoTo ()\">";
    echo "<option value=\"$semest er\">-Semester Year-</option>";
    while($data = mysql_fetch_arr ay($sql_query)) {
    if($data[semester]==@$semester && $data[year]==@$year){
    echo "<option value
    selected=\"$dat a[semester]\">$data[semester]:$data[year]</option><BR>";
    }
    echo "<option
    value=\"$data[semester]\">$data[semester]:$data[year]</option>"; }
    echo "</select>";
    mysql_free_resu lt($sql_query);

    The 'GoTo()' Javascript re-draws the same page with the chosen values
    of 'semester' and 'year' so that
    the values can be recovered by $_HTTP_GET_VARS , stored in two hidden
    variables and then used in
    another query to create another dynamic drop-down list, as in code
    snippet (Section B).

    $semesternow=$H TTP_GET_VARS['semester'];
    $yearnow=$HTTP_ GET_VARS['year'];
    if((isset($seme sternow) and strlen($semeste rnow) 0) and
    (isset($yearnow ) and strlen($yearnow ) 0)){
    print("<input type=\"hidden\" ID=\"semesterch osen\"
    value=\"$semest ernow\">");
    print("<input type=\"hidden\" ID=\"yearchosen \"
    value=\"$yearno w\">");
    $sql_query2=mys ql_query("SELEC T DISTINCT school from
    schoolproject_p ics
    WHERE semester='$seme sternow' AND
    year='$yearnow' ");
    echo "<select name=\"school\" onchange=\"GoMo re()\">";
    echo "<option value=\"$school \">-- School --</option>";
    while(list($sch ool) = mysql_fetch_arr ay($sql_query2) ){
    echo "<option value=\"$school \">$school</option>";
    }
    echo "</select>";
    mysql_free_resu lt($sql_query2) ;

    Now the question:
    When the page is re-drawn, how can the values of semester and year
    previously chosen, be displayed in the first drop down list. I tried to
    do it using the following, but it does not do anything:

    f($data[semester]==@$semester && $data[year]==@$year){
    echo "<option value
    selected=\"$dat a[semester]\">$data[semester]:$data[year]</option><BR>";
    }

    Any help would be greatly appreciated. Thanks in advance for your help.

  • Colin Fine

    #2
    Re: PLEASE HELP - Drop down list related question

    cpptutor2000@ya hoo.com wrote:
    Could some PHP guru please help me? I am creating a dynamic dropdown
    list using a code snippet(Section A) as below:
    Section A:
    $sql_query=mysq l_query("SELECT DISTINCT semester, year from
    schoolproject_p ics ORDER BY year
    DESC");
    echo "<select name=\"semester \" onchange=\"GoTo ()\">";
    echo "<option value=\"$semest er\">-Semester Year-</option>";
    while($data = mysql_fetch_arr ay($sql_query)) {
    if($data[semester]==@$semester && $data[year]==@$year){
    echo "<option value
    selected=\"$dat a[semester]\">$data[semester]:$data[year]</option><BR>";
    }
    echo "<option
    value=\"$data[semester]\">$data[semester]:$data[year]</option>"; }
    echo "</select>";
    mysql_free_resu lt($sql_query);
    >
    The 'GoTo()' Javascript re-draws the same page with the chosen values
    of 'semester' and 'year' so that
    the values can be recovered by $_HTTP_GET_VARS , stored in two hidden
    variables and then used in
    another query to create another dynamic drop-down list, as in code
    snippet (Section B).
    >
    $semesternow=$H TTP_GET_VARS['semester'];
    $yearnow=$HTTP_ GET_VARS['year'];
    if((isset($seme sternow) and strlen($semeste rnow) 0) and
    (isset($yearnow ) and strlen($yearnow ) 0)){
    print("<input type=\"hidden\" ID=\"semesterch osen\"
    value=\"$semest ernow\">");
    print("<input type=\"hidden\" ID=\"yearchosen \"
    value=\"$yearno w\">");
    $sql_query2=mys ql_query("SELEC T DISTINCT school from
    schoolproject_p ics
    WHERE semester='$seme sternow' AND
    year='$yearnow' ");
    echo "<select name=\"school\" onchange=\"GoMo re()\">";
    echo "<option value=\"$school \">-- School --</option>";
    while(list($sch ool) = mysql_fetch_arr ay($sql_query2) ){
    echo "<option value=\"$school \">$school</option>";
    }
    echo "</select>";
    mysql_free_resu lt($sql_query2) ;
    >
    Now the question:
    When the page is re-drawn, how can the values of semester and year
    previously chosen, be displayed in the first drop down list. I tried to
    do it using the following, but it does not do anything:
    >
    f($data[semester]==@$semester && $data[year]==@$year){
    echo "<option value
    selected=\"$dat a[semester]\">$data[semester]:$data[year]</option><BR>";
    }
    >
    Any help would be greatly appreciated. Thanks in advance for your help.
    >
    I'm not completely clear what you're trying to do.

    Are you saying that GoTo() submits a new HTTP request with
    ?semester=...&y ear=... ?

    I'm also not clear what you mean to have in your <option>, but you seem
    to be saying
    <option value selected = ...
    where I think you mean
    <option selected value = ...

    Furthermore you are setting $semesternow from the CGI variable ($_GET is
    preferred over $HTTP_GET_VARS since 4.1.0, but that's presumably not
    relevant), but seem to be comparing the value retrieved from the DB with
    $semester, not $semesternow.

    Does this help?

    Colin

    Comment

    • Manuel Lemos

      #3
      Re: PLEASE HELP - Drop down list related question

      Hello,

      on 09/30/2006 11:05 PM cpptutor2000@ya hoo.com said the following:
      Could some PHP guru please help me? I am creating a dynamic dropdown
      list using a code snippet(Section A) as below:
      If I got you right, you want to display linked select inputs with
      options taken from database queries results, so when on you change one
      select it display another with options dependent on the selected value
      of the first, right?

      In that case, you may want to take a look at this forms generation class
      that comes with a plug-in to do that.

      Actually, it does it better as it can use AJAX to update the dependent
      select input options without reloading. It supports MySQL and many other
      databases.

      Here is an example page of what it does. This is not the AJAX/Database
      driven version. For that see other example scripts that comes with the
      class:

      Test HTML page generated by the class using test_linked_select_input.php script. test_linked_select_page.html Class that generates HTML forms supporting: - Multiple inputs may be interconnected in such way that client side events that occur on one input can trigger actions on the context of other inputs. Developers may use input interconnection support without writing Javascript code. - Can be...


      The class is here:




      --

      Regards,
      Manuel Lemos

      Metastorage - Data object relational mapping layer generator


      PHP Classes - Free ready to use OOP components written in PHP
      Free PHP Classes and Objects 2026 Versions with PHP Example Scripts, PHP Tutorials, Download PHP Scripts, PHP articles, Remote PHP Jobs, Hire PHP Developers, PHP Book Reviews, PHP Language OOP Materials

      Comment

      Working...