wordpress ajax表单提交不起作用?

时间:2016-07-23 07:30:49

标签: ajax database wordpress

这是代码..我认为动作我ajax不工作并得到403错误!使用Wamp并在其上运行Wordpress

js代码     jQuery的('#InsertionForm&#39)。提交(

function (){
var x = jQuery('#nm').val();
var y = jQuery('#em').val();

jQuery.ajax({
type:"POST",

url: "<?php echo admin_url('admin-ajax.php'); ?>",

data: { 
        action: 'get_my_option',
        namo: x, 
        emao: y
    },

success:function(){
    alert("working");
}
});

return false;

});

主要插件代码..请问有人能告诉我......给定的波纹管代码是否正确顺序?

    <?php

/*

Plugin Name: lucky

*/


//adding script files in HTML

wp_enqueue_script('myscript', plugins_url('myscript.js', __FILE__), 

array('jquery'), '', true);

wp_localize_script( 'myscript', 'my_ajax_object',

            array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );


function get_my_option()

{

    //do something

   alert("this is a function action: in ajax");

}

add_action("wp_ajax_nopriv_get_my_option", "get_my_option");

add_action("wp_ajax_get_my_option", "get_my_option");



    #Shortcode function.........................................

function html_form_code() {

    global $wpdb;

    # Input form fields.....

    echo '<form method="post" id="InsertionForm">';

    echo '<h3>Insertion Form</h3>';

    echo '<p>Your Name (required) <br/>';

    echo '<input id="nm" type="text" name="namel" value="" size="40" />';

    echo '</p>';

    echo '<p>';

    echo 'Your Email (required) <br/>';

    echo '<input id="em" type="email" name="emaill" value="" size="40" />';

    echo '</p>';

    echo '<p><input type="submit" name="submitbtn" value="Send"> ';

    echo '</form>';
}

function my_ajax() {

    $nom = $_POST['namo'];

    $emo = $_POST['emao'];

    global $wpdb;

    $wpdb -> insert( 'lucky_wpdb', array('namel' => $nom, 'emaill' => $emo) 

);

echo 'Submitted';

}



    //Shortcode working here....

add_shortcode( 'sitepoint_contact_form', 'html_form_code' );

?>

3 个答案:

答案 0 :(得分:0)

要使ajax操作正常工作,您需要为登录和未登录的用户定义单独的操作。

add_action( 'wp_ajax_my_action', 'my_action_callback' );
add_action( 'wp_ajax_nopriv_my_action', 'my_action_callback' );

其中wp_ajax_nopriv_表示未登录用户,wp_ajax_表示登录用户my_action也是您的实际操作。他们可以根据您的要求致电发送行动方式或不同

答案 1 :(得分:0)

为什么您的操作方法alert中有get_my_option

要在WP中使用ajax你应该回声并且它们会死掉;

试试这个

function get_my_option()

{    
    //do something

   echo "this is a function action: in ajax";
   die();    
}

答案 2 :(得分:0)

感谢大家帮助我...但我已经解决了这个问题.. 这是寻求答案的代码......它有效......

以下是插件的主要php文件...我被创建了..

    <?php
/*
Plugin Name: lucky
Plugin URI: 
Description: 
Version: 
Author: 
Author URI: 
License: 
License URI: 
*/


    #Ajax action function.......
function get_my_option() {  

    $nom = $_POST['namo'];
    $emo = $_POST['emao'];

    global $wpdb;

    $wpdb -> insert( 'lucky_wpdb', array('namel' => $nom, 'emaill' => $emo) );
    echo 'Submitted';

}


    #Shortcode function.........................................
function html_form_code() {

    # Input form fields.....
    echo '<form method="post" id="InsertionForm">';

    echo '<h3>Insertion Form</h3>';

    echo '<p>Your Name (required) <br/>';

    echo '<input id="nm" type="text" name="namel" value="" size="40" />';

    echo '</p>';

    echo '<p>';

    echo 'Your Email (required) <br/>';

    echo '<input id="em" type="email" name="emaill" value="" size="40" />';

    echo '</p>';

    echo '<p><input type="submit" name="submitbtn" value="Send"> ';

    echo '</form>';

}

//adding script files....

wp_enqueue_script('myscript', plugins_url('myscript.js', __FILE__), array('jquery'), '', true); // ajax script file...

wp_localize_script( 'myscript', 'ajax_object',
            array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) ); //including admin-ajax.php in website html

    //Shortcode working here....
add_shortcode( 'sitepoint_contact_form', 'html_form_code' );

?>

myscript.js代码......

// AJAX FORM SUBMITION CODE......


jQuery('#InsertionForm').submit(

function ajaxSubmit(){

var x = jQuery('#nm').val();
var y = jQuery('#em').val();

jQuery.ajax({
type:"POST",
url: ajax_object.ajax_url,
data: { 
        'action': 'get_my_option', //action calling to insert data in wordpress db...
        'namo': x, 
        'emao': y
    },
success:function($data){
    alert($data);

}
});

return false;

})

将此代码放在所选主题的functions.php中......

add_action("wp_ajax_nopriv_get_my_option", "get_my_option");
add_action("wp_ajax_get_my_option", "get_my_option");