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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?