TTomu
TTomu

Reputation: 99

sql response with ajax filter not working

My first contact with ajax. I have modify this tutorial. However sql response is always not showing any record. I made var_dump of $result, but only got bool(false). Where I made mistake? Is any better way of making dynamic filters?

My function, simple table:

function showRoadMap() {
$conn.... //cut connection with database

$q = $_GET['q'];
$sql = "SELECT * FROM roadmap WHERE status={$q}";
$result = $conn->query($sql);

    if ($result->num_rows > 0) {
        echo '
        <table class="showGrid">
                <tr>
                    <td>Name</td>
                    <td>Status</td>
                </tr>';
        while($row = $result->fetch_assoc()) {

            echo '<tr><td>'.$row['name'].'</a></td>
                  <td>'.$row['status'].'</td></tr>';
        }
        echo '</table>';
    }
    else
        echo 'No record found';
    }
}

My php file:

<script>
function showFilter(str) {
if (str == "") {
    document.getElementById("txtHint").innerHTML = "";
    return;
} else { 
    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","roadmapShow.php?q="+str,true);
    xmlhttp.send();
}
}
</script>
<form>
<select name="status" onchange="showFilter(this.value)">
<option value="">Select a status:</option>
<option value="proposal">proposal</option>
<option value="approved">approved</option>
<option value="done">done</option>
<option value="refuse">refuse</option>
</select>
</form>
<div id="txtHint"></div>

and the last file: roadMapShow.php

<?php showRoadMap(); ?>

Upvotes: 0

Views: 83

Answers (2)

vacsora
vacsora

Reputation: 207

Get rid of third file or setup proper include and parameter passing. I fixed your code in two files. There is no change in "My php file", and the other two is combined below:

roadmapShow.php:

<?php 
    $q = $_GET['q'];

    function showRoadMap($q) {
        $conn.... //cut connection with database
        $sql = "SELECT * FROM roadmap WHERE status={$q}";
        $result = $conn->query($sql);

        if ($result->num_rows > 0) {
            echo '
            <table class="showGrid">
                    <tr>
                        <td>Name</td>
                        <td>Status</td>
                    </tr>';
            while($row = $result->fetch_assoc()) {

                echo '<tr><td>'.$row['name'].'</a></td>
                      <td>'.$row['status'].'</td></tr>';
            }
            echo '</table>';
        }
        else
            echo 'No record found';
    }

    showRoadMap($q); 
?>

Upvotes: 1

Ahmed Khan
Ahmed Khan

Reputation: 61

Try doing like this

function showRoadMap($query) {
$conn.... //cut connection with database

$q = $query;
$sql = "SELECT * FROM roadmap WHERE status={$q}";
$result = $conn->query($sql);

    if ($result->num_rows > 0) {
        echo '
        <table class="showGrid">
                <tr>
                    <td>Name</td>
                    <td>Status</td>
                </tr>';
        while($row = $result->fetch_assoc()) {

            echo '<tr><td>'.$row['name'].'</a></td>
                  <td>'.$row['status'].'</td></tr>';
        }
        echo '</table>';
    }
    else
        echo 'No record found';
    }

and in roadmapshow.php

<?php 
$q = $_GET['q'];
showRoadMap($q); ?>

Upvotes: 0

Related Questions