如何在Wordpress中将formdata从Js发送到PHP?

时间:2018-01-27 01:17:58

标签: javascript php jquery ajax wordpress

这是我的js代码。

 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);    

警报不起作用。 我确认html代码和表单提交链接没有任何问题。

wordpress functions.php如下。

  <?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代码不会重定向到functions.php。 你能告诉我为什么会遇到这个问题吗?

1 个答案:

答案 0 :(得分:0)

请点击此链接以正确使用ajax

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