Before I ask you my question I want to clarify that I'm just a rookie to Ajax and Jquery, so please spare me if the doubt is very small or a piece of cake, sorry for that.
I'm trying to create review system for my E-Commerce using Ajax and PHP. The problem is, the data is not inserting in to the database, but if I insert the the data manually in the database it displaying perfectly in my site.I think there is something going wrong with the variable review or user_review but couldn't find what it is.So, could you please tell me where I've done the mistake.
<div role="tabpanel" class="tab-pane" id="reviews">
<h4>Write your Review</h4>
<form action="" method="post" onsubmit="return post();">
<textarea id="review" class="reviewbox" placeholder="Write Your Review Here....."></textarea>
<button type="submit" class="btn">Submit</button>
</form>
<div id="all_reviews">
<?php
$query = $pdo->prepare("SELECT * FROM reviews WHERE product_id=?");
$query -> bindValue(1, $id);
$query->execute();
while ($row = $query->fetch(PDO::FETCH_ASSOC))
{
$name = $row['user_name'];
$text = $row['review_text'];
$time = $row['post_time'];
?>
<h5>By <?php echo $name; ?></h5>
<p><i>posted on <?php echo $time; ?></i></p>
<p>
<?php echo $text; ?>
</p>
<hr>
<?php
}
?>
</div>
<script type="text/javascript" src="jquery.js">
< script type = "text/javascript" >
function post() {
var review = document.getElementById("review").value;
if (review) {
$.ajax({
type: 'POST',
url: 'post_reviews.php',
data: {
user_review: review
},
success: function(response) {
document.getElementById("all_reviews").innerHTML = response + document.getElementById("all_reviews").innerHTML;
document.getElementById("review").value = "";
}
});
}
return false;
}
</script>
</div>
This is my post_reviews.php
:
<?php
session_start();
require('includes/product.php');
require('includes/connect.php');
$product = new Product;
if(isset ($_GET['id'])) {
$id = $_GET['id'];
$data = $product -> fetch_data($id);
if(isset($_POST['user_review'])){
$review=$_POST['user_review'];
if (isset($_SESSION['logged_in'])) {
$query = $pdo -> prepare("INSERT INTO reviews(product_id,user_name,review_text) VALUES (?,?,?)");
$query -> bindValue(1, $id);
$query -> bindValue(2, $_SESSION['name']);
$query -> bindValue(3,$review);
$query ->execute();
}
else{
$review_msg="Please login to post your review";
}
$query = $pdo->prepare("SELECT * FROM reviews WHERE product_id=?");
$query -> bindValue(1, $id);
$query->execute();
while ($row = $query->fetch(PDO::FETCH_ASSOC)){
$name = $row['user_name'];
$text = $row['review_text'];
$time = $row['post_time'];
?>
<?php if(isset($review_msg)){ ?>
<small style = "color : #aa0000"; ><?php echo $review_msg ?></small>
<br><br>
<?php } ?>
<h5>By <?php echo $name; ?></h5>
<p><i>posted on <?php echo $time; ?></i></p>
<p><?php echo $text; ?></p>
<hr>
<?php
}
}
exit;
}
?>