运行AJAX脚本的PHP只能运行一次

时间:2017-10-09 19:08:44

标签: javascript php jquery ajax

我有这个奇怪的问题,发生在我的PHP脚本中,在页面加载AJAX脚本运行,并且在第二次运行AJAX脚本之后它工作并将数据发送到PHP,但我似乎不明白为什么PHP脚本当我清理输入文本框并再次输入时,第二次发送时没有处理传入的POST请求,我得到一个空白的响应。我的代码有更多的阐述。

index.php:

<input type="text" onkeyup="searchmedia(this)" placeholder="Search for seller with UNIQUE ID or Name.">

<div id="resut" style="margin-top:-24px!important;">
    //where the ajax result is returned
</div>
<div style="margin-top:-24px!important;" id="normal">
    //bla bla data here
</div>
<div id="hui" style="display:none;"><img src="../ajax5.gif">
</div>

<script>
    function searchmedia(e) {
        var tuq = $(e).val();
        if (tuq == "") {
            $('#resut').hide();
            $('#normal').show();
            $('#hui').hide();
        } else {
            $('#normal').hide();
            $('#hui').show();
            $.ajax({
                type: 'POST',
                url: 'sellersmessageajax.php',
                data: {tuq: tuq},
                timeout: 5000,
                cache: false,
                success: function (r) {
//console.log(r); 
                    $('#resut').html(r);
                    $('#normal').hide();
                    $('#hui').hide();
                },
                error: function () {
                    alert("Could not search, reload the page and try again.");
                    $('#normal').show();
                    $('#hui').hide();
                }
            });
        }
    }
</script>

sellersmessageajax.php:

<?php include('../connect.php'); ?>


<?php
if (isset($_POST['tuq']))
{

    $term = $_POST['tuq'];

    $term = mysqli_real_escape_string($con,
        $term); //WHEN I ALERT HERE THE SECOND TIME I SEE THE INPUT TEXT DATA THAT CAME IN BUT PLEASE CHECK AFTER THE **FOREACH**


    $condition = '';
    $query     = explode(" ", $term);
    foreach ($query as $text)
    {
        $condition .= "name LIKE '%" . mysqli_real_escape_string($con,
                $text) . "%' OR reign_uniqeer LIKE '%" . mysqli_real_escape_string($con, $text) . "%' OR ";
    }

//WHEN I ALERT HERE I GET NOTHING

    $condition = substr($condition, 0, -4);
    $zobo      = "ORDER BY name";
    $sql_query = "SELECT * FROM sellers_login WHERE " . $condition . $zobo;
    $result    = mysqli_query($con, $sql_query);
    if (mysqli_num_rows($result) > 0)
    {
        while ($row = mysqli_fetch_array($result))
        {
            $v_ida            = $row['id'];
            $v_namea          = $row['name'];
            $v_reign_uniqeera = $row['reign_uniqeer'];
            ?>

            <div style="border-bottom:0.1px solid #eee;padding-bottom:20px;margin-top:20px;">
                <a class="zuka" title="<?php echo $v_ida ?>" id="<?php echo $v_ida ?>"
                   style="color:#666;text-decoration:none;outline:none!important;cursor:pointer;">
                    <b style="color:blue;"><?php echo $v_namea ?></b>
                    <br/>
                    <div style="height:auto;max-height:30px;">
                        <b>UNIQUE ID :</b> <b style="color:red;"><?php echo $v_reign_uniqeera ?></b>
                    </div>
                </a>
            </div>

            <?php
        }
    }
    else
    {
        ?>
        <h1 class="zuka" style="text-align:center;margin-top:20%;"> No result found.</h1>
        <?php
    }
}
?>

2 个答案:

答案 0 :(得分:0)

清除数据结果集后第二次隐藏。第二次数据正在返回,但它隐藏了

ajax success

中添加此行
$('#resut').show(); // Add this line

答案 1 :(得分:-2)

你错误地发送了var tuq。试试这个:

 data :  {"tuq": tuq}