简体   繁体   English

在Wordpress中通过jquery调用运行php文件

[英]Run php file via jquery call in Wordpress

So I wish to replicate the following functionality in wordpress. 因此,我希望在wordpress中复制以下功能。 Jquery calls a php file, which itself queries a mysql table, and returns the result encapsulated within an tag. jQuery调用一个php文件,该文件本身查询mysql表,并返回封装在标记中的结果。 How do I go about achieving this?: 我该如何实现这一目标?:

 <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> <script type="text/javascript"> .... function initialize() { .... feedData(); } $(document).ready(function () { initialize(); }); function feedData() { $(document).ready(function () { $.ajax({ cache: false, type: "POST", async: false, url: "page-gotw-search.php", data:{"action=showcountries"}, success: function (data) { $('#CountryList').append(data); }, error: function (data, status, error) { console.log(data); console.log(status); console.log(error); } }); }); } </script> <body> <div style="width: 800px"> <div style="float: left"> <select id="CountryList" onchange="getRegion()" size="20"></select> <select id="RegionList" size="20" onchange="getMap()"></select> </div> <div id="cityList" style="float: right"></div> </div> </body> </html> 

page-gotw-search.php page-gotw-search.php

 <?php include_once("pdo_mysql.php"); pdo_connect("localhost","root",""); pdo_select_db("wpdb"); $action=$_POST["action"]; if($action=="showcountries"){ $showcountry = pdo_query("Select distinct meta_value from wp_usermeta where meta_key =?, pdo_real_escape_string('country_registration')"); if (!$showcountry) { $message = 'Invalid query: ' . pdo_error() . "\\n"; $message .= 'Whole query: ' . $showcountry; die($message); }else{ foreach($showcountry as $row){ echo '<option value=".$row[country_code].">.$row[country_name].</option>'; } } } else if($action=="showregions"){ $country_id= $_POST["country_id"]; $showregion = pdo_query("Select region_code, region_name from regiontbl WHERE country_id=?", pdo_real_escape_string($country_id)); if (!$showregion) { $message = 'Invalid query: ' . pdo_error() . "\\n"; $message .= 'Whole query: ' . $regionquery; die($message); }else{ foreach($showregion as $row){ echo '<option value=".$row[region_code].">.$row[region_name].</option>'; } } } ?> 

Looks like you want to implement ajax into the wordpress. 看起来您想在Wordpress中实现Ajax。

I have a simple way to do this. 我有一个简单的方法来做到这一点。 Follow below given steps to use ajax calls in wordpress 请按照以下给定的步骤在Wordpress中使用Ajax调用

add some code in footer.php 在footer.php中添加一些代码

jQuery(‘#clickerid').change(function(){
var your_id = jQuery(‘#get_val').val();
var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
jQuery.ajax({
               cache: false,
               type: 'POST',
               url: '<?php echo admin_url('admin-ajax.php'); ?>',
               data: ‘id='+ id + '&action=get_id',
               success: function(data) 
               {   
                jQuery(‘#id').html(data);

               }
           });
       });

Then use this ajax call into the functions.php 然后使用此ajax调用进入functions.php

add_action( 'wp_ajax_get_your_action', 'prefix_ajax_get_your_action' );
add_action( 'wp_ajax_nopriv_get_your_action', 'prefix_ajax_get_your_action' );

function prefix_ajax_get_costofcare() {
    // Do your php code used in ajax call and return it.
}

Ajax calls works via admin-ajax.php which is build in functionality provided by wordpress Ajax调用通过admin-ajax.php进行工作,而admin-ajax.php是wordpress提供的内置功能

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM