0

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>
4
Contributors
5
Replies
6
Views
5 Years
Discussion Span
Last Post by stbuchok
0
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>

Edited by fobos: n/a

0

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";

0

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);
}

Edited by fobos: n/a

0

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.

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.