I am trying to communicate back and forth with the server with the help of jQuery.
This is my simple scenario:
- Send data through html form using jQuery post method.
- Receive data on the server side end echo it back again.
- Receive same data on the client side in the form of jQuery callback and do something with data (console.log in the broser)
Now my problem is that php is returning source html code with tags, instead of data that has been received ? So if I type "Hello" in the form field on the client side, php is returning whole html source from the page. Why is that ?
Html:
<form action="#">
<textarea name="content" id="content" rows="8" cols="40"></textarea>
<p><button>Click to submit</button></p>
</form>
jquery:
(function(){
$("form").on("submit",function(e){
$.post("save.php",$(this).serialize(),function(data){
alert(data);
})
e.preventDefault();
})
}());
PHP:
<!DOCTYPE HTML>
<html charset="utf-8">
<head>
<title>Index</title>
<link rel="stylesheet" href="path-to-stylesheet.css" type="text/css"/>
</head>
<body>
<?php
echo $_POST['content'];
?>
<script src=""></script>
</body>
</html>