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
2 Answers
Reset to default 4var 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');
}
});