admin管理员组

文章数量:1122826

I want to save my form data via jquery but i can't able to access it in my script.Can anyone help me. URL access in script: url: 'admin.php?page=insert.php',

My script

$(function () {
        $('form').on('submit', function (e) {
          e.preventDefault();
          var schema_key = $("#schema_key").val(); 
          $.ajax({
            type: 'post',
            url: 'admin.php?page=insert.php',
            data: schema_key,
            success: function () {
              alert('form was submitted');
            }
          });

        });

      });

I want to save my form data via jquery but i can't able to access it in my script.Can anyone help me. URL access in script: url: 'admin.php?page=insert.php',

My script

$(function () {
        $('form').on('submit', function (e) {
          e.preventDefault();
          var schema_key = $("#schema_key").val(); 
          $.ajax({
            type: 'post',
            url: 'admin.php?page=insert.php',
            data: schema_key,
            success: function () {
              alert('form was submitted');
            }
          });

        });

      });
Share Improve this question asked Mar 21, 2017 at 6:56 shpwebhostshpwebhost 115 bronze badges 4
  • do u want to use insert.php file only? As that is not the correct way to use AJAX in WP. – Aftab Commented Mar 21, 2017 at 7:13
  • Then how can i insert form value without page refresh – shpwebhost Commented Mar 21, 2017 at 7:21
  • Ok. I am adding an Answer for how to use AJAX in WP. Please check – Aftab Commented Mar 21, 2017 at 7:23
  • You should use the REST API for AJAX, don't make AJAX requests to specific files or pages – Tom J Nowell Commented Aug 24, 2021 at 16:49
Add a comment  | 

2 Answers 2

Reset to default 0

I think you must add all the url , because if your script file is not located in the main page it will not access this url: 'admin.php?page=insert.php', tray to add the full url , url: 'domain.com.../admin.php?page=insert.php'

$(function () {
    var site_url = window.location.hostname; // www.example.com
    $('form').on('submit', function (e) {
      e.preventDefault();
      var data = {
            action : 'insert',
            schema_key : $("#schema_key").val(),
      }; // adding action
      $.ajax({
        type: 'post',
        url: site_url+'/wp-admin/admin-ajax.php',
        data: data,
        success: function () {
          alert('form was submitted');
        }
      }); // end of ajax

   }); // end of form submit

}); // end of $function

If you check the data that I am sending to ajax call there is one "action" parameter, and this action is responsible for defining a function for inserting the detail in the database.

Now you need to define a hook and callback function in your active theme's functions.php file or your plugin file.

Syntax will be : add_action( 'wp_ajax_$action', 'callback_function' );

So in your case it will be :

function wp_insert_schema_key() {
    $schema_key = $_POST["schema_key"];
    // Your code for inserting/updating in DB.
    echo "Success"; // response to be sent from AJAX call
    die;

}
add_action( 'wp_ajax_insert', 'wp_insert_schema_key' );

For more information you can check - https://codex.wordpress.org/AJAX_in_Plugins

本文标签: How to get my insertphp url in jquery