SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Aug 2012
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    i want to retrieve record based on two dropdown using by ajax

    below code is used ajax to retrieve data based on one drop down(works fine)
    but i need to use two drop down how to pass two dropdown value

    <html>
    <head>
    <script type="text/javascript">
    function showUser(str,str1)
    {
    if (str=="")
    {
    document.getElementById("txtHint").innerHTML="";
    return;
    }
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
    }
    xmlhttp.open("GET","user1.php?q="+str,true);
    xmlhttp.send();
    }
    </script>
    </head>
    <body>

    <form>
    <select name="users" onchange="showUser(this.value)">
    <option value="">Select a person/option>
    <option value="15">15</option>
    <option value="2">Lois Griffin</option>
    <option value="3">Glenn Quagmire</option>
    <option value="4">Joseph Swanson</option>
    </select>

    <select name="users1" onchange="showUser(this.value)">
    <option value="">Select a person/option>
    <option value="1">1</option>

    </select>

    </form>
    <br />
    <div id="txtHint"><b>Person info will be listed here.</b></div>

    </body>
    </html>


    below is user1.php code
    <?php
    $q=$_GET["q"];


    $con = mysql_connect('localhost', 'root', '');
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }

    mysql_select_db("tesjoomlaautogua", $con);

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

    $result = mysql_query($sql);

    echo "<table border='1'>
    <tr>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>Age</th>
    <th>Hometown</th>
    <th>Job</th>
    </tr>";

    while($row = mysql_fetch_array($result))
    {
    echo "<tr>";
    echo "<td>" . $row['title'] . "</td>";
    echo "<td>" . $row['cname'] . "</td>";
    echo "<td>" . $row['pname'] . "</td>";
    echo "<td>" . $row['title1'] . "</td>";
    echo "<td>" . $row['desc'] . "</td>";
    echo "</tr>";
    }
    echo "</table>";

    mysql_close($con);
    ?>

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    Right. Lets bug hunt.

    Code:
    function showUser(str,str1)
    {
    if (str=="")
    {
    document.getElementById("txtHint").innerHTML="";
    return;
    }
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
    }
    xmlhttp.open("GET","user1.php?q="+str,true);
    xmlhttp.send();
    }
    </script>
    </head>
    <body>
    
    <form>
    <select name="users" onchange="showUser(this.value)">
    <option value="">Select a person/option>
    <option value="15">15</option>
    <option value="2">Lois Griffin</option>
    <option value="3">Glenn Quagmire</option>
    <option value="4">Joseph Swanson</option>
    </select>
    
    <select name="users1" onchange="showUser(this.value)">
    <option value="">Select a person/option>
    <option value="1">1</option>
    Problem #1:
    Code:
    <select name="users1" onchange="showUser(this.value)">
    Code:
    function showUser(str,str1)
    You're submitting one value to a function that expects 2 parameters.

    I wouldnt actually submit any values as parameters, and instead use the getElementById (and then give the selects ID's) to pull the data on change.

    Problem #2:
    Code:
    xmlhttp.open("GET","user1.php?q="+str,true);
    Where's your second value in that url string?
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.


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
  •