sql响应与ajax过滤器无法正常工作

时间:2015-11-04 12:00:01

标签: php ajax

我第一次接触ajax。我修改了this教程。但是,sql响应始终不显示任何记录。 我做了$ result的var_dump,但只得到了bool(false)。 哪里弄错了?有没有更好的方法来制作动态过滤器?

我的功能,简单的表格:

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';
    }
}

我的php文件:

<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>

和最后一个文件:roadMapShow.php

<?php showRoadMap(); ?>

2 个答案:

答案 0 :(得分:1)

删除第三个文件或设置正确的包含和参数传递。我把你的代码修改为两个文件。 &#34;我的php文件&#34;没有变化,另外两个结合在下面:

roadmapShow.php:

Image newImage = Image.FromFile("angryBird.jpg");

// Create coordinates for upper-left corner of image.
int x = 100;
int y = 100;

// Create rectangle for source image.
Rectangle srcRect = new Rectangle(0, 0, 71, 71);
GraphicsUnit units = GraphicsUnit.Pixel;

// Draw image to screen.
e.Graphics.DrawImage(newImage, x, y, srcRect, units);

答案 1 :(得分:0)

尝试这样做

 <?php 
 // get the ID of the current post
 $current_id = get_the_ID(); 
 ?>
 <?php foreach( $myposts as $post ) :  setup_postdata($post);?>
     <a href="<?php the_permalink(); ?>" class="list-group-item<?php echo ($post->ID==$current_id?' active-item':''); ?>"><?php the_title();?> <span class="fa fa-angle-right"></span></a>
 <?php endforeach; ?>

和roadmapshow.php

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';
    }