处理对php的AJAX调用以插入到MySQL数据库中

时间:2015-11-17 作者:KondukterCRO

我在wp管理仪表板中创建了顶部菜单。在那里我将有输入字段。它将在管理员可以输入谷歌位置的地方输入。选择正确的位置后,管理员将按保存,位置将保存到位置表中的wordpress数据库中。目前,我创建文本输入字段只是为了测试它。我有:

 <form id="location_form" method="post" action="save-locations.php">
                    <table class="form-table">
                <tr valign="top">
                    <th scope="row">Enter location:</th>
                    <td>
                        <div class="container">
       <div class="row">
  <input type="text" id="username" name="username" placeholder="Username" autocomplete="off" class="required"/>
  </div>
               </td>
               </tr>
   <input id="submit" name="submit" type="submit" value="Send It" />
   </form>
        <div id="response"></div>
    </div>
比我有保存位置。php位于wp admin文件夹中,在那里我将sql插入db,并进行了验证。js我有aja电话:

  function ajaxSubmit() {
  //loading circle so user can see something is happening
    $(\'#submit\').attr(\'disabled\', \'disabled\');
 //collect data
 var username = $(\'#username\').val();
 //put data in one variable
var data = \'username=\' + username;

$.ajax({
    url: "save-location.php", //send it to php to handle data
    type: \'get\',
    dataType: \'json\',
    data: data,
    cache: false,
    success: function (response) {
        if (response.error != \'true\') {

            $(\'#response\').html(\'<h3>Records added successfully.</h3>\').fadeIn(\'slow\');


        }
        else {

            $(\'#response\').html(\'<h3>Could not able to execute sql</h3>\').fadeIn(\'slow\');
        }
    },
    error: function (jqXHR, textStatus, errorThrown) {

        $(\'#response\').text(\'Error Thrown: \' + errorThrown + \'<br>jqXHR: \' + jqXHR + \'<br>textStatus: \' + textStatus).show();
    }
 });
 return false;
}
但什么都没发生。我在哪里做的?

1 个回复
最合适的回答,由SO网友:Mateusz Hajdziony 整理而成

在WordPress中进行AJAX调用时,您应该始终发布到admin-ajax.php 文件下面是你如何做到这一点。

在插件中或functions.php:

function process_my_ajax_call() {
    // Do your processing here (save to database etc.)
    // All WP API functions are available for you here
}
// This will allow not logged in users to use the functionality
add_action( \'wp_admin_my_action_no_priv\', \'process_my_ajax_call\' );
// This will allow only logged in users to use the functionality
add_action( \'wp_admin_my_action\', \'process_my_ajax_call\' );
您还必须将正确的URL传递给admin-ajax.php 文件到您的JavaScript和wp_localize_script 功能非常强大,仅此而已:

function localize_my_scripts() {
    $localizations = array(
        \'ajaxUrl\' => admin_url( \'admin-ajax.php\' ),
    );

    wp_localize_script( \'my-script-handle\', \'myVars\', $localizations );
}
add_action( \'wp_enqueue_scripts\', \'localize_my_scripts\' );
现在在JavaScript中:

$.ajax({
    url: myVars.ajaxUrl, // Notice the AJAX URL here!
    type: \'post\',
    data: \'username=test&action=my_action\', // Notice the action name here! This is the basis on which WP calls your process_my_ajax_call() function.
    cache: false,
    success: function ( response ) {
        // Parse ajax response here
        // Do something with the response
    },
    error: function ( response ) {
        // Handle error response here
    };
});
当然,这只是对如何在WordPress中进行AJAX调用的一个巨大总结。您仍然需要验证nonce,使用WP\\u AJAX\\u response类创建适当的AJAX响应,然后将WP AJAX响应脚本排入前端以处理WP的响应。如果你想了解更多,有一个comprehensive guide to AJAX in WordPress written by Ronald Huereca 而且它是免费的!

相关推荐

自定义模板earch.php中的搜索结果

现在我在搜索结果页面(search.php)中遇到了一个问题,结果是由索引页面模板显示的,而不是它的模板,它显示所有帖子,不管搜索者是否输入了内容我的索引。php页面<?php get_header(); ?> <?php $search = $_get[\'search\']; $args = array( \'post_title_like\' => $search ); $res = new wp_query($args