Jump to content

Archived

This topic is now archived and is closed to further replies.

kris1988edwards

PHP / MSSQL Query from HTML drop down

Recommended Posts

Hi all,

 

I am trying to do a query on a database that takes a variable from a html drop down box. I've tried so many different forums and can't find the answers. 

 

HTML PAGE:

 

    <html>
    <head>
    <script>
    function showUser(str) {
    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","getuser.php?q="+str,true);
    xmlhttp.send();
    }
    </script>
    </head>
    <body>
    
    <form>
    <select name="users" onchange="showUser(this.value)">
    <option value="">Select ID:</option>
    <option value="1">Boston</option>
    <option value="2">NYC</option>
    <option value="3">UK</option>
    <option value="4">Paris</option>
    </select>
    </form>
    <br>
    
    <div id="txtHint"><b>Info will be listed here</b></div>
    
    </body>
    </html>
 
 
PHP CODE:
 
    <?php
    $q = intval($_GET['q']);
    
    //connection details 
    // db details
    
    $query ="SELECT * FROM tblExample WHERE id = '".$q."'";
    $result = sqlsrv_query( $conn, $query);
    
    echo "<table>
    <tr>
    <th> Name</th>
    <th> Address</th>
    <th> Department</th>
    <th> Salary</th>
    <th> Bonus</th>
    </tr>";
    
    while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_ARRAY) ) {
    echo "<tr><td>".$row[Name]."</td><td>".$row[Address]."</td><td>".$row[Department]."</td><td>".$row[salary]."</td><td>".$row[bonus]."</td></tr>";
    }
    
    echo "</table>"
    
    sqlsrv_close( $conn);
 
This doesn't seem to work, is there any way some can help with sorting this. I want to basically select a load of data that matches the query. There doesn't seem to be loads of help for this. 

 

Share this post


Link to post
Share on other sites

You need to learn simple debugging techniques. Add some debugging code to figure out what is going on. Pick one point in the process to see if what is happening is what you expect and that values are what you expect. If yes, the problem if further down in the process. If no, then the problem exists at that point or before.

 

The first step is to ensure the function showUser() is getting called AND that you are passing the right value. So, add an alert as the first line of code in the function such as

alert(str)
;

 

If the alert does not display then you know the function isn't running. If it does alert, but not the right value, then you know the value isn't getting passed as you think it is. If all performs as expected, then try the next significant step in the process.

Share this post


Link to post
Share on other sites

I start off by always checking my $_GET/$_POST/etc array, and making sure the data I expect is there.

 

Next, check the query, and make sure it works directly with your database.

 

I either echo to the browser, or use syslog().

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.