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

我正在尝试从Wordpress主题的.js文件中从数据库中获取一些数据.我尝试使用jquery的.post(),但是没有任何反应.还请提出其他建议..js文件中的代码jq.post(../abc.php,{name:kumar,accId:window.accommodationId}...

我正在尝试从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;
?>

解决方法:

您可以在functions.php文件中使用类似wp_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();
     }
}

然后在您通过上面的enque_script包含的js文件中.用这个

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 click-on-单击链接可以在负载或任何其他事件上通信时起作用

本文标题为:php-如何通过jquery从数据库接收数据? [wordpress .js]

基础教程推荐