duanjianl183188 2017-10-09 19:08
浏览 50

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

I have this strange issue, happening to my PHP script, On page load the AJAX script runs and also after the second time the AJAX script runs it works and sends data to PHP, but i seem to not understand why the PHP script doesn't process the incoming POST request the second time it is sent in when i clean the input text box and type again, i get a blank response.My code for more expatiation.

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条回答 默认 最新

  • douwenan9849 2017-10-09 19:22
    关注

    you are sending the var tuq wrongfully. Try this:

     data :  {"tuq": tuq}
    
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?