简体   繁体   English

Ajax在表单提交时未返回任何内容

[英]Ajax not returning anything on form submit

I am working in wordpress and on pressing the submit button I want the function to return result through ajax and result should be shown as an alert on the screen. 我正在使用wordpress并按下提交按钮,我希望该函数通过ajax返回结果,并且结果应在屏幕上显示为警报。 But when I press submit nothing shows. 但是当我按下提交时,什么也没显示。

Below is my code 下面是我的代码

Ajax code (ajaxinsert.js file) Ajax代码(ajaxinsert.js文件)

jQuery(document).ready(function(){

////////////////////////////////////////////////////////////
    jQuery("#addimage").submit(function (e) { //form is intercepted
        e.preventDefault();
        //serialize the form which contains secretcode
        var sentdataa = $(this).serializeArray();

        //Add the additional param to the data        
        sentdataa.push({
            name: 'action',
            value: 'wp_up'
        })

        //set sentdata as the data to be sent
        jQuery.post(yess.ajaxurl, sentdataa, function (rez) { //start of funciton
            alert(rez);

            return false;
        } //end of function
        ,
        'html'); //set the dataType as json, so you will get the parsed data in the callback
    }); // submit end here
});

HTML Form HTML表格

<form id="addimage" action="" method="post" enctype="multipart/form-data">
<input type="submit" name="upload" style="margin-bottom:15px;">
</form>

PHP code (I have used shortcode on the page for this code and below code is in functions.php file) add_shortcode( 'test', 'addimage' ); PHP代码(我在该代码页上使用了简码,下面的代码在functions.php文件中)add_shortcode('test','addimage');

function wp_up()
{   
echo "zeeshanaslamdurrani";
exit();

}

function addimage(){  

add_action( 'wp_ajax_wp_up', 'wp_up' );
add_action( 'wp_ajax_nopriv_wp_up', 'wp_up');


// register & enqueue a javascript file called globals.js
wp_register_script( 'globalss', get_stylesheet_directory_uri() . "/js/ajaxinsert.js", array( 'jquery' ) ); 
wp_enqueue_script( 'globalss' );

// use wp_localize_script to pass PHP variables into javascript
wp_localize_script( 'globalss', 'yess', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
} 

Give it a try after putting the below mentioned hooks outside the add_image() function : 将下面提到的钩子放在add_image()函数之外后,尝试一下:

add_action( 'wp_ajax_wp_up', 'wp_up' );
add_action( 'wp_ajax_nopriv_wp_up', 'wp_up');

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

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