0

I am very new to ajax with wordpress . Here i am getting a selected option value in var test using bellow function when i alert it its showing a right value.

This var test value i have to send using ajax and i want GET this value in PHP variable.

So what i required to call ajax and get script variable value of var test in php in plugin file where i have both bellow function and select box.

JQuery Funtion.

jQuery("#time_slot").change(function()
{

var test= time_slot.value;
alert(test);

});

Select Box

<select name="time_slot" id="time_slot" class="time_slot">
<option value="">Choose a Time</option>
<option value="1:00 - 3:00">01:00 AM-03:00 AM</option>
<option value="3:00 - 5:00">03:00 AM-05:00 AM</option>
</select>

5 Answers 5

1

Use the built-in wordpress Ajax actions:

Your jquery will look like this:

$('#ajax_form').bind('submit', function() {
    var form = $('#ajax_form');
    var data = form.serialize();
    data.action = 'MyPlugin_GetVars'
    $.post('/wp-admin/admin-ajax.php', data, function(response) {
        alert(response);           
    });
return false; 

Your plugin code something like:

add_action("wp_ajax_MyPlugin_GetVars", "MyPlugin_GetVars");
add_action("wp_ajax_nopriv_MyPlugin_GetVars", "MyPlugin_GetVars");

function MyPlugin_GetVars(){
    global $wpdb;
    // use $wpdb to do your inserting

    //Do your ajax stuff here
    // You could do include('/wp-content/plugins/test/getvars.php') but you should
    // just avoid that and move the code into this function
}

For more info please visit AJAX in Plugins

1
  • can you tell me the file structure according to question code Jul 19, 2014 at 5:59
0

please look at below, it may help to you. Here it is your PHP code.

<?php

function ajax_enqueuescripts() {
    wp_enqueue_script('ajaxloadpost', plugins_url().'/your-plugin-name/js/my-ajax.js', array('jquery'));
    wp_localize_script( 'ajaxloadpost', 'ajax_postajax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}

add_action('wp_enqueue_scripts', ajax_enqueuescripts);

add_action('wp_ajax_nopriv_ajax_ajaxhandler', 'my_action_callback' );
add_action('wp_ajax_ajax_ajaxhandler', 'my_action_callback' );

function my_action_callback(){  
    print_r($_GET);
    die;
}

And here it is your JS code. You need to create new JS file and put below code in your new JS file.

jQuery(document).ready(function(){
 jQuery("#time_slot").change(function()
{

    var test= time_slot.value;

jQuery.ajax({
        type: 'GET',
        url: ajax_postajax.ajaxurl,
        data: {
            action: 'ajax_ajaxhandler',
            test : test   
        },
        success: function(data) {
            alert(data);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert("Error");
        }
    });

    return false;
  });
});

For more details, you can check here

14
  • what to do with this /your-plugin-name/js/my-ajax.js can elaborate it Jul 19, 2014 at 6:17
  • You need to put your js code in my-ajax.js(file name of js). So "/your-plugin-name/js/my-ajax.js" will be your path to JS file. Jul 19, 2014 at 6:37
  • booking.php is file name where i am getting Jqery value which i have to send in ajax call so where i will put this function ajax_enqueuescripts() { and can create the ja file under the booking.php file and can i put file name of that js file in Jul 19, 2014 at 6:44
  • getting error arse error: syntax error, unexpected T_STRING, expecting T_FUNCTION on this hook line add_action('wp_enqueue_scripts', ajax_enqueuescripts); Jul 19, 2014 at 6:53
  • i have done all thing you said in answer but getting above error Jul 19, 2014 at 6:56
0

Add this in Your Plugin php page

 add_action("wp_ajax_ajax_plugin_call", "ajax_plugin_call_callback"); 
 add_action("wp_ajax_nopriv_ajax_plugin_call", "ajax_plugin_call_callback");

Ajax Function in Php Page

function ajax_plugin_call_callback{
   global $wpdb;
   print_r($_POST) ; 
   // write your Php Code
   die;

}

Your Javascript Code like this

jQuery(document).ready(function(){
  jQuery('#formID').bind('submit', function() { // Form submit Button
      var form_value =  jQuery('#formID'); 
      var form_data = form_value.serialize(); //Serialize the Form 
      form_data['action']='ajax_plugin_call'; // Action 
     jQuery.post('<?php  echo admin_url('admin-ajax.php'); ?>',form_data,function(response){

         // Here is the Ajax Response

       });
    }
 }); 

Please Check this, it will provide you help in WordPress Ajax call in plugin.

5
  • Thanks for your answer, but please elaborate. Your answer is considered code dumping.
    – engelen
    Jul 19, 2014 at 10:54
  • @engelen hey i have used ajax like var data = { current_datedddd: sdfdsfsdf, post_id: "'.$duplicate_of.'", action: "call_addon_price1" '.$attribute_fields_str.' }; alert(sdfdsfsdf); its posting time value but in function action: "call_addon_price1" i am not able to store in variblle using POST Jul 19, 2014 at 11:27
  • can you tell me what is "$attribute_fields_str"? Jul 19, 2014 at 11:31
  • @engelen you can check my ajax call test.skovian.com/thefoodieclub.com/dine/dine/… Jul 19, 2014 at 11:34
  • Please Try this add_action("wp_ajax_ajax_call_addon_price1", "call_addon_price1_callback"); add_action("wp_ajax_nopriv_call_addon_price1", "call_addon_price1_callback"); function call_addon_price1_callback(){ try this in your plugin print_r($_POST); die; } Jul 19, 2014 at 11:45
0

I have call ajax using bellow code its working good. Here i am sending the variable time to this page using post $.post("'. plugins_url().'/woocommerce-booking/gettime.php",

var time=0;
jQuery("#time_slot").change(function()
                                {   
                                time=time_slot.value;
                            $.post("'. plugins_url().'/woocommerce-booking/gettime.php",
                                 {name: time},
                                  function(response){
                                  console.log(response);
                                  }
                                );  
                                 });

my gettime.php file will be look like bellow and getting response from my ajax call.

<?php session_start();
    echo $name= $_POST["name"];
    file_put_contents("name.txt", $name)

 ?>
0

You must first enqueue the script and define the URL. Then you have to create a function for your AJAX callback and add a WordPress action that calls it. Here is a video where I explain it, hope it helps. :)

https://www.youtube.com/watch?v=R7OK-TtNuEc

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.