我在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";
}
}
有人能幫助我。在此先感謝:)
傳遞的search.php文件的完整路徑是這樣http://www.domain.com/wp-content/themes/site_theme/templates/search.php – 2014-10-17 06:36:29
的麪糊瞭解檢查本教程Ajax與WordPress一起使用http://code.tutsplus.com/articles/getting-started-with-ajax-wordpress-pagination--wp-23099 – 2014-10-17 06:45:44