WordPress ajax->php request

Sorry if there is theme like this, but i didn’t find solution for my problem. So let’s go… =)(I’m starting to learn php).

This is calendar of events and it will be widget for WP. It need to work onclick event. All code written in one file(this is plugin). In widget(in right sidebar) i write phpcode(for testing) where i call this functions.
http://herytire.esy.es/calendar
You can push ‘2’ or ‘5’ dates, there is data in mysql for these dates.
All works fine, but i never work with php in wordpress, and have problems with this code.

Read More

The problem is:

When i use this code in simple html page, you can see it at link above, all works fine, I get results for day that I click, BUT in wp, alert(for error), returns undefined. Two days I’m trying to fix this problem, but nothing. I hope somebody can help me please..!

**admin-ajax.php is done for view side of wp!

<script type="text/javascript"> // ajaxurl in header.php
    var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";
</script>

/*
Plugin Name: Release Dates
Description: Small calendar for serials release dates.
Author: J_J
Version: 1.0
*/

function draw_calendar($month, $year) // creating calendar
{
  $month = date('m');
  $year = date('y');
  $calendar = '<table class="calendar">';
  $headings = array('S', 'M', 'T', 'W', 'T', 'F', 'S');
  $calendar .= '<tr class="calendar-row"><td class="calendar-day-head">'.implode('</td><td class="calendar-day-head">', $headings).'</td></tr>';
  $running_day = date('w', mktime(0, 0, 0, $month, 1, $year));
  $days_in_month = date('t', mktime(0, 0, 0, $month, 1, $year));
  $days_in_this_week = 1;
  $day_counter = 0;
  $dates_array = array();
  $calendar .= '<tr class="calendar-row">';
  for ($x = 0; $x < $running_day; ++$x) {
      $calendar .= '<td class="calendar-day-np"> </td>';
      ++$days_in_this_week;
  }
  for ($list_day = 1; $list_day <= $days_in_month; ++$list_day) {
      $calendar .= '<td class="calendar-day"><div class="day-number"><a style="cursor:pointer;" onClick="relDaySerial('.$list_day.');">'.$list_day.'</a></div></td>';
      if ($running_day == 6) {
          $calendar .= '</tr>';
          if (($day_counter + 1) != $days_in_month) {
              $calendar .= '<tr class="calendar-row">';
          }
          $running_day = -1;
          $days_in_this_week = 0;
      }
      ++$days_in_this_week;
      ++$running_day;
      ++$day_counter;
  }
  if ($days_in_this_week < 8) {
      for ($x = 1; $x <= (8 - $days_in_this_week); ++$x) {
          $calendar .= '<td class="calendar-day-np"> </td>';
      }
  }
  $calendar .= '</tr>'.'</table>';
  return $calendar; }

function get_serials($day) { // here i catch relDaySerial:id
 if ($_POST['relDaySerial'] === null) {
    $day = date('d');
} else {
    $day = $_POST['relDaySerial'];
}

global $wpdb;

$result = $wpdb->get_results("SELECT * FROM tvt_calendar WHERE day=$day");
foreach ($result as $value) {
    echo "
  <table id='cal-data'>
    <tr>
      <td class='calendar-day-head'>TV Show</td>
      <td class='calendar-day-head'>S</td>
      <td class='calendar-day-head'>E</td>
    </tr>
    <tr class='cal-content'>
      <td><a href='#'>".$value->title.'</a></td>
      <td>'.$value->season.'</td>
      <td>'.$value->series.'</td>
    </tr>
  </table>
    ';
}}
function my_action_javascript() {
  ?>
<script type="text/javascript" >
function relDaySerial(id) {
jQuery.ajax({
  type:'POST',
  url:ajaxurl,
  data:{relDaySerial:id},
success:function(data) {
  if(relDaySerial.type == "success") {
     jQuery('#cal-bottom').html(data)
  }
  else {
     alert()
   }
}});}
</script>
<?php}
add_action('wp_footer', 'my_action_javascript');
add_action('wp_ajax_my_action_javascript', 'my_action_javascript');
add_action('wp_ajax_nopriv_my_action_javascript', 'my_action_javascript');?>

Related posts

1 comment

  1. Because your alert() requires a parameter.
    Try with this:

    alert('An error has occurred.');

Comments are closed.