I am trying to update the comments.json by using one of tutorial example of Comment app of React.js.
I have done very few changes in code :
handleCommentSubmit: function(comment){
var comments = this.state.data;
var newComments = comments.concat([comment]);
this.setState({data: newComments});
$.ajax({
url: 'test.php',
dataType: 'json',
type: 'POST',
data: comment,
success: function(data) {
this.setState({data: data});
}.bind(this),
error: function(xhr, status, err) {
console.error(this.props.url, status, err.toString());
}.bind(this)
});
},
Code of Test.php as folllows:
<?php
$json = $_POST['json'];
echo $json;
if (json_decode($json) != null) { /* sanity check */
$file = fopen('comments.json','w+');
fwrite($file, $json);
fclose($file);
} else {
echo "error found";
}
?>
Any help will be good for me, because I am very new to JavaScript.