SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Oct 2005
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    using onchange to relaunch form

    So I'm trying to populate a drop menu of boat launch options by using an onchange event in my form to reload the page after I select a state and then a lake, which is built after state selection. The state select works perfectly by populating the lakes in the second select, but when selecting the lake, it never reloads the page. Any help is much appreciated.

    Here's my Javascript
    Code:
    <script language=JavaScript>
    function narrowState(form) {
    var val=form.state.options[form.state.options.selectedIndex].value; 
    self.location='add_events.php?st=' + val;
    }
    </script>
    <script language=JavaScript>
    function narrowWater(form) {
    <?php print("var state=" . $_REQUEST[st] . ";\n");?>
    var val=form.water.options[form.water.options.selectedIndex].value; 
    self.location='add_events.php?st='+ state'&water='+ val;
    }
    </script>

    Here is the portion of the form
    PHP Code:
    <table align="left" width="100%">
        <tr> 
            <td align="right" width="25%">State:</td>
            <td align="left" width="75%">
            <?php
                $getState 
    = @mysql_query("SELECT state_abbv, state_name FROM states ORDER BY state_abbv");
            
    ?>
            <select name='state' onChange="narrowState(this.form)"><option value=''>- Select State -</option>
            <?php
                
    while($result mysql_fetch_array($getState)) {
                    if(
    $result['state_abbv']==@$_REQUEST['st']){
                        echo 
    "<option selected value='$result[state_abbv]'>$result[state_name]</option>"."<br />";
                    }    else {
                        echo 
    "<option value='$result[state_abbv]'>$result[state_name]</option>";
                    }
                }
            
    ?>
            </select>
            <span class="tip"> Select the State to narrow Lake options</span>
            </td>
        </tr>
        <tr> 
            <td align="right" width="25%">Lake:</td>
            <td align="left" width="75%">
            <?php
                $getWater 
    = @mysql_query("SELECT water_id, water_name FROM water WHERE water_state='$_REQUEST[st]' ORDER BY water_name");
            
    ?>
            <select name='water' onChange="narrowWater(this.form)"><option value=''>- Select Water -</option>
            <?php
                
    while($result mysql_fetch_array($getWater)) {
                    if(
    $result['water_id']==@$_REQUEST['water']){
                        echo 
    "<option selected value='$result[water_id]'>$result[water_name]</option>"."<br />";
                    }    else {
                        echo 
    "<option value='$result[water_id]'>$result[water_name]</option>";
                    }
                }
            
    ?>
            </select>
            <!-- <span class="tip"> Select the Water to narrow Launch options</span> -->
            </td>
        </tr>

  2. #2
    Under Construction silver trophybronze trophy AussieJohn's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    776
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    There is a tiny problem with your script at the moment. The self.location will fail because of a missing "+" character.

    self.location='add_events.php?st='+ state + '&water='+ val;
    var details = {
    . . web: "afterlight.com.au",
    . . photos: "jvdl.id.au",
    . . psa: "usethelatestversion.com"
    }

  3. #3
    SitePoint Member
    Join Date
    Oct 2005
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks AussieJohn. Your change embarrased me, then led me to the end problem, undefined variable. Had to change my php inside the script. I don't think I've ever used php inside javascript before, so I'm always learning.

    Had to change
    PHP Code:
    <?php print("var state=" $_REQUEST[st] . ";\n");?>
    to
    PHP Code:
    var state='<?php print($_REQUEST[st]);?>';
    in the narrowWater function

    Thanks again. Can always count on this forum for help.
    SitePoint rules!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •