如何在相同的PHP页面上发送Jquery Ajax数据?

时间:2013-08-28 12:56:46

标签: php javascript jquery ajax wordpress

嗨我想通过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');
                }
            }); 

2 个答案:

答案 0 :(得分:4)

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

PHP

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

&GT?;

答案 1 :(得分:2)

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