【问题标题】:Wordpress Submit a form using AJAXWordpress 使用 AJAX 提交表单
【发布时间】:2013-03-07 14:08:31
【问题描述】:

我创建了一个 Wordpress 插件,以允许我的客户创建能够回复和支付活动费用的活动。我对某些代码需要放置在哪里以及如何将表单提交到位于插件文件夹函数文件中的函数感到困惑。

此时它返回一个空白警报。

我在单个活动页面上显示此表单:

<div id="rsvpContent">  
  <form id="Attendevent" action="">
   <input id="attend" name="attend" type="checkbox" value="yes" /><label for="attent">Yes, I plan to attend.</label>
 </form>

然后,我把这个放到了通用的 header.php 文件中:

 $("#attend").click(function(){
    var form = $("#Attendevent").serialize();

    $.ajax({
      type:'POST',
      data:{"action":"Attending","data": form },
      url: "<?php bloginfo('url'); ?>/wp-admin/admin-ajax.php",
      beforeSend: function(){
        alert(form);
      },
      success: function(data) {
            alert(data);
      }

    }); 

 });

然后,我把这个函数放到插件文件夹内的主函数页面中:

function eventAttent() {
$wpdb->show_errors();
            if($_POST['attend'] == 'yes') {
                $wpdb->insert('wp_event_attendants',
                array( 'event_id' => get_the_ID(),'user_id' => $current_user->ID));
 echo $wpdb->print_error();
                }
            }


 add_action('wp_ajax_Attending', 'eventAttend');
 add_action('wp_ajax_nopriv_Attending', 'eventAttend');

当用户点击“参加”复选框时,如何调用此函数?

【问题讨论】:

    标签: php jquery ajax wordpress


    【解决方案1】:

    link 是一个很好的 ajaxifying 主题来源。

    将此代码放入您的子主题的functions.php中。

    // enqueue your custom js       
     function my_js_method() {
        // for jquery ui
           wp_enqueue_style( 'jqueryUI_style', get_stylesheet_directory_uri() . '/customCSS/jquery-ui.css' );
            wp_enqueue_script(
                'handleFormAjax-script',
                get_stylesheet_directory_uri() . '/handleFormAjax.js',
                array( 'jquery' )
            );
        // add the appropriate hook
        add_action( 'wp_enqueue_scripts', 'my_scripts_method' );
    
    
        // declare the URL to the file that handles the AJAX request (wp-admin/admin-ajax.php)
        wp_localize_script( 'handleFormAjax-script', 'MyAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
        }
    
    
        // AJAX handler snippet
        function myHandlerMethod(){
          if (is_admin())
        echo "woow working like a charm";
        die();
        }
        //add the hooks for your handler function
        add_action('wp_ajax_myHandlerMethod', 'myHandlerMethod');
        // for users who are not logged in
        add_action('wp_ajax_nopriv_myHandlerMethod', 'myHandlerMethod');
    

    除此之外,您的表单应如下所示。

    <form id="assetForm" action= "/wp-admin/admin-ajax.php" method="post">
        <input type="hidden" name="action" value="myFunction"/>
        <button id="sbmBtn">SUBMIT </button>
    </form>
    

    最后,你的js函数应该处理你在functions.php中定义的处理函数

    var data = jQuery("#myForm :input").serializeArray();
    
        jQuery.post(jQuery("#myForm").attr("action"),data, function(info) {
            // success code ;
                    });
    

    【讨论】:

      【解决方案2】:

      尝试使用此代码。这是我测试过的代码。和我一起工作得很好..但它不会处理文件上传。

      HTML

      <form id="form_name" name="form_name" >
      <span id='errfrmMsg' style='margin:0 auto;'></span>
        //form attributes just as input boxe.
        <input type="hidden" name="action" value="anyName" />
        <input id="submit_button" onclick="submit_form();" type="button" value="Send" />
      </form>
      

      j 查询

      function submit_form()
      {
        jQuery.post(the_ajax_anyName.ajaxurl_anyName,
         jQuery("#form_name").serialize(),
           function(response_from_the_action_function){
                      jQuery("#errfrmMsg").html(response_from_the_action_function).css({"display":"block"});
            }
         );
      }
      

      functions.php 中的 PHP 代码

      wp_register_script("ajax-anyName", get_bloginfo('template_directory')."/js/custom_js.js", array( "jquery"));
      wp_enqueue_script('ajax-anyName');
      wp_localize_script("ajax-anyName","the_ajax_anyName", array("ajaxurl_anyName" => admin_url("admin-ajax.php")));
      // add actions
      add_action( "wp_ajax_anyName", "ajax_action_anyName" );
      add_action( "wp_ajax_nopriv_anyName", "ajax_action_anyName" );
      
      function ajax_action_anyName(){ 
        //Form processing code in PHP
      
      
      }
      

      【讨论】:

        猜你喜欢
        • 2013-09-17
        • 2017-09-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-19
        • 2012-11-16
        相关资源
        最近更新 更多