YaoRaoLov 2016-04-13 20:32 采纳率: 50%
浏览 19

WordPress的ajax-> PHP请求

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.

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');?>
  • 写回答

1条回答 默认 最新

  • weixin_33738578 2016-04-13 21:06
    关注

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

    alert('An error has occurred.');

    评论

报告相同问题?

悬赏问题

  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误