1

I do not know why this response me -1 always? I am using this code in my plugin file. i want response on click p tag

<?php    function myajax(){ ?>
        <script type="text/javascript">
        jQuery(document).ready(function(){
                jQuery("#testbutton").click(function(){
                     jQuery.ajax({
                        url: "<?php echo admin_url('admin-ajax.php'); ?>",
                        type: 'POST',
                        data: {
                        action: 'stravyfuncajax22'
                        },
                        dataType: 'html',
                        success: function(response) {
                        alert(response);
                        }
                    });
                }); 

            });
    </script><?php
    echo'
        <p id="testbutton1">click</p>';
    }

    add_shortcode("my_ajax", "myajax");


    add_action('wp_ajax_stravyfuncajax22', 'testfunc11');
    add_action('wp_ajax_nopriv_stravyfuncajax22', 'testfunc11');

    function testfunc11() {
    echo "2";
    die();
    }
?>

EDIT : added php tag now....

3
  • 1
    Looks like some of your code didn't come through. Can you repost it? Commented Mar 29, 2012 at 20:21
  • 2
    Is that the exact code you're using? If so you're missing <?php ?> tags.
    – soulseekah
    Commented Mar 29, 2012 at 20:24
  • @Soulseekah Can you please check it now///
    – Ajay Patel
    Commented Mar 30, 2012 at 5:45

2 Answers 2

1

according to wordPress jedi, scribu, you can now enqueue scripts from inside the plugin handler.

http://scribu.net/wordpress/conditional-script-loading-revisited.html

you were echoing things and in shortcode handling you have to return. i ran into some issues getting the script to work by returning the script, so i moved it to an external file which definitely works.

function myajax(){ 

    wp_enqueue_script('my-script', plugins_url('my-script.js', __FILE__), array('jquery'), '1.0', true);

    $data = array( 
        'ajaxurl' => admin_url('admin-ajax.php')
    );

    wp_localize_script( 'my-script', 'myscriptL10n', $data );

    $text = '<a id="testbutton">click</a>';

    return $text;
}
add_shortcode("my_ajax", "myajax");


add_action('wp_ajax_stravyfuncajax22', 'testfunc11');
add_action('wp_ajax_nopriv_stravyfuncajax22', 'testfunc11');

function testfunc11() {
    echo "2";
    die();
}

and then the my-script.js

jQuery(document).ready(function($){
    $("#testbutton").click(function(){
        $.ajax({
            url: myscriptL10n.ajaxurl,
            type: 'POST',
            data: {
                action: 'stravyfuncajax22'
            },
            dataType: 'html',
            success: function(response) {
                alert(response);
            }
        });
    }); 
});

hope that helps.

EDIT 1 i didn't test this part ,but i think it should work for echoing a form value as the ajax response.

` function myajax(){

    wp_enqueue_script('my-script', plugins_url('my-script.js', __FILE__), array('jquery'), '1.0', true);

    $data = array( 
        'ajaxurl' => admin_url('admin-ajax.php')
    );

    wp_localize_script( 'my-script', 'myscriptL10n', $data );

    ob_start(); ?>

    <form id="form">
    <input type="text" name="foo" value="bar"/>
    <a id="testbutton">click</a>';
    </form>

    <?php $text = ob_get_contents();

    return $text;
}
add_shortcode("my_ajax", "myajax");


add_action('wp_ajax_stravyfuncajax22', 'testfunc11');
add_action('wp_ajax_nopriv_stravyfuncajax22', 'testfunc11');

function testfunc11() {
    $data = $_POST['data'];
    echo $data['foo'];
    die();
}

`

new javascript

jQuery(document).ready(function($){
    $("#testbutton").click(function(){
         data = $('#form').serialize();
        $.ajax({
            url: myscriptL10n.ajaxurl,
            type: 'POST',
            data: {
                action: 'stravyfuncajax22',
                                data: data
            },
            dataType: 'html',
            success: function(response) {
                alert(response);
            }
        });
    }); 
});
3
  • 1
    don't forget to add a nonce. it is talked about in the link that jared gave. Commented Mar 30, 2012 at 12:15
  • can you please help me how can i pass data in arry abd get it $data = array( 'ajaxurl' => admin_url('admin-ajax.php') );
    – Ajay Patel
    Commented Mar 31, 2012 at 6:46
  • see my update. should (in theory) pass data in $_POST Commented Apr 2, 2012 at 14:28
2

Try JSON encoding your response before echoing it like so (don't forget the header either!).

function testfunc11() {
    $response = json_encode( array( 'code' => 2 ) );
    header( "Content-Type: application/json" );
    echo $response;
    exit;
}

And then up in your JS code..

alert( response.code );

And it should show you '2'.

However, I wouldn't recommend using jQuery.ajax, instead I would recommend using this method which is (in my opinion) much easier to read and use.

2
  • $.post is a shorthand way of using $.ajax for POST requests, so they're very similar. $.ajax has a very more config options, and better error handling. Commented Mar 30, 2012 at 12:16
  • Ok, that makes sense. I just think that $.post is a bit easier to read and use (hence less config options :P).
    – Jared
    Commented Mar 30, 2012 at 15:42

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.