Reputation: 17
I'm sending request by $.ajax method from test.php to ajax.php page. The console shows "200 ok" that means the request is ok. But return nothing, although the console doesn't show any error. My used pages are as following:
test.php
<div id="return-data">
<ul class="return-lists">
</ul>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$('.submit-form').click(function() {
var name = "tanvir";
var address = "Dhaka";
var cData = "name=" + encodeURIComponent(name) + "&address=" + encodeURIComponent(address);
$.ajax({
url: "ajax.php",
type: "POST",
data: cData,
success: function(data)
{
alert(data);
$('#return-data .return-lists').append(data);
}
});
});
</script>
And ajax.php
<?php
if( isset($_POST['name']) ) {
$name = $_POST['name'];
$address = $_POST['address'];
$lists = '';
$lists .= '<li>' . $name . '</li>';
$lists .= '<li>' . $address . '</li>';
return $lists;
exit;
}
I also have tried by removing if( isset($_POST['name']) ) {}
and by using echo instead of return in ajax.php
Upvotes: 0
Views: 387
Reputation: 4953
Try this one. Change return $list for echo $list and no need to use exit at the end of ajax.php
<?php
if( isset($_POST['name']) ) {
$name = $_POST['name'];
$address = $_POST['address'];
$lists = '';
$lists .= '<li>' . $name . '</li>';
$lists .= '<li>' . $address . '</li>';
echo $lists;
}
Upvotes: 1
Reputation: 17
Yeah, I have solved the problem by normally putting "echo" instead of "return". Because the "return" return an string and echo print an string. Thanks to all.
Upvotes: 0