SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: Combo boxs sql

  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2008
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Combo boxs sql

    Hello before all, ive got this php code in a php page:

    <?php $sql = "SELECT name FROM properties";
    $result= mysql_query($sql);
    echo "<select name=sel onChange='valores( this.value );'>";
    printf("<option>--seleccione--</option>");
    while($res = mysql_fetch_array($result)){
    echo "<option value=\"".$res[name]."\">".$res[name]."</option>\n";
    }
    echo "</select>\n\n";
    ?>

    Query based on this table named 'properties':

    - id
    - name
    - city
    - user_id

    It makes a combo box working ok this combo shows properties name.
    Now i would need a second combo box with rooms of each property.
    For example property 1 has six rooms ->room1, room2...
    So when you select in first combo box property 1, second combo box will show then room 1 value, room 2 value...

    Second table named 'rooms' from where i want to take room's name:

    - id
    - name
    - user_id
    - property_id

    Trying to help, ive found this code not based on sql:

    form.html -> ive replace this code:
    Code:
    <form name="formulario">
    Combo 1 : <select name="combo1" onChange="valores( this.value );">
        <option value="---">Seleccione una opción</option>
        <option value="mamiferos">Mamiferos</option>
        <option value="aves">Aves</option>
        <option value="moluscos">Moluscos</option>
        </select><BR>
    Combo 2 : <select name="combo2"></select>
    </form>
    with first code gived.

    process.php
    Code:
            function get_valores ( $parameters )  {
                // los parametros siempre son un array ... en este caso de solo 1 elemento
    	    $room = "select name from rooms where user_id ='3';
    		echo $room;
    	    $rooma = mysql_fecth_array($room);
    	    echo $rooma;
                $mamiferos  = array ($rooma);
                $aves       = array ( 'gallina', 'gorrión', 'cuervo', 'canario' );    
                $moluscos   = array ( 'berberecho', 'coquina', 'almeja');
                
                $valor = $parameters[0];
                // comprobamos que existan el array con los valores:
                if ( isset( $$valor)) {
                    // devolvemos el array como una cadena delimitada por ~
                    return implode( '~',  $$valor );
                } else {
                    // sinó ... enviamos una cadena vacia ... 
                    return  'naita';
                }
            }
    misfunciones.js
    Code:
    function valores( cadena ) {
        /*
            Esta funcion solo ejecuta jsrsExecute con los siguientes parametros:
            1: Fichero [url] del .php que ofrece el servicio
            2: nombre de la funcion que recibirá el resultado ... recibe siempre un parámetro
            3: nombre de la funcion a ejecutar en el servidor
            4: parametros a enviar al servidor ... en este una cadena ... 
        
        */
        jsrsExecute("procesos.php", valores_resultado, "get_valores",  cadena  );
    }
    function valores_resultado( cadena ) {
        // esta funcion recibe una cadena ... que transformaremos en un array ... 
        // con la funcion jsrsArrayFromString ... es un split ... 
        miArray  = jsrsArrayFromString( cadena  , "~" ) ;
        objeto = document.formulario.combo2 ;
        objeto.options.length = 0 ;
        var defaultSelected = true;
        var selected = false;
        var length = 0;
        for( indice = 0; indice < miArray.length ; indice ++) {
           if ( indice == 0 ) {
              defaultSelected = true;
              selected = true;
           } else {
              defaultSelected = false;
              selected = false;
        
           }
           optionName = new Option(miArray[indice], miArray[indice], defaultSelected, selected) ;
           length = objeto.options.length;
           objeto.options[length] = optionName ;
        }
    }
    and jsrsClient.js - javascript remote scripting client include.
    As i said before code is not sql based, thats why im asking this.

    How could i do this?
    Thanks in advance.
    R3g@rd$

  2. #2
    SitePoint Enthusiast
    Join Date
    Jun 2008
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ive found this code and i have made some changes but...i have only one of this combo box query sql.

    Code:
    <?php
    include 'config.php';
    include 'opendb.php';
    ?>
    <html>
    <head>
    <title>Listas desplegables dependientes 2</title>
    </head>
    
    <p align="center"><b>Listas desplegables dependientes 2
    <script LANGUAGE="JavaScript">
    
    <!-- This script and many more are available free online at -->
    <!-- The JavaScript Source!! http://javascript.internet.com -->
    
    <!-- Begin
    
    var middleeastArray =  new Array("('Select name_room','',true,true)",
    "('Egypt')",
    "('Iran')",
    "('Israel')",
    "('Kuwait')",
    "('Lebanon')",
    "('Morocco')",
    "('Saudi Arabia')",
    "('Syria')",
    "('Turkey')",
    "('U. A. Emirates')",
    "('Other')");
    
    var namericaArray =  new Array("('Select name_room','',true,true)",
    "('Canada')",
    "('USA')",
    "('Other')");
    
    
    function populatename_room(inForm,selected) {
    var selectedArray = eval(selected + "Array");
    while (selectedArray.length < inForm.name_room.options.length) {
    inForm.name_room.options[(inForm.name_room.options.length - 1)] = null;
    }
    for (var i=0; i < selectedArray.length; i++) {
    eval("inForm.name_room.options[i]=" + "new Option" + selectedArray[i]);
    }
    if (inForm.formulario.options[0].value == '') {
    inForm.formulario.options[0]= null;
    if ( navigator.appName == 'Netscape') {
    if (parseInt(navigator.appVersion) < 4) {
    window.history.go(0);
    }
    else {   	
    if (navigator.platform == 'Win32' || navigator.platform == 'Win16') {
    window.history.go(0);
                }
             }
          }
       }
    }
    function populateUSstate(inForm,selected) {  
    var stateArray =  new Array("('Select State','',true,true)",
    "('Alabama')",
    "('Alaska')",
    "('Arizona')",
    "('Arkansas')",
    "('California')",
    "('Colorado')",
    "('Connecticut')",
    "('Delaware')",
    "('Columbia')",
    "('Florida')",
    "('Georgia')",
    "('Hawaii')",
    "('Idaho')",
    "('Illinois')",
    "('Indiana')",
    "('Iowa')",
    "('Kansas')",
    "('Kentucky')",
    "('Louisiana')",
    "('Maine')",
    "('Maryland')",
    "('Massachusetts')",
    "('Michigan')",
    "('Minnesota')",
    "('Mississippi')",
    "('Missouri')",
    "('Montana')",
    "('Nebraska')",
    "('Nevada')",
    "('New Hampshire')",
    "('New Jersey')",
    "('New Mexico')",
    "('New York')",
    "('North Carolina')",
    "('North Dakota')",
    "('Ohio')",
    "('Oklahoma')",
    "('Oregon')",
    "('Pennsylvania')",
    "('Rhode Island')",
    "('South Carolina')",
    "('South Dakota')",
    "('Tennessee')",
    "('Texas')",
    "('Utah')",
    "('Vermont')",
    "('Virginia')",
    "('Washington')",
    "('West Virginia')",
    "('Wisconsin')",
    "('Wyoming')");
    if (selected == 'USA') {
    for (var i=0; i < stateArray.length; i++) {
    eval("inForm.name_room.options[i]=" + "new Option" + stateArray[i]);
    }
    if ( navigator.appName == 'Netscape') {
    if (parseInt(navigator.appVersion) < 4) {
    window.history.go(0)
    }
    else {    	
    if (navigator.platform == 'Win32' || navigator.platform == 'Win16') {
    window.history.go(0)
             }
          }
       }
    }
    else {
    }  
    if (selected == 'Other') {
    newname_room = "";
    while (newname_room == ""){
    newname_room=prompt ("Please enter the name of your name_room.", "");
    }
    if (newname_room != null) {
    inForm.name_room.options[(inForm.name_room.options.length-1)]=new Option(newname_room,newname_room,true,true);
    inForm.name_room.options[inForm.name_room.options.length]=new Option('Other, not listed','Other');
       }
    }
    if(inForm.name_room.options[0].text == 'Select name_room') {
    inForm.name_room.options[0]= null;
       }
    }
    // End -->
    </script>
    </head>
    
    <!-- STEP TWO: Add the last coding to the BODY of your HTML document  -->
    </b>
    <body>
    
    </p>
    
    <center>
    <form name="globe">
      <p>
      <select name="formulario" onChange="populatename_room(document.globe,document.globe.formulario.options[document.globe.formulario.selectedIndex].value)">
    <?php $sql = "SELECT name FROM properties";
    $result= mysql_query($sql);
    echo "<select name='formulario' onChange='populatename_room(document.globe,document.globe.formulario.options[document.globe.formulario.selectedIndex].value)'>">
    printf("<option>--seleccione--</option>"); 
    while($res = mysql_fetch_array($result)){
    echo "<option value=\"".$res[name]."\">".$res[name]."</option>\n";
    }
    echo "</select>\n\n";
    ?>
    
      <select name="name_room" onChange="populateUSstate(document.globe,document.globe.name_room.options[document.globe.name_room.selectedIndex].text)">
      <option value>&lt;--------------------</option>
      </select> </p>
    </form>
    </center>
    <p></p>
    <center>
    <p>&nbsp;</p>
    </center>
    <p></p>
    <!-- Script Size:  6.08 KB  -->
    
    </body>
    
    </html>	
    <?php
    ?>
    Hope it helps.

    R3g@rd$

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    It seems that people in the php forums will be able to beter help you with this query
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •