2014-10-17 29 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