WordPress中的AJAX表单提交无法正常工作(conti ...)

时间:2016-12-01 08:06:25

标签: javascript php jquery ajax wordpress

这是我的问题here的延续。我有自定义页面模板,表单有2个下拉列表和1个输入提交。当用户提交表单时,将显示结果,这是正常的。但是我遇到了问题,在数据库中获取数据时会重新加载浏览器。

我正在尝试使用AJAX来阻止刷新浏览器。我刚刚更新了functions.php和JS中的AJAX函数。当用户单击提交按钮时,页面现在不刷新。但结果未显示。我正在使用is_page,在特定页面中显示不同的结果

如何解决这个问题?如何在AJAX中调用自定义PHP文件?

AJAX(functions.php)

function ajax_enqueue_sl(){
    wp_enqueue_script('ajax_load_sl', get_template_directory_uri().'/js/alternate-dropdown.js', array('jquery'));
    wp_localize_script('ajax_load_sl', 'ajax_object', array('ajax_url' => admin_url( 'admin-ajax.php')));
}
    add_action('wp_enqueue_scripts', ajax_enqueue_sl);

add_action('init', 'registerFormAction');
function registerFormAction(){
    // To handle the form data we will have to register ajax action. 
    add_action('wp_ajax_nopriv_submitAjaxForm','submitAjaxForm_callback');
    add_action('wp_ajax_submitAjaxForm','submitAjaxForm_callback');
}

function submitAjaxForm_callback(){
    global $wpdb;
    if(is_page(9208)){
        //ROCK ON!
        if (isset($_POST['store_list']) && $_POST['store_list'] != 'Select by Store'){
        $store_list = $_POST['store_list'];
        $stores= $wpdb->get_results($wpdb->prepare("SELECT street FROM tablename WHERE stores= '" . $store_list . "' AND col IN ('test') ORDER BY street ASC", OBJECT));
        foreach ($stores as $record_s){
            echo '<div class="records">';
            echo '<div><span class="icons-tabbed-store icon-icon-stores">' . $record_s->street . '</span></div>';
            echo '</div>';
        }
    } elseif (isset($_POST['mall_list']) && $_POST['mall_list'] != 'Select by Mall'){
        $mall_list = $_POST['mall_list'];
        $street = $wpdb->get_results($wpdb->prepare("SELECT stores FROM tablename WHERE street= '" . $street_list . "' AND col IN ('test') ORDER BY stores ASC", OBJECT));
        foreach ($street as $record_m){
            echo '<div class="records">';
            echo '<div><span class="icons-tabbed-store icon-icon-stores">' . $record_m->stores . '</span></div>';
            echo '</div>';
        }
    }
    }
    wp_die();
}

PHP w / HTML(test.php)

$results_street = $wpdb->get_results('SELECT DISTINCT street FROM tablename WHERE code IN ("test1") ORDER BY street ASC', OBJECT);
$results_stores = $wpdb->get_results('SELECT DISTINCT stores FROM tablename WHERE code IN ("test2") ORDER BY stores ASC', OBJECT);

<form action='' method='post' name='myform' id="myform">
<div class="pos-div">
<select name="street_list" id="filterbystreet">
    <option name="default" class="filter_by" value="Select by">Select by</option>
    <?php
    foreach($results_street as $option){
        if(isset($_POST['street_list']) && $_POST['street_list'] == $option->street)
            echo '<option name="street_list" class="filter_by" selected value="'. $option->street .'">'. $option->street .'</option>';
        else    
         echo '<option name="street_list" class="filter_by" value="'. $option->street .'">'. $option->street .'</option>';
     };
    ?>
</select>
</div>
<span class="or">or</span>
<div class="pos-div">
<select name="store_list" id="filterby">
    <option name="default" class="filter_by" value="Select by">Select by</option>
    <?php 
    foreach($results_stores as $option){
        if(isset($_POST['store_list']) && $_POST['store_list'] == $option->stores)
            echo '<option name="store_list" class="filter_by" selected value="'. $option->stores .'">'. $option->stores .'</option>';
        else    
         echo '<option name="store_list" class="filter_by" value="'. $option->stores .'">'. $option->stores .'</option>';
     };
    ?>
</select>
</div>
<input type="submit" value="List all partner stores" class="pos-submit"/>
</form>

if (isset($_POST['store_list']) && $_POST['store_list'] != 'Select by Store'){
        $store_list = $_POST['store_list'];
        $stores= $wpdb->get_results($wpdb->prepare("SELECT street FROM tablename WHERE stores= '" . $store_list . "' AND code IN ('test3') ORDER BY street ASC", OBJECT));
        foreach ($stores as $record_s){
            echo '<div class="records">';
            echo '<div><span>' . $record_s->street. '</span></div>';
            echo '</div>';
        }
    } elseif (isset($_POST['street_list']) && $_POST['street_list'] != 'Select by'){
    $street_list = $_POST['street_list'];
    $streets = $wpdb->get_results($wpdb->prepare("SELECT street FROM tablename WHERE street_list= '" . $street_list. "' AND code IN ('test3') ORDER BY stores ASC", OBJECT));
    foreach ($streets as $record_m){
        echo '<div class="records">';
        echo '<div><span>' . $record_m->stores . '</span></div>';
        echo '</div>';
    }
}

ajax_js.js

    jQuery(document).ready(function($){
    //$('#myform').submit(ajaxFormSubmit);

    $('#filterbystreet').change(function(){
        $('#filterbystore').prop('selectedIndex','Select by Store');
    });

    $('#filterbystore').change(function(){
        $('#filterbystreet').prop('selectedIndex','Select by');
    });

    jQuery('.pos-submit').on('click',function(e){ 
        e.preventDefault(); 
        var myform = jQuery('#myform').serialize(); 

        jQuery.ajax({ 
            type:"POST", 
            // Get the admin ajax url which we have passed through wp_localize_script(). 
            url: ajax_object.ajax_url, 
            action: "submitAjaxForm", 
            data: myform, 
            success:function(data){ 
                jQuery(".records").html(data); 
                console.log(data);
            } 
        }); 
    });
});

1 个答案:

答案 0 :(得分:1)

首先在wp_enqueue_scripts函数和wp_localize_script,你应该注册然后在函数中排队句柄,作为codex中的例子

  // Register the script
  wp_register_script( 'ajax_load_sl', get_template_directory_uri().'/js/alternate-dropdown.js', array('jquery'));

  wp_localize_script('ajax_load_sl', 'ajax_object', array('ajax_url' => admin_url( 'admin-ajax.php'))););

  // Enqueued script with localized data.
  wp_enqueue_script( 'ajax_load_sl');

详情和示例here

在你的php脚本中,你没有提到它是否代表一个函数(我想是这样,并假设这个),在函数的顶部,你需要添加

global $wpdb;

然后你可以使用$wpdb

tablename,永远不会被识别,您需要将wp_posts放在wp_作为您的表前缀的地方,或$wpdb->prefix.'posts'如果您不想对前缀负责。

global $wpdb;

$results_street = $wpdb->get_results('SELECT DISTINCT street FROM wp_posts WHERE code IN ("test1") ORDER BY street ASC', OBJECT);
$results_stores = $wpdb->get_results('SELECT DISTINCT stores FROM wp_posts WHERE code IN ("test2") ORDER BY stores ASC', OBJECT);

使用正确的数据库名称更改wp_posts。

告诉我它是否有效!