jquery - Data is not inserting to database but retrieving is working fine using PHP and Ajax? -
before ask question want clarify i'm rookie ajax , jquery, please spare me if doubt small or piece of cake, sorry that.
i'm trying create review system e-commerce using ajax , php. problem is, data not inserting in database, if insert the data manually in database displaying in site.i think there going wrong variable review or user_review couldn't find is.so, please tell me i've done mistake.
<div role="tabpanel" class="tab-pane" id="reviews"> <h4>write review</h4> <form action="" method="post" onsubmit="return post();"> <textarea id="review" class="reviewbox" placeholder="write review here....."></textarea> <button type="submit" class="btn">submit</button> </form> <div id="all_reviews"> <?php $query = $pdo->prepare("select * reviews 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 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 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 post review"; } $query = $pdo->prepare("select * reviews 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; } ?>
there mistake have done script code not closed
<script type="text/javascript" src="jquery.js">
to
<script type="text/javascript" src="jquery.js"></script>
that reason ajax not working.
Comments
Post a Comment