1

wonder if anyone can help; I'm trying to implement some ajax through jquery onto a form in a wordpress template.

The jquery is working, and the I can log a console message in the sucess: section, but the data is 0, when it should be calling the php function (at the moment on the same page, and I can call this directly)

so I guess the jquery is working, the admin-ajax is being called, its just the php function is not being called. Any ideas what I could be doing wrong ? I don't fully understand hooks, so perhaps that is an issue - that I need to hook something in somewhere?

jquery (domain would replace comments)

<script type="text/javascript">
    jQuery(function ($) {
        $( "#movies" ).autocomplete({
                minLength:2,
                delay:500,
                source: function( request, response ) {
                    $.ajax({
                        type: 'POST',
                        url: "http://<!--domain here -->/wp-admin/admin-ajax.php",
                        dataType: 'json',
                        data: {
                            action: 'getMoviesForCode',
                            searchString: $("#movies").val()
                        },
                        success: function( data ) {
                            response(data);
                            console.log('jjj'+data);
                        }
                    });
                }           
        });
    });
    </script> 

php function (on same page)

<?php

    function getMoviesForCode(){
echo "
        <script type=\"text/javascript\">
        alert(\"hh\");
        </script>
    ";
   $searchString = $_POST['searchString'];
   $results = va_getMoviesForCode($searchString);  
  $results = json_encode($results);
  die($results);
}
 ?>

Thanks,

1 Answer 1

6

You're doing it wrong. You php function should be in your theme's functions.php file.

You should then hook the function to wp_ajax_[your_action] and wp_ajax_nopriv_[your_action].

Example of what should be in your functions.php :

function getMoviesForCode(){
echo "
        <script type=\"text/javascript\">
        alert(\"hh\");
        </script>
    ";
   $searchString = $_POST['searchString'];
   $results = va_getMoviesForCode($searchString);  
  $results = json_encode($results);
  die($results);
}
add_action('wp_ajax_getMoviesForCode', 'getMoviesForCode');
add_action('wp_ajax_nopriv_getMoviesForCode', 'getMoviesForCode');
Sign up to request clarification or add additional context in comments.

5 Comments

Hi, That's brilliant - I can now see the return in firebug response so it's calling the function correctly. just need to get the autocomplete to work in the field now, many thanks !
by the way i changed it to add_action('wp_ajax_getMoviesForCode', 'getMoviesForCode'); add_action('wp_ajax_nopriv_getMoviesForCode', 'getMoviesForCode'); - I'm guessing that's right ?
thanks for the link, I'll hve a look. it's quite good knowing a bit more about ajax, thinking I'll be using it alot more now !
Hi, if we write a plugin then can I handle the backend in plugin php file without writin in function.php?
Yes, the advantage of using a plugin is that you still have the functionality if you change theme.

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.