2017-10-09 103 views
-1

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

的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

'当我警告我没有任何东西'PHP没有警报功能,那也是JS也在哪里调用这个js函数? 'searchmedia()'通信总是从客户端到服务器。 – ArtisticPhoenix

+0

@ArtisticPhoenix它在第一个输入字段中从'onkeyup =“searchmedia(this)”调用。 – Barmar

回答

-2

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

data : {"tuq": tuq} 
+0

没有区别。除非JavaScript对象文字中包含特殊字符,否则引号是可选的。 – Barmar

+0

如果您认为需要引号,为什么不把引号放在'data'? – Barmar

+0

@SivarajS做到了。 – Lio

0

第二次清除数据结果集后隐藏。第二次数据恢复,但它隐藏

阿贾克斯成功块添加此行

$('#resut').show(); // Add this line