Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP Drop down list selection populates second dropdown list or text box - HELP!!

    Hi all,

    I have been struggling on a bit of code for a while now. I need to populate a second drop down list (Region) based upon the selection of the first (County).
    I have found a piece of code that works on its own and have adapted to suit my needs - see below. However, when I drop it into my main page the javascript is not working. It's because of the formObject but I just don't know enough to resolve this! Furthermore, I need the textboxes the user has already completed in the form to retain their value once the javascript kicks in as the completed form will submit to a database.

    This piece of code is working well . . . .
    Code:
    <?php
    
    $link = mysql_connect('myhost', 'myusername', 'mypassword') or die('Could not connect: ' . mysql_error());
    mysql_select_db('mydatabase') or die('Could not select database');
    
    if(isset($_GET["County"]) && is_numeric($_GET["County"]))
    {
    $County = $_GET["County"];
    }
    
    if(isset($_GET["Region"]) && is_numeric($_GET["Region"]))
    {
    $Region = $_GET["Region"];
    }
    
    ?>
    
    <script language="JavaScript">
    
    function autoSubmit()
    {
    var formObject = document.forms['theForm'];
    formObject.submit();
    }
    
    </script>
    
    <form name="theForm" method="get">
    
    <!-- County SELECTION BASED ON city VALUE -->
    
    <?php
    
    ?>
    
    <select name="County" onChange="autoSubmit();">
    <option value=''</option>
    
    <?php
    
    //POPULATE DROP DOWN MENU WITH COUNTRIES FROM A GIVEN city
    
    $sql = "SELECT * FROM county_regions";
    $counties = mysql_query($sql,$link);
    
    while($row = mysql_fetch_array($counties))
    {
    echo ("<option value=\"$row[CountyID]\" " . ($County == $row["CountyID"]? " selected" : "") . ">$row[County]</option>");
    }
    
    ?>
    
    </select>
    
    <?php
    
    ?>
    
    <br><br>
    
    <?php
    
    if($County!= null && is_numeric($County))
    {
    
    ?>
    
    <select name="Region" onChange="autoSubmit();">
    
    
    <?php
    
    //POPULATE DROP DOWN MENU WITH RegionS FROM A GIVEN city, County
    
    $sql = "SELECT * FROM county_regions WHERE CountyID = $County ";
    $Regions = mysql_query($sql,$link);
    
    while($row = mysql_fetch_array($Regions))
    {
    echo ("<option value=\"$row[CountyID]\" " . ($Region == $row["CountyID"]? " selected" : "") . ">$row[Region]</option>");
    }
    
    ?>
    
    </select>
    
    <?php
    
    }
    
    ?>
    What follows is my form where the javascript is not working - edited quite a bit to save on space!

    Code:
    <head>
     
    <script language="JavaScript">
    
    function autoSubmit()
    {
    var formObject = document.forms['subform'];
    formObject.submit(); 
    }
    
    </script>
    
    </head>
    
    <form enctype="multipart/form-data" method="post" action="add_attraction01.php" FORM NAME="FormName">     
    <input type="hidden" name="MAX_FILE_SIZE" value="32768" />    
    <label for="Business_name">Business Name</label>    
    <input type="text" size="60" STYLE="color: #FFFFFF; font-family: Arial, Helvetica, sans-serif; font-weight: bold; font-size: 12px; background-color: #72A4D2;"     
    <id="Business_name" name="Business_name" maxlength=60/><font size="1" face="arial" color="red">Required field</font><br />    
    <label for="StreetAddress">Address</label>    
    <input type="text" size="60" rows="2" id="StreetAddress" name="StreetAddress" maxlength=120/><font size="1" face="arial" color="red">Required field</font><br />    
    <label for="Town">Town</label>    <input type="text" size="25" id="Town" name="Town" maxlength=25/><font size="1" face="arial" color="red">Required field</font><br /> 
    
    <?php
    
    $link = mysql_connect('myhost', 'myusername', 'mypassword') or die('Could not connect: ' . mysql_error());
    mysql_select_db('mydatabase') or die('Could not select database');
    
    if(isset($_GET["County"]) && is_numeric($_GET["County"]))
    {
    $County = $_GET["County"];
    }
    
    if(isset($_GET["Region"]) && is_numeric($_GET["Region"]))
    {
    $Region = $_GET["Region"];
    }
    
    ?>
    
    <form name = "subform" method="get"> 
    
    <select name="County" onChange="autoSubmit();"> 
    <option value=''</option>
    
    <?php
    
    $sql = "SELECT * FROM county_regions";
    $counties = mysql_query($sql,$link);
    
    while($row = mysql_fetch_array($counties))
    {
    echo ("<option value=\"$row[CountyID]\" " . ($County == $row["CountyID"]? " selected" : "") . ">$row[County]</option>");
    }
    
    ?>
    
    </select>
    
    <?php
    
    ?>
    
    <br><br>
    
    <?php
    
    if($County!= null && is_numeric($County))
    {
    
    ?>
    
    <select name="Region" onChange="autoSubmit();">
    
    
    <?php
    
    $sql = "SELECT * FROM county_regions WHERE CountyID = $County ";
    $Regions = mysql_query($sql,$link);
    
    while($row = mysql_fetch_array($Regions))
    {
    echo ("<option value=\"$row[CountyID]\" " . ($Region == $row["CountyID"]? " selected" : "") . ">$row[Region]</option>");
    }
    
    ?>
    
    </select>
    
    <?php
    
    }
    
    ?>
    <input type="text" size="20"id="Tel_No" name="Tel_No" maxlength=20 onkeypress="return isNumberKey(event)"/><font size="1" face="arial" color="red">Required field</font><br />    
    <br/>    
    <input type="submit" value="Submit your attraction" name="submit" onclick="return BothFieldsIdenticalCaseSensitive();"/>   
    </form>
    </body>
    </html>
    It's probably obvious to you guys!!

    Thanks in advance for your help.
    Last edited by scotty22; 07-13-2011 at 09:05 AM.

  • #2
    New Coder
    Join Date
    Feb 2008
    Posts
    29
    Thanks
    4
    Thanked 3 Times in 3 Posts
    the hell is this in your form code??

    Code:
    <form enctype="multipart/form-data" method="post" action="add_attraction01.php" FORM NAME="FormName">

  • #3
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,

    may be easier if I post more of the code as I have confused things!
    The complete code posted previous is a combination of two seperate ones. So below is how the form was before I added the function to self poultae the field "Region" upon selection of a "County".

    Code:
    <body>
    <script type="text/javascript" language="JavaScript">
    <!--
    
    function BothFieldsIdenticalCaseSensitive() {
    var one = document.FormName.Email.value;
    var another = document.FormName.Emailconfirm.value;
    if(one == another) { return true; }
    alert("Your email addresses do not match.");
    return false;
    }
    
    function BothFieldsIdenticalCaseInsensitive() {
    var one = document.FormName.Email.value.toLowerCase();
    var another = document.FormName.Emailconfirm.value.toLowerCase();
    if(one == another) { return true; }
    alert("Your email addresses must be identical.");
    return false;
    }
    
    
    //-->
    
    <!--
          function isNumberKey(evt)
          {
             var charCode = (evt.which) ? evt.which : event.keyCode
             if (charCode > 31 && (charCode < 48 || charCode > 57))
                return false;
    
             return true;
          }
          //-->
    </script>
    
    <h2>Add your attraction here</h2>  
    <p>Please share your attraction:</p>  
    <form enctype="multipart/form-data" method="post" action="add_attraction01.php" FORM NAME="FormName">    
    <input type="hidden" name="MAX_FILE_SIZE" value="32768" />    <label for="Business_name">Business Name</label>    
    <input type="text" size="60" STYLE="color: #FFFFFF; font-family: Arial, Helvetica, sans-serif; font-weight: bold; font-size: 12px; background-color: #72A4D2;"     
    <id="Business_name" name="Business_name" maxlength=60/><font size="1" face="arial" color="red">Required field</font><br />    
    <label for="StreetAddress">Address</label>    
    <input type="text" size="60" rows="2" id="StreetAddress" name="StreetAddress" maxlength=120/><font size="1" face="arial" color="red">Required field</font><br />    
    <label for="Town">Town</label>    <input type="text" size="25" id="Town" name="Town" maxlength=25/><font size="1" face="arial" color="red">Required field</font><br /> 
    <?php
    $link = mysql_connect('myhostname', 'myusername', 'mypassword') or die('Could not connect: ' . mysql_error());
    mysql_select_db('mydatabase') or die('Could not select database');
    $query = "select `CountyID`,`County`, 'Regions' from `county_regions`";
    $result = mysql_query($query) or die('Query failed: ' . mysql_error());
    ?>
    <label for="County">County</label> 
    <select name="County">
    <option value=''></option>
    <?php
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
      echo '<option value="'.$row['County'].'"'.
    (isset($_SESSION['County']) && $_SESSION['County'] == $row['County'] ? 'selected="selected"' : '').'>'.        
    $row['County'].'</option>';}
    ?>
    </select>
    <font size="1" face="arial" color="red">Required field</font><br />   
    <br />    
    <br />    
    <label for="Postcode">Postcode</label>    
    <input type="text" size="7"id="Postcode" name="Postcode" maxlength=7/><font size="1" face="arial" color="red">Required field</font><br />    
    <label for="Tel_No">Contact Number</label>    
    <input type="text" size="20"id="Tel_No" name="Tel_No" maxlength=20 onkeypress="return isNumberKey(event)"/><font size="1" face="arial" color="red">Required field</font><br />    
    <br/>    
    <label for="Website_address">Website address</label>    
    <input type="text" size="60" id="Website_address" name="Website_address" maxlength=60/><br />    
    <label for="Email">Contact email</label>    <input type="text" size="45" id="Email" name="Email" maxlength=45/><br />    
    <label for="Emailconfirm">Re-enter email</label>    <input type="text" size="45" id="Emailconfirm" name="Emailconfirm" maxlength=45/><br />    
    
    <br />    
    <input type="submit" value="Submit your attraction" name="submit" onclick="return BothFieldsIdenticalCaseSensitive();"/>   
    </form>
    </body>
    </html>
    In order to achieve a dropdown box or textbox called "Region" which populates upon selection of "County", I have the following code - which works as it is.

    Code:
    <script language="JavaScript">
    
    function autoSubmit()
    {
    var formObject = document.forms['theForm'];
    formObject.submit();
    }
    
    </script>
    
    </head>
    <body>
    
    <?php
    
    $link = mysql_connect('myhost', 'myusername', 'mypassword') or die('Could not connect: ' . mysql_error());
    mysql_select_db('mydatabase') or die('Could not select database');
    
    if(isset($_GET["County"]) && is_numeric($_GET["County"]))
    {
    $County = $_GET["County"];
    }
    
    if(isset($_GET["Region"]) && is_numeric($_GET["Region"]))
    {
    $Region = $_GET["Region"];
    
    }
    
    ?>
    
    <form name="theForm" method="get">
    
    <select name="County" onChange="autoSubmit();">
    <option value=''</option>
    
    <?php
    
    
    $sql = "SELECT * FROM county_regions";
    $counties = mysql_query($sql,$link);
    
    while($row = mysql_fetch_array($counties))
    {
    echo ("<option value=\"$row[CountyID]\" " . ($County == $row["CountyID"]? " selected" : "") . ">$row[County]</option>");
     }
    
    ?>
    
    </select>
    
    <?php
    
    ?>
    
    <br><br>
    
    <?php
    
    if($County!= null && is_numeric($County))
    {
    
    ?>
    
    <select name="Region" onChange="autoSubmit();">
    
    
    <?php
    
    
    $sql = "SELECT * FROM county_regions WHERE CountyID = $County ";
    $Regions = mysql_query($sql,$link);
    
    while($row = mysql_fetch_array($Regions))
    {
    echo ("<option value=\"$row[CountyID]\" " . ($Region == $row["CountyID"]? " selected" : "") . ">$row[Region]</option>");
    
    }
    ?>
    
    </select>
    
    <?php
    
    }
    
    ?>
    So above I have two sets of codes which independently work fine. However, I need to combine them. I also need to retain anything that has already been entered into the form.

    My first attempt at combining which I posted previously is a bit of a butcher's job - so if anyone can suggest how I can combine these two pieces of code so that the page works it would be great!


  •  

    Posting Permissions

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