AJAX + PHP = Not Updating MySQL DB ... code inside

Discussion in 'JavaScript' started by scottatmu, Apr 2, 2010.

  1. #1
    The JAVASCRIPT I'm using:

    <script type="text/javascript">
    
      function GetXmlHttpObject(){
      		if (window.XMLHttpRequest){
      		  // code for IE7+, Firefox, Chrome, Opera, Safari
      		  return new XMLHttpRequest();
      		  }
      		if (window.ActiveXObject){
      		  // code for IE6, IE5
      		  return new ActiveXObject("Microsoft.XMLHTTP");
      		  }
      		return null;
      }
    
    
    	function rating(val){
    		ratequest=GetXmlHttpObject();
    		if (ratequest==null){
    		  alert ("Your browser does not support AJAX!");
    		  return;
    		  }
    		var url="rate_time.php";
    		url=url+"?id=" + val;
        url=url+"&sid="+Math.random();
    		ratequest.open("GET",url,true);
    		ratequest.onreadystatechange = handleRequestStateChange;
    		ratequest.send(null);
    	}
    
    </script>
    Code (markup):
    As you can see it calls a .php file called rate_time.php ... and that file is:
    
    <?php
    session_start();
    include_once("db.php");
    $id=mysql_real_escape_string($_GET['id']);
    mysql_query("INSERT INTO log SET rating='1', book_id='$id'");
    ?>
    Code (markup):
    db.php connects to the database.

    I'm using the following HTML to call it:

    <a href="#" onClick="rating(23)">1</a>
    Code (markup):
    However the issue is that I cannot get the script to insert the rating into the MySQL DB.

    I can run the rate_time.php file via the browser and it inserts everything, but just not via JS/AJAX.

    Anyone see any errors in the coding?
     
    Last edited: Apr 2, 2010
    scottatmu, Apr 2, 2010 IP
  2. elixiusx

    elixiusx Peon

    Messages:
    65
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #2
    hehehe use Jquery or some other javascript framework
    with jquery is only:

    function rating(val) {
    $.get('rate_time.php?id='+val);
    }

    :D
     
    elixiusx, Apr 8, 2011 IP