Wordpress ajax-> php请求

时间:2016-04-13 20:32:00

标签: php ajax wordpress

很抱歉,如果有这样的主题,但我没有找到解决我问题的方法。所以,让我们去...... =)(我开始学习php)。

这是活动日历,它将是WP的小部件。它需要处理onclick事件。所有代码都写在一个文件中(这是插件)。在小部件(右侧边栏)中,我编写phpcode(用于测试),我调用这个函数。 http://herytire.esy.es/calendar 你可以推动' 2'或者' 5'日期,mysql中有这些日期的数据。 一切正常,但我从未在wordpress中使用php,并且在使用此代码时遇到问题。

问题是:

当我在简单的html页面中使用此代码时,您可以在上面的链接中看到它,一切正常,我得到我点击的一天的结果,但在wp,alert(for error),返回undefined 。两天我试图解决这个问题,但没有。我希望有人可以帮助我..!

** admin-ajax.php已完成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');?>

1 个答案:

答案 0 :(得分:0)

因为您的alert()需要参数。 试试这个:

alert('An error has occurred.');