dongwang3066 2014-10-17 06:28
浏览 43
已采纳

如何在php中实现wordpress中的ajax和代码

I made a search box in php and it works fine ... but when i implement it in wordpress it does not works correctly. I think the ajax is not working fine in wordpress.

I want to implement at it on cart page and want to get results also on same page.

//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>

//and here is my action file

$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";
}
}

can anyone help me. Thanks in Advance :)

  • 写回答

1条回答 默认 最新

  • dongqianchi0512 2014-10-17 07:03
    关注

    With the provided information it's not possible to tell what's wrong. Could you provide a link to your site? What toes the console tab in Firebug (or another debuger, on chrome the network tab) tell you about the ajax request? Is it sent at all?

    One thing you could try is to replace the $ sign for jQuery with jQuery (the $ sign is used by other librarys too, that are maybe used by your template, or jQuery may runs in silent mode. Try this code:

    <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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料