简体   繁体   English

如何修复 WordPress 插件对表单处理程序文件的 ajax 请求。 (我自己正在开发一个插件)

[英]How to fix ajax request to the form handler file from the WordPress plugin. (I am developing a plugin myself)

everyone!I'm creating WordPress plugin.大家!我正在创建 WordPress 插件。 The main goal of this plugin is creating form, and after user will fill out this form data from the form will be written to the database.这个插件的主要目标是创建表单,在用户填写这个表单后,表单中的数据将被写入数据库。 I made the part of the plugin that is responsible for creating the database and displaying the form using a shortcode.我制作了负责创建数据库和使用短代码显示表单的插件的一部分。 But faced with a problem.但是遇到了问题。 How to send data from a form to a database using the ajax method.如何使用 ajax 方法将数据从表单发送到数据库。

After activating the plugin, a table is created in the database:激活插件后,在数据库中创建一个表:

if ( ! function_exists ( 'jal_install' ) ) {
function jal_install (){
    global $wpdb;
    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    $table_name = $wpdb->prefix . "liveshoutbox";
    $charset_collate = $wpdb->get_charset_collate();
        if($wpdb->get_var("SHOW TABLES LIKE '{$table_name}'") !== $table_name){
            $sql = "CREATE TABLE $table_name (
            id mediumint(9) NOT NULL AUTO_INCREMENT,

            client_name tinytext NOT NULL,
            client_mail varchar(55) DEFAULT '' NOT NULL,

            PRIMARY KEY  (id)
            ) $charset_collate;";
        dbDelta( $sql );
    }   
  }
}

The form on the page is displayed with a shortcode:页面上的表单显示一个短代码:

function form_shortcode() {
        ob_start(); ?>

      <form class="main_form" method="POST" action="">
            <input type="text" name="user_name">
            <input type="text" name="user_mail">
            <button type="submit">Subnit</button>
      </form>
      <div class="main_form__success">Greate Worck!</div>
         
    <?php 
      return ob_get_clean();
    }

    add_shortcode('form-shortcode', 'form_shortcode');

For adding data from the form to the database, there is a file: form_action.php I placed it in the main plugin directory.为了将表单中的数据添加到数据库中,有一个文件:form_action.php 我把它放在主插件目录中。 And for initiating the work of this file I use the form_ajax.js为了启动这个文件的工作,我使用了 form_ajax.js

$("form.main_form").submit(function() {
      var th = $(this);
      $.ajax({
        type: "POST",
        url: "/form_action.php",
      }).done(function() {
        $(th).find('.main_form__success').addClass('active').css('display', 'flex').hide().fadeIn();
        setTimeout(function() {
          $(th).find('.main_form__success').removeClass('active').fadeOut();
          th.trigger("reset");
        }, 3000);
      });
      return false;
    });

As a result, I cannot get to this form_action.php file.结果,我无法访问这个 form_action.php 文件。 And I have an error.我有一个错误。 The code tries to find it in the root of the site, but not in the root of the plugin.代码试图在站点的根目录中找到它,而不是在插件的根目录中。 I do not understand how to fix it, tell me how to do it please.我不明白如何解决它,请告诉我如何做。

this is a basic syntax for ajax jquery request这是 ajax jquery 请求的基本语法

here , in the data field , you have to specify your form using id ,name or tag在这里,在数据字段中,您必须使用 id、name 或 tag 指定表单

$.ajax({
  url: "/../form_action.php",
  type: "POST",
  data:$('form').serialize(),
  success: function (data) {
    .....
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM