Php 如何通过jquery从数据库接收数据?[wordpress.js]

Php 如何通过jquery从数据库接收数据?[wordpress.js],php,jquery,wordpress,Php,Jquery,Wordpress,我试图从wordpress主题的.js文件中的数据库中获取一些数据。 我尝试了jquery的.post(),但什么都没有发生 也请向我推荐任何替代品 js文件中的代码 jq.post("../abc.php", { name:"kumar", accId:window.accommodationId }, function(data,status) { alert("hel

我试图从wordpress主题的.js文件中的数据库中获取一些数据。 我尝试了jquery的.post(),但什么都没有发生

也请向我推荐任何替代品

js文件中的代码

jq.post("../abc.php",
        {
        name:"kumar",
        accId:window.accommodationId

        },  function(data,status)
            {
                alert("hello");

             //alert("Data: " + data + "\nStatus: " + status);
            }
        );
abc.php文件中的代码

<?php
global $wpdb;

$max_minAge = $wpdb->get_results( "SELECT price_per_day FROM  wp_byt_accommodation_vacancies where accommodation_id='1741'" );   

echo $max_minAge[0]->price_per_day;
?>

您可以使用
jQuery AJAX-get
速记:

$.get( "../abc.php", function( data ) {
  alert( "Data Loaded: " + data );
});

很好地了解:因为您要从文件中获取数据,所以应该使用
get

您可以在functions.php文件中使用像这样的wp\u ajax钩子

  // script atyle add at frontend
    add_action( 'wp_enqueue_scripts','my_scripts_style');

 function my_scripts_style()
{
    wp_enqueue_script( 'scriptid', PATH_TO . 'script.js', array('jquery') );
    // localize the script
    wp_localize_script( 'scriptid', 'myAjax', array( 'url' =>admin_url( 'admin-ajax.php' ),'nonce' => wp_create_nonce( "ajax_call_nonce" )));
}
然后添加ajax钩子

   // action for execute ajax from frontend 
    add_action( 'wp_ajax_nopriv_execute_ajax','execute_ajax');

function execute_ajax() 
{
    $nonce = check_ajax_referer( 'ajax_call_nonce', 'nonce' );
    if($nonce==true)
    {
    // here you will perform all the db communication get data from db and send it to the view area.
    echo 'test this';   
    die();
     }
}
然后在您的js文件中,您通过上面的
enque_脚本
包含了该文件。用这个

jQuery(function(){
jQuery('.click-onthis').live('click', function(){ // get data by click
    var data = {
                    action: 'execute_ajax',
                    nonce: myAjax.nonce,
                    // anyother code etc
                };

                jQuery.post( myAjax.url, data, function(response) 
                {
                    if(response=='success')
                    {

                    }
                });
        });
    });

jquery
单击此
将在单击一个链接时工作,您可以在加载时进行通信或任何其他事件

它被称为
AJAX
异步javascript和xml。使用wp\u AJAX钩子处理wp中的所有AJAX请求请明确说明我必须在.php文件中添加任何特殊代码,或者如何。如果我希望加载它,该怎么办,意思是当这个js加载时应该回显值。只需删除单击功能就可以实现你的建议,好的,让我们来筛选它,我应该在我的.php文件中添加前两个部分,然后在路径上写什么,在哪里添加查询。我不知道其他答案是什么,我只是通过Wordpress方式添加了这些内容。php函数和钩子将被添加到functions.php文件中,jquery脚本将被添加到脚本文件中,您可以在functions.php中定义它的路径。您只需要添加jquery文件的路径,如果它在wordpress主题的js目录中,那么它将是
get\u bloginfo('template\u url'))“/js/script.js”
没有出现警报,如何检查$wpdb是否正常工作。我已经把这个文件放在我的主题的根文件夹中,当然,在这个JS之前有没有包含jQuery,但这是我正在关注的wordpress主题。。我是新来的