Visualizzazione dei risultati da 1 a 2 su 2
  1. #1
    Utente di HTML.it
    Registrato dal
    Sep 2009
    Messaggi
    17

    Selezione Dinamica da Database con AJAX e PHP con due select

    Con i seguenti due file si realizza uno script AJAX e PHP che interagisce con un database MySQL.

    Con una select all'interno di un form, si seleziona un dipendente e contestualmente viene interrogato un database col quale si ottengono informazioni sul dipendente selezionato.

    Ho bisogno di modificare lo script inserendo una seconda select nel form in modo da inviare due variabili al secondo file per fare una ricerca più complessa.

    potete aiutarmi?


    -------- File 1
    <script type="text/javascript">
    var xmlhttp;

    function mostraInfo(str)
    {
    xmlhttp=GetXmlHttpObject();
    if (xmlhttp==null)
    {
    alert ("Browser does not support HTTP Request");
    return;
    }
    var url="mostra_utenti.php";
    url=url+"?q="+str;
    xmlhttp.onreadystatechange=stateChanged;
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
    }

    function stateChanged()
    {
    if (xmlhttp.readyState==4)
    {
    document.getElementById("info").innerHTML=xmlhttp. responseText;
    }
    }

    function GetXmlHttpObject()
    {
    if (window.XMLHttpRequest)
    {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    return new XMLHttpRequest();
    }
    if (window.ActiveXObject)
    {
    // code for IE6, IE5
    return new ActiveXObject("Microsoft.XMLHTTP");
    }
    return null;
    }
    </script>

    <form>
    Seleziona Dipendente:
    <select name="users" onChange="mostraInfo(this.value)">
    <option selected="selected">seleziona un dipendete...</option>
    <option value='1'>Giuseppe Bianco</option>
    <option value='3'>Luigi Gallo</option>
    <option value='2'>Michele Rossi</option>
    <option value='4'>Roberto Viola</option>
    </select>
    </form>


    <div id="info"></div>


    -------- File 2
    <?php
    $q=$_GET["q"];

    $con = mysql_connect('xxxx', 'xxxx', 'xxxx');
    mysql_select_db("xxxx", $con);

    $sql="SELECT * FROM dipendenti WHERE id = '".$q."'";

    $result = mysql_query($sql);

    echo "<table border='1'>
    <tr>
    <th>Nome</th>
    <th>Cognome</th>
    <th>Anni</th>
    <th>Lavoro</th>
    <th>Stipendio</th>
    </tr>";

    while($row = mysql_fetch_array($result))
    {
    echo "<tr>";
    echo "<td>" . $row['nome'] . "</td>";
    echo "<td>" . $row['cognome'] . "</td>";
    echo "<td>" . $row['anni'] . "</td>";
    echo "<td>" . $row['lavoro'] . "</td>";
    echo "<td>" . $row['stipendio'] . "</td>";
    echo "</tr>";
    }
    echo "</table>";

    mysql_close($con);
    ?>

  2. #2
    Moderatore di Annunci siti web, Offro lavoro/collaborazione, Cerco lavoro L'avatar di cavicchiandrea
    Registrato dal
    Aug 2001
    Messaggi
    26,134
    Leggi qui non è la stessa cosa ma la logica non cambia
    Cavicchi Andrea
    Problemi con javascript, jquery, ajax clicca qui

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.