1

I'm trying to "POST" data from my Form through ajax to a mysql database. This code is hooked to the "after_setup_theme" action:

add_action("wp_ajax_ajax_anmeldung", "ajax_anmeldung");
add_action("wp_ajax_nopriv_ajax_anmeldung", "ajax_anmeldung"); 

function ajax_anmeldung() {
    echo "<script>
        alert('hey');
        </script>";
    die();
}

And this is the JS code that calls it:

$('#teilnahme_form').submit(function(e){
    e.preventDefault();

    var name = document.getElementById("name").value;

    jQuery.ajax({
       type:"POST",
       url: window.ajaxurl,
       data: {
           action: "ajax_anmeldung",
           name : name
       },
       success:function(data){
           alert(data);
       }
     });
});

When I run this I get returned a "0", nothing more.

When I tried: http://my-domain.com/wp-admin/admin-ajax.php?action=ajax_anmeldung I also get a "0".

I read all the others post on here surrounding this topic, but nothing seemed to help me, or I overlooked something really important.

Cheers

2
  • You don't need to echo hey on your script tag, only echo 'hey';
    – Benoti
    Commented Mar 25, 2017 at 16:20
  • Thanks, but still get the same result Commented Mar 25, 2017 at 18:24

2 Answers 2

1

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);
      }
   });
});
4
  • I get "undefined" as a response. That probably means my code isn't executed, no? Commented Mar 26, 2017 at 11:04
  • Sorry, I missed a line initially, Edited answer now, please try if it works. Commented Mar 26, 2017 at 12:49
  • Still undefined. I think my actions don't work. Im declare them in the functions.php file. Is that not correct? Commented Mar 26, 2017 at 13:51
  • Probably you're doing something wrong somewhere. Updated answer again with full code. Please try this Commented Mar 26, 2017 at 14:58
0

When using "POST" as request type, exclude "action" in data param and append it to the url.

$('#teilnahme_form').submit(function(e){
    e.preventDefault();

    var name = document.getElementById("name").value;

    jQuery.ajax({
       type:"POST",
       url: window.ajaxurl + '?action=ajax_anmeldung',
       data: {
         name : name
       },
       success:function(data){
         alert(data);
       }
    });
});
3
  • Sadly this didn't work for me. I also tried to combine your @Abdul Awals answer, which didn't work either. I still get "0" or "undefined" when it's combined. Commented Mar 26, 2017 at 11:05
  • Oh I see why it didn't work. Remove e.preventDefault(); and see what happens -: Commented Mar 26, 2017 at 11:59
  • Still nothing, sadly. Commented Mar 26, 2017 at 13:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.