#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Location
    Waterbury, CT
    Posts
    1
    Rep Power
    0

    PHP/MySQL/JavaScript/HTML Forms Question


    I need help!!

    I am trying to figure out a good way to make a usable HTML form that gets its fields from a Database and "onChange" updates the remainder of selected fields.

    I have some fields in a MySQL database table: "account, hostname, apptype, appname, version". I want a pull down menu for each one of these fields. When one of these pulldown menu's gets modified (onChange) i want the rest of the pulldown menu's to be updated based on what was changed in the changed pulldown menu. i.e. If i select the hostname "TESTHOST" i only want to see apptype's, appname's and version's that fall under that host. The only way I can think of how to do something like that would be JavaScript but I don't know much about it.

    Anybody have any ideas?
    If you need me to be more verbose (i.e. samples of data/table layout, samples of scripts) let me know.
  2. #2
  3. Newbie :P
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Jan 2001
    Location
    In the PHP Engine :-)
    Posts
    2,880
    Rep Power
    21
    Mike,

    You can do it with a combination of both, here is an example of something I created when I had three feedback forms each for a different feedback type.

    <SCRIPT LANGUAGE="JavaScript">
    <!--

    /* Handles the auto submit element of form */

    function displaypage() {
    document.forms[0].submit();
    }

    -->
    </SCRIPT>


    <SELECT NAME="loadpage" onChange="displaypage()">
    <OPTION VALUE="request_for_quote">Request for Quote</OPTION>
    <OPTION VALUE="ask_a_question">Ask A Question</OPTION>
    <OPTION VALUE="<?echo"$loadpage";?>" SELECTED="SELECTED">--> <?printf("%s", $Format=ucwords($Format1=str_replace("_"," ",$loadpage)));?></OPTION></SELECT>

    PHP Code:
    if ($loadpage==request_for_quote)
    {
    $FormType ="forms/request_for_quote.php3";
    }elseif (
    $loadpage==general_enquiry)
    {
    $FormType ="forms/general_enquiry.php3";
    }elseif (
    $loadpage==ask_a_question)
    {
    $FormType="forms/ask_a_question.php3";
    }elseif (!
    $loadpage)
    {
    $loadpage="request_for_quote";
    $FormType="forms/request_for_quote.php3";

    PHP Code:
    <?include("$FormType");?>

    Ok put the first section of the above PHP at the top of your page, then the Javascript then create a form and stick the OPTION box in, then enter the final peice of PHP.

    How it works...

    Each page has it's own FORM so in effect you have two forms in one page. When the option box is used it will change the $FormType from the default to a new option and refresh the page with the new form included.

    It worked for me but it may not be what you need, either way some of it may be useful.
    ---------------------
    -- SilkySmooth --
    ---------------------
    Proxy
  4. #3
  5. No Profile Picture
    Divine Wind
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2001
    Location
    Mongo
    Posts
    24
    Rep Power
    0
    Have a look at the double and triple combo boxes here:

    http://www.wsabstract.com/script/cutindex16.shtml

    I think these will do what you want as long as you are careful about the naming you should be OK. I've used the double combo script wirting to it from mySQL. I'll include the whole example (which draws out information on people and the first DDM produces the letters and the second displays the names that match the letters) although the important bit is the bit which writes the data into the javascript

    PHP Code:
        <FORM NAME="person_select" ACTION="whatever.php"  METHOD="GET">
    <?php

       $alphabet 
    = array('A''B''C''D''E''F''G''H''I''J''K''L''M''N''O''P''Q''R''S''T''U''V''W''X''Y''Z');
       
    $mysql_username "****";
       
    $mysql_password "****";
       
    $db_name "****";

    ?>
          <P><B>Author:</B><BR>
          <SELECT NAME="firstletter" SIZE="1" ONCHANGE="redirect(this.options.selectedIndex)" CLASS="input" ONFOCUS="this.className='inputOn'" ONBLUR="this.className='inputOff'">
          <OPTION SELECTED="SELECTED">Select letter:</OPTION>
    <?php


       
    while (list ($key$value) = each ($alphabet))
       {

       
    $value trim ($value);

       echo 
    "      <OPTION VALUE=\"\">$value</OPTION>\n";

       }

    ?>
          </SELECT>
          <SELECT NAME="person" SIZE="1">
          <OPTION SELECTED="SELECTED" VALUE="">&lt;--Select first letter of surname</OPTION>
          </SELECT>

    <SCRIPT>
    <!--

    /*
    Double Combo Script Credit
    By Website Abstraction ([url]www.wsabstract.com[/url])
    Over 200+ free JavaScripts here!
    */

    var groups=document.person_select.firstletter.options.length
    var group=new Array(groups)
    for (i=0; i<groups; i++)
    group[i]=new Array()

    group[0][0]=new Option("<--Select first letter of surname","")

    <?php

       reset 
    ($alphabet);

       
    $db mysql_connect("localhost""$mysql_username""$mysql_password") or die ("Couldn't connect to server");
       
    mysql_select_db ("$db_name",$db) or die ("Couldn't select database");

       while (list (
    $key$value) = each ($alphabet)) {

         
    $value trim ($value);
         
    $ar1 $key "1";
         
    $lc_value strtolower($value);

         
    $sql "SELECT person_id, surname, initials FROM person WHERE surname LIKE '$value%' OR surname LIKE '$lc_value%' ORDER BY surname, initials, main_name";
         
    $result mysql_query ($sql) or die ("Couldn't execute query");

         if (
    mysql_num_rows ($result) <= 0) {
           echo 
    "group[$ar1][0]=new Option(\"No entries for this letter\",\"\")\n";
         }
         else {

           echo 
    "group[$ar1][0]=new Option(\"Now select author:\",\"\")\n";

           
    $ar2 1;

             while (
    $row mysql_fetch_array ($result)) {

    echo 
    "group[$ar1][$ar2]=new Option(\"$row[surname]$row[initials]\",\"$row[person_id]\")\n";

               
    $ar2++;

             }
         }
         echo 
    "\n";
       }

    ?>
    var temp=document.person_select.person

    function redirect(x){
    for (m=temp.options.length-1;m>0;m--)
    temp.options[m]=null
    for (i=0;i<group[x].length;i++){
    temp.options[i]=new Option(group[x][i].text,group[x][i].value)
    }
    temp.options[0].selected=true
    }

    //-->
    </SCRIPT>
    <INPUT TYPE="SUBMIT" NAME="submit_person" VALUE="Submit" 
        </FORM>
    If you want more than a triple combo box you should try adapting one of the existing scripts or go over to the JavaScript forum and ask them for help (or you are going about things the wrong way!!) and use the general principles outlined above to get your data into the DDMs.

    Emperor

IMN logo majestic logo threadwatch logo seochat tools logo