javascript 如何在同一 php 页面上发送 Jquery Ajax 数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18488591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 12:07:50  来源:igfitidea点击:

How to Send Jquery Ajax data on Same php Page?

phpjavascriptjqueryajaxwordpress

提问by Harsh

hi i want to send javascript data via ajax to the same page in wordpress. Cannot fetch the data in testajax.php page. its shows Undefined index: name. And how to send the data on same php page rather than testajax.php ?

嗨,我想通过 ajax 将 javascript 数据发送到 wordpress 中的同一页面。无法获取 testajax.php 页面中的数据。它显示未定义的索引:名称。以及如何在同一个 php 页面而不是 testajax.php 上发送数据?

<script>
jQuery.ajax({
                type: "POST",
                url: 'http://localhost/projects/xxx/wordpress/wp-content/themes/xxx/testajax.php',
                data: {name:'foo'},
                success: function(data)
                {
                    alert('success');
                }
            }); 

回答by Deepu

var foo = 'somename';
jQuery.ajax({
    url:'yourUrl',
    type: "POST",
    data: {'name':foo},
    success: function(data)
        {
           alert('success');
        }
});

php

php

<?php
   if(isset($_POST['name'])){
      //Do whatever you want
   }else{
      //Do whatever you want
   }

?>

?>

回答by Nox

jQuery.ajax({
   url: "./_FILE_" // ./index.php for example
   type: "POST",
   data: {name:'foo'},
   success: function(data)
   {
      alert('success');
   }
});