2016-01-13 95 views
-3

的search.php

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post"> 
     <input type="text" name="search" id="search" autocomplete="off"> 
     <button type="submit" class="btn btn-primary">Search</button> 
</form> 
    <?php $search = $_POST['search']; ?> // This is what I've tried. 
    <script type="text/javascript" src="js/search.js"></script> 

我試着加入$search = $_POST['search'];,放入search.js這樣var searchTerm = "<?php echo $search; ?>";

search.js

var ajax_arry=[]; 
var ajax_index =0; 
var sctp = 100; 
$(function(){ 
    $('#loading').show(); 
    var searchTerm = "<?php echo $search; ?>"; // This is what I've tried. 
$.ajax({ 
    url:"scroll.php", 
       type:"POST", 
       data:"actionfunction=showData&page=1&search="+searchTerm, // This is what I've tried. 
    cache: false, 
    success: function(response){ 
     $('#loading').hide(); 
     $('#demoajax').html(response); 

    } 

    }); 
$(window).scroll(function(){ 

    var height = $('#demoajax').height(); 
    var scroll_top = $(this).scrollTop(); 
    if(ajax_arry.length>0){ 
    $('#loading').hide(); 
    for(var i=0;i<ajax_arry.length;i++){ 
    ajax_arry[i].abort(); 
    } 
} 
    var page = $('#demoajax').find('.nextpage').val(); 
    var isload = $('#demoajax').find('.isload').val(); 

    if ((($(window).scrollTop()+document.body.clientHeight)==$(window).height()) && isload=='true'){ 
     $('#loading').show(); 
    var ajaxreq = $.ajax({ 
    url:"scroll.php", 
       type:"POST", 
       data:"actionfunction=showData&page="+page, 
    cache: false, 
    success: function(response){ 
     $('#demoajax').find('.nextpage').remove(); 
     $('#demoajax').find('.isload').remove(); 
     $('#loading').hide(); 

     $('#demoajax').append(response); 

    } 

    }); 
    ajax_arry[ajax_index++]= ajaxreq; 

    } 
return false; 

if($(window).scrollTop() == $(window).height()) { 
    alert("bottom!"); 
} 
}); 

}); 

但是當我得到search from data:"actionfunction=showData&page=1&search="+searchTerm on scroll.php查詢它不起作用。

function showData($data,$con,$limit){ 
$page = $data['page']; 
if($page==1){ 
    $start = 0; 
} 
else{ 
    $start = ($page-1)*$limit; 
} 
$name = $data['search']; 
$sql = "SELECT * FROM product WHERE p_name LIKE '%$name'"; 
+0

你不能寫''裏面的文件.js' PHP code' – Saty

+0

與嘗試替換'?''到'並在你的php文件中包含js – urfusion

+0

對不起<?php $ search = $ _POST ['search'];'?>'這只是發佈時的錯誤。我編輯過。 –

回答

1

你應該在你的php文件中包含你的Js代碼。因爲你不能在js文件中編寫php代碼。而js代碼可以用php編寫。

所以你的代碼應該是這樣的。

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post"> 
    <input type="text" name="search" id="search" autocomplete="off"> 
    <button type="submit" class="btn btn-primary">Search</button> 
</form> 

<?php if ($_POST) { 
    $search = $_POST['search']; 
    ?> 
    <script> 
     var ajax_arry = []; 
     var ajax_index = 0; 
     var sctp = 100; 
     $(function() { 
      $('#loading').show(); 
      var searchTerm = "<?php echo $search; ?>"; // This is what I've tried. 
      $.ajax({ 
       url: "scroll.php", 
       type: "POST", 
       data: "actionfunction=showData&page=1&search=" + searchTerm, // This is what I've tried. 
       cache: false, 
       success: function (response) { 
        $('#loading').hide(); 
        $('#demoajax').html(response); 

       } 

      }); 
      $(window).scroll(function() { 

       var height = $('#demoajax').height(); 
       var scroll_top = $(this).scrollTop(); 
       if (ajax_arry.length > 0) { 
        $('#loading').hide(); 
        for (var i = 0; i < ajax_arry.length; i++) { 
         ajax_arry[i].abort(); 
        } 
       } 
       var page = $('#demoajax').find('.nextpage').val(); 
       var isload = $('#demoajax').find('.isload').val(); 

       if ((($(window).scrollTop() + document.body.clientHeight) == $(window).height()) && isload == 'true') { 
        $('#loading').show(); 
        var ajaxreq = $.ajax({ 
         url: "scroll.php", 
         type: "POST", 
         data: "actionfunction=showData&page=" + page, 
         cache: false, 
         success: function (response) { 
          $('#demoajax').find('.nextpage').remove(); 
          $('#demoajax').find('.isload').remove(); 
          $('#loading').hide(); 

          $('#demoajax').append(response); 

         } 

        }); 
        ajax_arry[ajax_index++] = ajaxreq; 

       } 
       return false; 

       if ($(window).scrollTop() == $(window).height()) { 
        alert("bottom!"); 
       } 
      }); 

     }); 
    </script> 

<?php } ?> 
+0

接受。再次感謝你。 –

0

說明

你做在JavaScript不是 「商店」 PHP變量。 PHP是服務器端,JavaScript在客戶端工作。他們必須溝通。

解決方案

  1. 用PHP創建一個隱藏的輸入,你可以用JavaScript <input type="hidden" name="Language" value="English" id="val1">

  2. 使用ajax sleect使在JavaScript中的PHP文件的調用,使PHP文件返回所需的值

  3. 使用嵌入式JavaScript內部的PHP文件將被服務器編輯給用戶。但我不會推薦這一點。

+0

我不知道該怎麼做,你能幫我做一個例子嗎? –

+0

@ButterToast哪3個? –