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. But when I press submit nothing shows.
Below is my code
Ajax code (ajaxinsert.js file)
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
<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' );
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' ) ) );
}