Note that there are some explanatory texts on larger screens.

plurals
  1. POImproving a star rating system
    text
    copied!<p>I made a star rating system for a school project and need some help improving it a bit. It's a fairly simple setup: index.php contains list-items (content is fetched from a DB) with an image and a div holding the stars for rating. Each star is a link that triggers a function which saves the rating in a database.</p> <p>Here's <a href="http://kristianmaar.dk/projekter/s3p2/v3/" rel="nofollow">the link</a>! for starters.</p> <p>If you click on any star, the first click will result in a green check mark on the first list-item. The idea is that this check mark will appear on each list-item when rated. That's where I need you guys to point me in the right direction. First of all I know I can't echo out multiple divs with the same id, but I had to in order for the ajax function to work (document.getElementById("rated")). Any ideas on how to solve this?</p> <p><strong>CODE</strong></p> <p>insert_receive.php:</p> <pre><code>&lt;?php session_start(); $sess = session_id(); $mysqli = new mysqli("", "", "", ""); if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } $stmt = $mysqli-&gt;prepare("REPLACE INTO Ratings (projId_fkey, sessionId, rvalue) VALUES ('".$_GET['projId']."','".$sess."','".$_GET['rating']."')"); $stmt-&gt;execute(); printf("✔"); ?&gt; </code></pre> <p>ajax_framework.js:</p> <pre><code>function saveClick(rating,projId) { var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 &amp;&amp; xmlhttp.status==200) { document.getElementById("rated").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","insert_receive.php?rating=" + rating + "&amp;projId=" + projId,true); xmlhttp.send(""); } </code></pre> <p>index.php: (the part that matters)</p> <pre><code>&lt;?php $select = "SELECT id, projName, location FROM Projects"; if($result = $mysqli-&gt;query($select)) { while($row = $result-&gt;fetch_assoc()) { echo '&lt;li id="'.$row['id'].'"&gt;'; echo '&lt;h1 class="header"&gt;'.$row['projName']."&lt;/h1&gt;"; echo '&lt;img src="'.$row['location'].'" alt=""/&gt;'; echo '&lt;div class="rating"&gt;'; echo '&lt;a href="#" onclick="saveClick(5, '.$row['id'].')"&gt;'.★★★★★."&lt;/a&gt;"; echo '&lt;a href="#" onclick="saveClick(4, '.$row['id'].')"&gt;'.★★★★."&lt;/a&gt;"; echo '&lt;a href="#" onclick="saveClick(3, '.$row['id'].')"&gt;'.★★★."&lt;/a&gt;"; echo '&lt;a href="#" onclick="saveClick(2, '.$row['id'].')"&gt;'.★★."&lt;/a&gt;"; echo '&lt;a href="#" onclick="saveClick(1, '.$row['id'].')"&gt;'.★."&lt;/a&gt;"; echo '&lt;div id="rated"&gt;'.""."&lt;/div&gt;"; echo "&lt;/div&gt;"; echo "&lt;/li&gt;"; } } ?&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload