Please try this:
Add the following in your theme’s functions.php
add_action( 'init', 'tnc_aj_scripts' );
function tnc_aj_scripts() {
wp_register_script( "ajax_anmeldung_script", get_template_directory_uri(). '/my-ajax.js', array('jquery'), '1.0', true);
wp_localize_script( 'ajax_anmeldung_script', 'mYAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'ajax_anmeldung_script' );
}
add_action("wp_ajax_ajax_anmeldung", "ajax_anmeldung");
add_action("wp_ajax_nopriv_ajax_anmeldung", "ajax_anmeldung");
function ajax_anmeldung() {
$result['one'] = 'Hey';
$result = json_encode($result);
echo $result;
die();
}
Create a .js file in your theme directory called my-ajax.js and put the following inside that file:
jQuery('#teilnahme_form').submit(function(e){
e.preventDefault();
var name = document.getElementById("name").value;
jQuery.ajax({
type:"post",
dataType : "json",
url : mYAjax.ajaxurl,
data: {
action: "ajax_anmeldung",
name : name
},
success:function(data){
alert(data.one);
}
});
});