1

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

7
  • I don't see the URL... Where are you sending your data ? Maybe this is the problem... Commented Aug 28, 2013 at 12:59
  • I hope u r still adapting to AJAX. This is completely wrong. you have to have a url provided.. Commented Aug 28, 2013 at 12:59
  • What does your console say? Commented Aug 28, 2013 at 12:59
  • 3
    Everyone... a blank URL means it submits to the same page, as OP wants. 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? Commented Aug 28, 2013 at 13:01

2 Answers 2

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
   }

?>

Sign up to request clarification or add additional context in comments.

2 Comments

Your missing a ) in the php isset
@웃웃웃웃웃 Can you not leave the url field empty?
2
jQuery.ajax({
   url: "./_FILE_" // ./index.php for example
   type: "POST",
   data: {name:'foo'},
   success: function(data)
   {
      alert('success');
   }
}); 

12 Comments

BUt is there a way to get the data on the same page itself ?
For this I will need to know how you call your page, the url of your form and what techno / FW you're using
i am not using a <form>. i use a table. with few input tags. and gather the data onclick event. i can store the data in javascript variable. but cannot send that data using ajax.
You should edit your post and add your HTML file plus the link of those, that would help me / us
<li> <fieldset> <label>Total Weight(KG) :</label> <input type="text" name="total_weight" id="total_weight" value="" /> </fieldset> </li> <li class="last"> <fieldset> <input type="button" name="button" value="Check" class="Check" onclick="calculate();" /> </fieldset> </li>
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.