Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8398347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:02:01+00:00 2026-06-09T21:02:01+00:00

im quite new to php and javascript but still trying to get my webpage

  • 0

im quite new to php and javascript but still trying to get my webpage to work.
Im trying to add a 5-star-voting-system to the page which will contact my database and update accordingly.
As you might have noticed my code grammar isnt perfect either.

<div class="first"  onmouseover="starmove('-32px')" onmouseout="starnormal()" onclick="rate('harrys','1')">

this is the div that “contacts” the javascript

function rate(id,ratings){
var i=id;
var r=ratings;
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()
   {
xmlhttp.open("POST","http://------/rating.php?id="+i+"&rating="+r,true);
xmlhttp.send();
   }

this is the javascript

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Namnlös 1</title>
</head>
<body>
<?php
mysql_connect("host", "username", "password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());
mysql_set_charset('utf8');
$itemid = ($_GET["id"]);
$rating = ($_GET["rating"]);
$query = "INSERT INTO ratings(id,rating) VALUES ('$itemid','$rating')";
mysql_query($query);
?>
</body>
</html>

this is my rating.php file (note: the values in mysql_connect is right on actual page, even if not defined here.)

if I just open my php file there will be a row added to mysql so i know its connected properly.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T21:02:02+00:00Added an answer on June 9, 2026 at 9:02 pm

    You are passing the values to rating.php as GET but you are fetching it as POST

    xmlhttp.open("POST","http://------/rating.php?id="+i+"&rating="+r,true);
    

    Here, even if you give the method as POST, ?id="+i+"&rating="+r means that id and rating is being passed as GET.

    So you cannot access them as

    $itemid = ($_POST["id"]);
    $rating = ($_POST["rating"]);
    

    You will need to change that to:

    $itemid = ($_GET["id"]);
    $rating = ($_GET["rating"]);
    

    Also change your query to:

    $query = "INSERT INTO ratings(id,rating) VALUES ('".$itemid."','".$rating."')";
    

    EDIT:

    I have updated the Javascript code here. Try this:

    <script>
    function rate(id,ratings)
    {
        var i=id;
        var r=ratings;
        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.open("POST","http://------/rating.php?id="+i+"&rating="+r,true);
        xmlhttp.send();
    }
    </script>
    

    Changes:
    1. There were two braces after:

        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
       }
       }
    

    The second brace effectively closed the rate() function, so the code after that wasnt doing what it was expected to. So I moved that brace to the end of the code, to wrap the function.

    1. The onreadystatechange function is called AFTER data has been sent to the server in order to track, what is going on with the request. In your code, you were sending the request INSIDE the onreadystatechange, which would never execute, since the request would never get sent..

    I changed

      xmlhttp.onreadystatechange=function()
           {
             xmlhttp.open("POST","http://------/rating.php?id="+i+"&rating="+r,true);
             xmlhttp.send();
           }
    

    into a simple POST call:

     xmlhttp.open("POST","http://------/rating.php?id="+i+"&rating="+r,true);
     xmlhttp.send();
    

    I hope this explains everything 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to build a multi-page form with Yii, but am quite new
I am extremely new to PHP and, although I am quite familiar to javascript,
i'm quite new with php. i think this is quite straight forward but i'm
Being new to javascript, I'm way in over my head. But I'm trying :P
Context: I have to add an online payment system, on a quite old PHP
I'm quite new in PHP dev and I can't find a way to solve
I'm quite new to php and have been reading Larry Ullman book to develop
I'm quite new to PHP so I don't know how to solve this error.
This is probably something really simple, however I am quite new to PHP, and
I'm new to PHP, and don't have quite the grip on how it works.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.