douzhengyi5022 2013-03-17 17:06
浏览 28
已采纳

PHP AJAX之后返回空数据

I don't get any data alert after using the following script

$(document).ready(function() {
    $('.autosuggest').keyup(function() {
        var search_term = $(this) .attr('value');
        $.post('search.php', {search_term:search_term}, function(data) {
            alert(data);
        });

    });
});

And using the following PHP code

<?php
include('system/includes/db_connect.php');
if (isset($_POST['search_term']) == true && empty($_POST['search_term']) == false) {

    $search_term = mysql_real_escape_string($_POST['search_term']);
    $query = mysql_query("SELECT username FROM promoties WHERE username LIKE '$search_term%'");
    while (($row = mysql_fetch_assoc($query)) !== false) {
        echo '<li>', $row['username'], '</li>';
    }
}
?>

If I put 'test' in the alert, it just works fine, but if I alert the data, it gives an empty alert box.

I also tested the PHP code on it's own, and it works perfectly..

What's wrong? Thanks!

  • 写回答

2条回答 默认 最新

  • dqzd92796 2013-03-17 17:13
    关注

    if your .autosugest is an input(which i think it is)... you need to get its value by

    replace this

    var search_term = $(this) .attr('value');    
    

    by

    var search_term = $(this) .val();   
    

    attr() is not needed here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类