• PHP Help
  • Star Rating - SQL Not Inserting values

I'm passing a few variables into the jquery code below in a file called rating-ajax.js, which in turn is supposed to load rating-window-ajax.php and finish doing the sql insert. When completing a rating, it's calling the function and I'm getting to the 'data processing', but then doesn't execute the insert command and does not insert into my db. After confirming two javascript alert items, an element window appears within another element window but do not see any confirmation of insert.

The rating form_______________________

<?Php
echo "<br><br><div id='rating-box' style=\"text-align:center;border:1px solid red;width:550px;\">Please  rate this Article on one to five scale<br>";
echo "<form name=f1 action='' method=post>";
echo "<input type=hidden id=page_name name=page_name value='$page_name'> 
<input type=hidden name=todo id=todo value='submit-rating'>
<INPUT TYPE=RADIO NAME=rone Value=1 onClick='ajax_rating_Function(1)';><img src=/images/star.gif> <INPUT TYPE=RADIO NAME=rone Value=2 onClick='ajax_rating_Function(2)';><img src=/images/star.gif><img src=/images/star.gif> <INPUT TYPE=RADIO NAME=rone Value=3 onClick='ajax_rating_Function(3)';><img src=/images/star.gif><img src=/images/star.gif><img src=/images/star.gif> <INPUT TYPE=RADIO NAME=rone Value=4 onClick='ajax_rating_Function(4)';><img src=/images/star.gif><img src=/images/star.gif><img src=/images/star.gif><img src=/images/star.gif> <INPUT TYPE=RADIO NAME=rone Value=5 onClick='ajax_rating_Function(5)';><img src=/images/star.gif><img src=/images/star.gif><img src=/images/star.gif><img src=/images/star.gif><img src=/images/star.gif>";
echo" </div></form>";
?>

rating-ajax.js_____________________________________

<script>
function ajax_rating_Function(rating)
{
alert(rating);
var httpxml;
try
  {
  // Firefox, Opera 8.0+, Safari
  httpxml=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    httpxml=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      httpxml=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
function stateChanged() 
    {
    if(httpxml.readyState==4)
      {

document.getElementById("rating-box").innerHTML=httpxml.responseText;
document.getElementById("rating-box").style.borderColor='#00FF00 #0000FF';
document.getElementById("rating-box").style.display='inline';

  }
}
function getFormData() { 
var page_name=document.getElementById("page_name").value;
var sParam = "page_name="+page_name+"&rating=" + rating + "&todo=submit-rating";
return sParam;
} 


var url="rating-window-ajax.php";

var parameters=getFormData();
alert(parameters);
httpxml.onreadystatechange=stateChanged;
httpxml.open("POST", url, true)
httpxml.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
httpxml.send(parameters)  

document.getElementById("rating-box").style.background='#ffffcc';
document.getElementById("rating-box").offsetWidth='550px';
document.getElementById("rating-box").innerHTML="Data Processing ....";
document.getElementById("rating-box").style.display='inline';

}
</script>

rating-window-ajax.php__________________________

<?Php
require "config.php"; 
$todo=$_POST['todo'];
if(isset($todo) and $todo=="submit-rating"){
$rating=$_POST['rating'];
$page_name=$_POST['page_name'];
$msg="dadfadfad";
$status="OK";
if(!isset($rating)){$msg=$msg."Pleae give your score ";
$status="NOT OK";
}				


if ($status=="OK")
{
$sql=$dbo->prepare("insert into plus2net_rating (rating,page_name) values(:rating,:page_name)");
$sql->bindParam(':rating',$rating,PDO::PARAM_INT, 1);
$sql->bindParam(':page_name',$page_name,PDO::PARAM_STR, 100);

if($sql->execute()){
$rating_id=$dbo->lastInsertId(); 
echo " Thanks ..  id = $rating_id ";
}
else{
echo " Not able to add data please contact Admin ";
}

}else {
echo $msg;
}	
}// end of todo checking
?>

    Replaced the back-ticks (for in-line mono-spaced text) with [code]...[/code] tags for better code block formatting here. 😉

      Any takers? Once I select a star rating from the form, I'm anticipating results to appear in place of form. It's almost like the rating-window-ajax.php file isn't even loading. I just echoed the msg variable before any conditionals and I'm not seeing the output so I guess rating-window-ajax.php is not being loaded.

        Got it working, the call to the javascript required a '/' before the path, ...root relative.

        crawdidly Glad we could help. 🙂 Seriously, though, glad you were able to debug it. Personally I've been busy this weekend so didn't dive in to all that code (and since I'm mainly a server-side guy, probably wouldn't have seen that anyway.)

          Write a Reply...