douye2110 2016-11-16 22:27
浏览 47
已采纳

在Wordpress上使用ajax的多个表单

I have a problem. I have a forum that shows a popup, when the user clicks "OK", I use ajax to switch the "1" to a "0" and the message is not shown again.

Here is my function.

`add_action( 'wp_ajax_profile_msg', 'profile_msg' ); 
 function profile_msg() {
    global $wpdb;
    global $current_user;
    $user = $current_user->ID;

    $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->users SET usrmsg = '%d' WHERE ID = '%d'", '0', $user )); }`

Here is my Ajax.

    `jQuery(document).ready(function($) {

$('.wordpress-ajax-form').on('submit', function(e) {
    e.preventDefault();
    var $form = $(this);
    $.post($form.attr('action'), $form.serialize(), function(data) {
    }, 'json');
}); });`

Here is my form

   `<form class="wordpress-ajax-form" method="post" action="<?php echo  AJAX_URL; ?>" ><input type="hidden" name="action" value="profile_msg">
<button id="msgbutton" >OK</button>

This code actually works fine, this issue I'm having is I want to add another form to do the same thing in MySQL but a different question. I tried changing the action name, and the "wordpress-ajax-form" in the js, but it seems to be firing the first form and now my new one. I will post my new one also.

Here is my other function

         `add_action( 'wp_ajax_remove_account', 'remove_account' );
         function remove_account() {
          global $wpdb;
         global $current_user;
         $id = ThemexUser::$data['active_user']['ID'];
         $table_views = $wpdb->prefix . "user_views";
         $table_favorites = $wpdb->prefix . "user_favorites";

         $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->users SET removed = '%d' WHERE ID = '%d'", '1', $id )); }`

Here is my other Ajax

     `<script>
    jQuery(document).ready(function($) {

$('.wordpress-ajax-form2').on('submit', function(e) {
    e.preventDefault();
    var $form = $(this);
    $.post($form.attr('action'), $form.serialize(), function(data) {
    }, 'json');
});  });

`

Here is my other form

   `<form class="wordpress-ajax-form2" method="post" action="<?php echo AJAX_URL; ?>">
  <input type="hidden" name="action" value="remove_account">
  <button class="msgbutton" >Remove Account</button> </form>`

The first form works and the second form doesn't work, I'm new to this so be gentle.

Thanks.

  • 写回答

1条回答 默认 最新

  • dougou8552 2016-11-16 22:44
    关注

    Any function that is invoked by the wp_ajax_ (whatever your function name is) hook must end with a wp_die(); or else the API will return a 0;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 unity3d机械臂
  • ¥20 判断两个表是否完全相同
  • ¥20 工控机出现散热器转一下停一下这种情况,是什么原因造成的
  • ¥15 双显卡,启动docker容器报错。
  • ¥15 想编写一个期货跨期套利的程序
  • ¥15 一个线程在sleep的时候set一个信号会起作用吗
  • ¥50 如何利用matlab将现有的地基雷达回波数据调制为机载雷达回波数据
  • ¥100 需求高精度PT100设计电路和算法
  • ¥15 单片机配网,继电器开关,广播
  • ¥60 Qcustomplot绘制实时动态曲线