Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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....

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

2 Answers 2

up vote 1 down vote accepted

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);
            }
        });
    }); 
});
share|improve this answer
    
It works thanks ... –  Ajay Patel Mar 30 '12 at 7:02
1  
don't forget to add a nonce. it is talked about in the link that jared gave. –  helgatheviking Mar 30 '12 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 Mar 31 '12 at 6:46
    
see my update. should (in theory) pass data in $_POST –  helgatheviking Apr 2 '12 at 14:28

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.

share|improve this answer
    
$.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. –  helgatheviking Mar 30 '12 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 Mar 30 '12 at 15:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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