1

I am practice to create simple ajax wordpress plugin. I refered wordress ajax documentation and some tutorials to create plugin. How can i pass the values to get result from php file. The same code i created in normal php it works fine. How can i solve this. Here are the codes i created.

index.php

<?php
 /*
  Plugin Name: Ajax Serach
 */
  function my_assets() {

  wp_enqueue_style( 'pubmed-style', plugin_dir_url( __FILE__ ).'css/pubmed-style.css' );
  wp_enqueue_script( 'pubmed-script', plugin_dir_url( __FILE__ ).'js/pubmed-script.js', array('jquery'), true );

 wp_localize_script( pubmed-script, litsearch, array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));

 }

  add_action( 'wp_enqueue_scripts', 'my_assets' );
?>

 <?php
    function pubget(){
  ?>     
<input type="text" name="" value="" id="sterm"/> 
<input type="button" name="" value="Search term" id="pub_search"/>

<div id="container">olddata</div>

 <?php
 }
 add_shortcode('pubajax', 'pubget');
 ?>

Test.php

<?php 
class Testclass{
    function infotext(){
        $txt = 'This is a ajax response text';
    }
}
 ?>

Pub.php

<?php
function litsearch(){
    $newterm = $_GET['nwterm'];
    $newtest = new Testclass();
    if($newterm == $_GET['nwterm']){
        include('test.php');
        $newtest = new Testclass();
        $newtest->infotext();
    }
    die();
}
add_action('wp_ajax_litsearch', 'litsearch');
add_action('wp_ajax_nopriv_litsearch', 'litsearch');
 ?>

mainjquery.js

jQuery( document ).ready(function(){
//alert("Jquery Loaded");
  jQuery("#pub_search").click(function(){
    alert("You clicked");
            event.preventDefault();           
            var term= $("#sterm").val();
            console.log('your term send: '+term);
            var data = {'nwterm': term }            
           $.get(litsearch.ajaxurl, data, function(data){
            $("#container").html(data);
          });
        });
});
2
  • What is the return value of the ajax call ? I assume it is -1 or 0
    – Laxmana
    Feb 7 '17 at 13:28
  • Are you working according to this tutorial pippinsplugins.com/…
    – prosti
    Feb 7 '17 at 22:55
1

You have to put the name of your wp ajax action in your data when you do the ajax call.

I assume the ajax url is correct.

jQuery( document ).ready(function(){
//alert("Jquery Loaded");
  jQuery("#pub_search").click(function(){
    alert("You clicked");
            event.preventDefault();           
            var term= $("#sterm").val();
            console.log('your term send: '+term);
            var data = {'nwterm': term, 'action': 'litsearch'}            
           $.get(litsearch.ajaxurl, data, function(data){
            $("#container").html(data.response);
          });
        });
});

Also because you want to put a response in your html code you have to send something back from server.

Test.php

<?php 
class Testclass{
    function infotext(){
        $txt = 'This is a ajax response text';
        return $txt;
    }
}
 ?>

Pub.php

<?php
function litsearch(){
    $newterm = $_GET['nwterm'];
    $newtest = new Testclass();
    $data = array();
    if($newterm == $_GET['nwterm']){
        include('test.php');
        $newtest = new Testclass();
        $data['response'] = $newtest->infotext();
    }
    wp_send_json( $data );
}
add_action('wp_ajax_litsearch', 'litsearch');
add_action('wp_ajax_nopriv_litsearch', 'litsearch');
 ?>
9
  • console give this error Uncaught ReferenceError: litsearch is not defined. i have already created object in wp_localize_script. Feb 7 '17 at 13:51
  • The first two variables in wp_localize_script has to be a string. You don't have quotes around the names. Also check in your html code if the js variable litsearch is there.
    – Laxmana
    Feb 7 '17 at 13:55
  • yes i check it's working but content not showing. it gives only zero. Feb 7 '17 at 13:59
  • According to documentation: if the request succeeds, but the Ajax action does not match a WordPress hook defined with add_action('wp_ajax_(action)', ...) or add_action('wp_ajax_nopriv_(action)', ...), then admin-ajax.php will respond 0.
    – Laxmana
    Feb 7 '17 at 14:01
  • where i can change to get the correct result. Feb 7 '17 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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