Hussain Rahimi
Hussain Rahimi

Reputation: 841

ajax in wp front end

I want to use ajax in wp plugin front end not the admin pages, I used wp_localize_script() to be able to access ajaxurl in front end and it works. Here is my ajax code

$j = jQuery.noConflict();

function commonProvinceChange() {

    $j(".province").change(function() {
       var selectedProvinceId = $j(this).val();
       var districts = "<option disabled selected='selected'>Select District</option>";
       $j.post(naqdina_map.ajaxurl, {
         province_id : selectedProvinceId,
         action : 'province_districts'
        }, function(data, status) {
          alert(naqdina_map.ajaxurl);
                            data = JSON.parse(data);
                            $j(".district").html(districts);
                            for ( var i = 0; i < data.length; i++) {
                                $j(".district").append(
                                        "<option value=" + data[i].id + ">"
                                                + data[i].district_name
                                                + "</option>");
                            }// for
                        });

                    });
}// commonProvinceChange()

and on my plugin php file

function naqdina_front_script() {

    wp_enqueue_script ( 'jquery' );

    wp_register_script ( 'naqdina_map_script', plugins_url ( 'naqdina_map/map.js', __FILE__ ), array (
            'jquery' 
    ) );
    wp_localize_script('naqdina_map_script','naqdina_map',array('ajaxurl'=>admin_url('ajax-admin.php')));
    wp_enqueue_script ( 'naqdina_map_script' );
}//naqdina_front_script()

add_action ( 'wp_enqueue_scripts', 'naqdina_front_script' );

and then

add_action ( 'wp_ajax_province_districts', 'province_districts' );

function province_districts() {

    global $wpdb;

        $province_id = $_POST ['province_id'];

    $pname = $wpdb->get_results ( "select d.id,d.name as district_name from naqdina_districts as d join naqdina_provinces as p on(d.province_id=p.id) where p.id='" . $province_id . "'" );

    echo json_encode ( $pname );
    die (); // use die() to prevent further content

} // province_districts()

here the post() method in my js code above returns html code of the whole page not the the response echoed by province_districts() function.

Could someone help??

Upvotes: 0

Views: 413

Answers (2)

Hussain Rahimi
Hussain Rahimi

Reputation: 841

Problem solved, I had a ridiculous mistake.
It is admin-ajax.php not ajax-admin.php.
Replacing

wp_localize_script('naqdina_map_script','naqdina_map',array('ajaxurl'=>admin_url('ajax-admin.php')));  

with

wp_localize_script('naqdina_map_script','naqdina_map',array('ajaxurl'=>admin_url('admin-ajax.php')));  

solved my problem.

Upvotes: 0

Dan Cowell
Dan Cowell

Reputation: 385

You need to use add_action ( 'wp_ajax_nopriv_province_districts', 'province_districts' ); to use AJAX in the frontend.

EDIT: In map.js on line 9 you are POSTing to "" which resolves as a relative URL to the current page. If you replace that with naqdina_map.ajaxurl your code will work.

Edit: This is what you need to do.

Upvotes: 1

Related Questions