I am trying to use ajax in wordpress
I have two files, php and js
this is my php code
combo_check-out.php
function iRange($first, $last, $format = 'm/d/Y' ) {
$dates = array();
$current = strtotime($first);
$i=1;
while( $i <= $last ) {
$dates[] = date($format, $current);
$current = strtotime('+1 day', $current);
$i++;
}
$time = date("m/d/Y",$current);
return $time;
}
if($_REQUEST)
{
$id = $_REQUEST['parent_id'];
?>
<select name="check-out" id="check-out-date">
<option value="<?php echo iRange($id, 1, $format = 'm/d/Y' ) ?>">"1 Day (Same Day)"</option>
<option value="<?php echo iRange($id, 2, $format = 'm/d/Y' ) ?>">"2 Days"</option>
<option value="<?php echo iRange($id, 3, $format = 'm/d/Y' ) ?>">"3 Days"</option>
<option value="<?php echo iRange($id, 4, $format = 'm/d/Y' ) ?>">"4 Days"</option>
</select>
<?php}?>
and here it is my js code
combo_checkout_iRange.js
$(document).ready(function() {
$('#loader').hide();
$('#check-in-date').change(function(){
$('#check-out-date-wrap').fadeOut();
$('#loader').show();
$.post("combo_check-out.php", {
parent_id: $('#check-in-date').val(),
}, function(response){
setTimeout("finishAjax('check-out-date-wrap', '"+escape(response)+"')", 400);
});
return false;
});
});
//JQuery to hide Loader and return restults
function finishAjax(id, response){
$('#loader').hide();
$('#'+id).html(unescape(response));
$('#'+id).fadeIn();
}
function alert_id()
{
if($('#check-out-date').val() == '')
alert('Please select a sub category.');
else
alert($("#check-out-date").val());
return false;
}
they work fine outside wordpress
how to integrate them in wordpress theme
Note: this should work in post type called “meetings”
so this is what i wrote on function.php
add_action("wp_enqueue_scripts", function() {
if (is_single()) {
if (get_post_type() == 'meetings')
{
wp_enqueue_script('combo_checkout_iRange', get_template_directory_uri() . '/js/combo_checkout_iRange.js', array( 'jquery' ), '1.0' ,true);
}
}
});
First step
the url you need to hit that is :
now there are couple of method to get this into the js . that is your “ajaxurl”
Second Step
with the url , you need to pass a action like :
in jquery , it would be like :
Third Step
into the functions.php you can add
you can see its
thats it
please read this : http://codex.wordpress.org/AJAX_in_Plugins