How to Send Jquery Ajax data on Same php Page?

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

Related posts

Leave a Reply

2 comments

  1. 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
       }
    

    ?>