1

I am trying to fetch few data from database in my .js file of wordpress theme. I tried with .post() of jquery but nothing is happening.

Please also suggest me any alternate.

code in .js file

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

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

             //alert("Data: " + data + "\nStatus: " + status);
            }
        );

code in abc.php file

<?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;
?>
3
  • It's called AJAX asynchronous javascript and xml. Commented Jan 19, 2015 at 10:27
  • Use wp_ajax hook for all kind of ajax requests in wp codex.wordpress.org/Plugin_API/Action_Reference/… Commented Jan 19, 2015 at 10:28
  • please make it clear that i have to add any special code in my .php file or what. Commented Jan 19, 2015 at 10:30

2 Answers 2

1

you can use wp_ajax hooks like this in your functions.php file

  // 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" )));
}

Then add the ajax hook

   // 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();
     }
}

then in your js file which you included via enque_script above. use this

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-this will work when click on a link you can coomunicate on load or any other event

Sign up to request clarification or add additional context in comments.

4 Comments

what if i want it on load, means value should be echoed when this js load.
just remove the click function
tough to implement your suggestion, ok lets screen it, Should I add top two section in my .php file, then what to write on PATH_TO, and where to add query.
I don't know what the other answers are saying, I just added the the things via Wordpress way. and the php functions and hooks will be added to your functions.php file and the jquery script will be in your script file which you enque in your functions.php the PATH_TO is nothing you just need to add the path of your jquery file, if its in the wordpress theme's js directory then it will be get_bloginfo('template_url').'/js/script.js'
0

You can use the jQuery AJAX get shorthand:

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

Good to know: Since you're getting data from a file, you should use a GET.

3 Comments

no alert appeared, how to check if $wpdb is working or not. I have placed this file in root folder of my theme
Have you included jQuery before this JS?
ofcourse, but this is wordpress theme I am following.. where I am new

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.