简体   繁体   中英

How to send formdata from Js to PHP in Wordpress?

This is my js code.

 var form = document.forms[0];
 form.addEventListener('submit', function(event) {

event.preventDefault();
this.submit.disabled = true;
alert('hey');
$.ajax({
dataType: "text",
url: 'https://matchipet.com/wp-admin/admin-ajax.php',
data: {
    'action': 'text_ajax_test',
    'text_test': text,
    'secure': '<?php echo wp_create_nonce('text_test_ajax ') ?>'
}
 }); 
  }, false);    

alert does not work. I confirmed that html code and form submit link do not have any problems.

wordpress functions.php is below.

  <?php 
    function text_ajax_test(){
    check_ajax_referer('text_test_ajax','secure');
    if(isset($_REQUEST)){
     echo 'heyheyheye' ;
       exit();
     }
   }
   add_action('wp_ajax_text_ajax_test','text_ajax_test');
    add_action('wp_ajax_nopriv_text_ajax_test','text_ajax_test');
     ?>

Js code does not redirect to functions.php. Could you tell me why I have this problem?

Please follow this link to for correct use of ajax

https://webkul.com/blog/using-ajax-wordpress/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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