最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to Send Jquery Ajax data on Same php Page? - Stack Overflow

programmeradmin2浏览0评论

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 ?

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

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 ?

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

Share Improve this question edited Aug 28, 2013 at 13:05 Harsh asked Aug 28, 2013 at 12:56 HarshHarsh 111 gold badge1 silver badge5 bronze badges 7
  • I don't see the URL... Where are you sending your data ? Maybe this is the problem... – Mohamed Amine Commented Aug 28, 2013 at 12:59
  • I hope u r still adapting to AJAX. This is pletely wrong. you have to have a url provided.. – thecodejack Commented Aug 28, 2013 at 12:59
  • What does your console say? – tymeJV Commented Aug 28, 2013 at 12:59
  • 3 Everyone... a blank URL means it submits to the same page, as OP wants. – tymeJV Commented Aug 28, 2013 at 13:00
  • Please provide more code. Have you tried using Chrome's Developer Tools (F12) to check for Javascript syntax? – Christian Dechery Commented Aug 28, 2013 at 13:01
 |  Show 2 more ments

2 Answers 2

Reset to default 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
   }

?>

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

评论列表(0)

  1. 暂无评论