2014-10-17 24 views
0

我在php中做了一個搜索框,它工作正常......但是當我在wordpress中實現它時,它無法正常工作。我認爲Ajax在wordpress中運行不正常。如何在php中實現ajax和代碼從WordPress

我想在購物車頁面上實現它,並希望在同一頁面上獲得結果。

//here is my form 
<script> 
$(document).ready(function() 
{ 
    $('#form').submit(function() { 
    $.ajax({ 
     data: $(this).serialize(), 
     type: $(this).attr('method'), 
     url: $(this).attr('action'), 
     success: function(response) { 
      $('#result').html(response); 
     } 
    }); 
    return false; 
}); 
}); 
</script> 




<body> 
<form method="post" id="form" action="search.php"> 
    <input type="text" name="record" id="record" placeholder="Enter Zip Code" onkeypress="return isNumberKey(event)"> 
    <input type="submit" class="submit" value="find"> 
</form> 
<div id="result" style="height:50px;width:200px;"></div> 




<SCRIPT language=Javascript> 
     function isNumberKey(evt) 
     { 
      var charCode = (evt.which) ? evt.which : evt.keyCode; 
      if (charCode != 46 && charCode > 31 
      && (charCode < 48 || charCode > 57)) 
      return false; 
      return true; 
     } 
    </SCRIPT> 

//這裏是我的操作文件

$file = 'PINCODES DELHIVERY.csv'; 
if ($_POST) { 
$searchfor = $_POST['record']; 
if (!empty($searchfor)) { 
if(strlen($searchfor)==6) { 
    header('Content-Type: text/plain'); 
$contents = file_get_contents($file); 
$pattern = preg_quote($searchfor, '/'); 
$pattern = "/^.*$pattern.*\$/m"; 
if(preg_match_all($pattern, $contents, $matches)){ 

    echo "YES"; 
} 
else{ 
    echo "NO"; 
} 
} else { 
    echo "Enter valid zip code"; 
} 
} else { 
    echo "Enter Zip Code First"; 
} 
} 

有人能幫助我。在此先感謝:)

+0

傳遞的search.php文件的完整路徑是這樣http://www.domain.com/wp-content/themes/site_theme/templates/search.php – 2014-10-17 06:36:29

+0

的麪糊瞭解檢查本教程Ajax與WordPress一起使用http://code.tutsplus.com/articles/getting-started-with-ajax-wordpress-pagination--wp-23099 – 2014-10-17 06:45:44

回答

0

有了所提供的信息,不可能知道什麼是錯的。你能提供一個鏈接到你的網站? Firebug中的控制檯選項卡(或另一個debuger,在網絡選項卡上顯示)告訴你關於ajax請求的信息?它是否被髮送?

有一兩件事你可以嘗試是jQuery更換$標誌的jQuery($符號由其他圖書館的太多,這是可能使用你的模板中使用,或jQuery的可能以靜默模式運行試試這個代碼:

<script> 
    jQuery(document).ready(function() { 
     jQuery('#form').submit(function(e) { 
      e.preventDefault(); 
      jQuery.ajax({ 
       data: jQuery(this).serialize(), 
       type: jQuery(this).attr('method'), 
       url: jQuery(this).attr('action'), 
       success: function(response) { 
        jQuery('#result').html(response); 
       } 
      }); 
     }); 
    }); 
</script> 
在AJAX網址
+0

user2989952感謝回覆, 這是我的網站的網址:http:// goo .gl/9TlYPW 查看錶格有佔位符「輸入郵政編碼」。它會在下一頁顯示結果,但我們希望在同一頁面上顯示結果。 – 2014-10-17 09:00:27

+0

user2989952謝謝,它現在對我很好,謝謝:) – 2014-10-17 09:14:08