2017-10-09 140 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