php - Few issues in voting -
<?php include_once 'dbconnect.php'; include_once 'index_header.php'; $article_id = $_get['id']; $counter = 0; // select article db $sql = $con->query("select * posts post_id = '$article_id'"); if ($sql){ while($row = mysqli_fetch_assoc($sql)) { $select_title = $row['title']; $select_article = $row['article']; }} if(isset($_post['downvote'])){ $counter = $counter - 1; } if(isset($_post['upvote'])){ $counter = $counter + 1; if($counter<=1){ $insert_query = $con->query("insert votes(vote_count,post_id) values(1,'$article_id')"); } } $selet_votes = $con->query("select sum(vote_count) vote_count_sum votes post_id= '$article_id'"); if ($selet_votes){ $row_votes = mysqli_fetch_assoc($selet_votes); $votes = $row_votes['vote_count_sum']; } else { $votes = "0"; } if (empty($votes)){ $votes = "0"; } ?> <!doctype html> <html> <head> <script src="https://code.jquery.com/jquery-3.0.0.min.js"></script> <script type='text/javascript'> function switchimg() { if ($('#upvote').css('display') == 'none') { $('#upvote').css('display', 'block'); $('#upvote_clicked').css('display', 'none'); var hr = new xmlhttprequest(); // create variables need send our php file var url = "post.php"; var upvote = document.getelementbyid("upvote").value; hr.open("post", url, true); // set content type header information sending url encoded variables in request hr.setrequestheader("content-type", "application/x-www-form-urlencoded"); // access onreadystatechange event xmlhttprequest object hr.onreadystatechange = function() { if (hr.readystate == 4 && hr.status == 200) { var return_data = hr.responsetext; document.getelementbyid("counter").innerhtml = return_data; } } // send data php now... , wait response update status div hr.send(upvote); // execute request } else { $('#upvote').css('display', 'none'); $('#upvote_clicked').css('display', 'block'); } } </script> <script type='text/javascript'> function switchimgdown() { if ($('#downvote').css('display') == 'none') { $('#downvote').css('display', 'block'); $('#downvote_clicked').css('display', 'none'); } else { $('#downvote').css('display', 'none'); $('#downvote_clicked').css('display', 'block'); } } </script> <script type='text/javascript'> function switchimgdown() { if ($('#downvote').css('display') == 'none') { $('#downvote').css('display', 'block'); $('#downvote_clicked').css('display', 'none'); } else { $('#downvote').css('display', 'none'); $('#downvote_clicked').css('display', 'block'); } } </script> <title> <?php echo $select_title; ?> </title> </head> <body> <div id="rating"> <table> <tr> <div class="upvote-div"> <input type="image" name="upvote" id="upvote" value="1" onclick="switchimg()" src="images/up.png" /> <input type="image" name="upvote_clicked" id="upvote_clicked" value="-1" style="display:none" onclick="switchimg()" src="images/up_clicked.png" /> </div> </tr> <tr> <div id="counter"> <?php echo $votes; ?> </div> </tr> <tr> <div class="downvote-div"> <input type="image" name="downvote" id="downvote" value="-1" onclick="switchimgdown()" src="images/down.png" /> <input type="image" name="dowvote_clicked" id="downvote_clicked" value="1" style="display:none" onclick="switchimgdown()" src="images/down_clicked.png" /> </div> </tr> </table> </div> <div class="container"> <p> <h2><?php echo $select_title; ?></h2></p> <?php echo $select_article; ?> </div> </body> </html> <?php if (isset($_post['upvote'])){ $sql = $con->query("insert votes(vote_count,post_id) values (1,'$article_id')"); echo "thanks voting"; } ?>
here's post.php
have 2 issues:
the main 1 when click vote, undefined index $article id on line 6
. when wrote script, made ajax url vote.php
, when saw error, thought article_id
wasn't passed vote.php
put action on same page , strangely, still same problem.
my voting system stackoverflow, upvote, downvote , counter in middle, can't make ajax work. if post using forms, doesn't work.
your problem not passing correct parameters in ajax call. have this:
var upvote = document.getelementbyid("upvote").value; hr.send(upvote); // execute request
so sending number, not complete querystring parameters. need make that:
var parameters = "id=".$yourid."&upvote=true&downvote=false"; hr.send(parameters);
this post ajax call, , getting id get. need change method (in line 6):
$article_id = $_post['id'];
sending parameters post , reading $_post
can solve problem.
Comments
Post a Comment