problem with ajax and the path to the php page

i would like to call a php page from an Ajax method : the alert works fine, the code as well has been tested outside of wordpress, but here it seems that the php page is never called.

I found some articles, among them : How to manage ajax calls and JSON in wordpress
but i get more confused. Can you please tell me what should i do here? (with “diapo.php” it doesn’t work, and like that neither)

Read More

here is the call with jquery :

        $('.sous-cat').find('img').click(function(){
                var alt_p = $(this).attr('alt');
                //alert(alt_p);

                $.ajax({
                    type: "POST",
                    url: "<?php bloginfo( 'template_directory' ) ?>/diapo.php",
                    data: {p:alt_p},
                    success: function(data) {
                        alert(data);
                    }
                });
                return false;
        });

and the php page for now :

        <?php
        /*
        $p = $_POST['p'];
        query_posts( 'cat=3&p=$p' );
        while (have_posts()) : the_post();
            $result = the_post_thumbnail('normal');
        endwhile;
        wp_reset_query();*/

        echo '$result';

        ?>

Thanks for your help

Related posts

Leave a Reply

2 comments

  1. To use the WordPress ajax url you can pass the var using wp_localize_script:

    wp_enqueue_script( 'functions', get_bloginfo( 'stylesheet_directory' ) . '/js/functions.js', array( 'jquery' ), false);
        wp_localize_script( 'functions', 'MyAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
    

    In your functions.js

            $.ajax({
                    type: "POST",
                    url:  MyAjax.ajaxurl,
                    data: {p:alt_p},
                    success: function(data) {
                        alert(data);
                    }
                });
    
  2. Calling PHP file directly is more generic way to implement Ajax and should not be used in WordPress.

    See Ajax in Plugins in Codex and try to slowly work through examples there. Yes, it’s not the easiest stuff if you aren’t very experienced in WP, but that is how Ajax should be done in it.