i need help, i want to pass a value to javascript from php and it keeps giving me "null". i want to get what picture the user selected.

<?php
        
        mysql_connect('localhost','root','');
        mysql_select_db("ajax");
       $query="SELECT * FROM xxxx";
       $result= mysql_query($query);

       while($row=  mysql_fetch_array($result)){
           
         $rating=$row['ID'];
         
echo "<img src =\"" .$row['filepath']."\" /><br/><a id=".$row['ID']."  value='rating' onclick='getrating();'> likes". $row['likes']."<a/><br/>";
       }
       
   im trying to get what picture the user selected to rate. 
      
        ?>
          <script type="text/javascript" >
               
               
               function getrating(){
              var x= document.getElementById("<?php echo $row['ID']; ?>");
             
               
            alert(x)
          
             
  
               }
    </script>
while($row = mysql_fetch_array($result) {
    echo "<img src='".$row['filepath']."'>";
    echo "<br>";
    echo "<a id='".$row['id']."' value='".$row['id']."' onclick='getrating();'>Likes: ".$row['likes']."</a>";
    echo "<br>";
}
<script type="text/javascript" >                 
function getrating(){              
    var x = document.getElementById("<?php echo $row['id']; ?>").value;              
    alert(x)                  
}    
</script>

thanks for your reply, but when i click on the link of the rating, nothing appears. but when i take off value from

var x = document.getElementById("<?php echo $row['ID']; ?>");
    
alert(x)

it only alerts "null";

I think the problem is the while statement when performing the query. If im correct, you cannot get a value from an a attribute. Put in a hidden input field or just put the id in the function as below. If you use the input, take out r in the function and use the previous function that was provided, but i think that putting the id in the function name () and calling the function with a variable will work better.

Change:

while($row = mysql_fetch_array($result) {    echo "<img src='".$row['filepath']."'>";    echo "<br>";    echo "<a id='".$row['id']."' value='".$row['id']."' onclick='getrating();'>Likes: ".$row['likes']."</a>";    echo "<br>";}while($row = mysql_fetch_array($result) {
    echo "<img src='".$row['filepath']."'>";
    echo "<br>";
    echo "<a onclick='getrating('".$row['id']."');'>Likes: ".$row['likes']."</a>";
    //echo "<input type='hidden' id='".$row['id']."' value='".$row['id']."' />";
    echo "<br>";
}

If the onclick doesnt work, tak out the '' from the '".$row."'.
Then in our function:

function getrating(r) {
    document.getElementyById(r).value;
    alert(r);
}

What fobos said. Your programs are doing exactly what you programmed them to do. You are intermixing unrelated and otherwise unconnected execution environments; I still screw it up now and again after years of programming PHP, HTML and JS. Examine the HTML/JS on your browser; That's what runs on your browser, not your PHP program.

Think of PHP as a program generator. That is, it generates a static program (HTML/JS) that is sent to the browser to be executed. After the server has shipped the program to the browser, the server PHP program is done and no longer running. You have to generate your HTML/JS such that it has all the information it needs.

Next, by the time the PHP generates your getrating() function, $row is null--it must be, in order to exit the while loop above. So eliminate the loopy-loop complexity. Drop the ID and value and move the $row[] arg from getElementById to each and every getrating() function call. Tweak your code to be straightforward. You already know the row ID when you are generating the <img> tags, so call getrating() with the ID arg pre-set.

A simple task flow:

  1. Write a PHP program to generate an HTML/JS program.
  2. Verify that the PHP program is correct.
  3. If the generated HTML/JS is incorrect, reprogram your PHP code and go to step 2.

That is, keep writing and re-writing your PHP code until the generated HTML/JS is correct. Remember: you aren't just writing a program; you are writing a program that writes a program. Keep that extra level of abstraction in mind at all times.

Member Avatar for stbuchok

Just out of curiousity, why not use the following:

echo "<img src =\"" .$row['filepath']."\" /><br/><a id=".$row['ID']." onclick='getrating(this, 'rating');'> likes". $row['likes']."<a/><br/>";
function getrating(element, value){
    alert(value);
}

Also there is no value attribute for the anchor tag in HTML. If you no longer need the element and only the value, remove the "this" variable and element from the function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.