Reputation: 9293
What is wrong:
$('#btnSend').click(function()
{
var msg = $('#txtar').val(); // textarea
alert (msg); // works well, for example 'abc'
$.ajax(
{
type: "POST",
url: "write.php",
data: {msg: msg},
success: function(r)
{
alert(r); // doesn't work
}
});
});
write.php
:
$a = $_POST['msg'];
echo $a;
Instead of abc
I get the content of whole write.php
file in the alert.
Upvotes: 0
Views: 70
Reputation: 2013
Also here's a small example. Just tested this on an apache2.4 server with libapache2-mod-php5. If PHP code works otherwise though, don't worry about your server, or the config, just make sure you're including a JS library to make those AJAX requests work.
PHP(5)
<?php
if($_POST && isset($_POST['payload'])) {
echo "hello ".$_POST['payload'];
}
JS
$.ajax({
type: "POST",
url: "this.php",
data: {payload:"world"},
success: function(response){ alert(response); }
});
Upvotes: 1
Reputation: 272026
Seems like your write.php
file does not contain <?php
. Add it at the beginnig. The closing ?>
is optional.
Upvotes: 2
Reputation: 2466
Thats very simple...
write.php
$a = $_POST['msg'];
echo $a;
exit;
put exit or end here or else the code will move further unnecessarily.
Upvotes: 3
Reputation: 8074
Does write.php
contain opening and closing php tags:
<?php
$a = $_POST['msg'];
echo $a;
?>
Upvotes: 2