简体   繁体   English

将值从PHP传递到JavaScript

[英]Passing values from PHP to JavaScript

I'm trying to get the value which is the id in the mysql database. 我正在尝试获取的值是mysql数据库中的id。 However, each time I click on the image, I get null . 但是,每次单击图像时,都会得到null I used this to get the value but it is not working as it keeps giving me null in the alert box. 我用this来获取值,但由于它在警报框中始终为我提供null值,因此无法正常工作。

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <?php
        mysql_connect('localhost','root','');
        mysql_select_db("ajax");
        $query="SELECT * FROM xxxx";
        $result= mysql_query($query);

        while($row=  mysql_fetch_array($result)){
            echo "<img src='".$row['filepath']."' value='".$row['ID']."' id='".$row['ID']."'   onclick='getrating(this.value);'>";

            echo "<br>";
       }
    ?>

    <script type="text/javascript" >
        function getrating(row_id){
            var x = document.getElementById(row_id);
            alert(x);
        }
    </script>  
</body>
</html>

What is the problem? 问题是什么?

You need getrating(this.id) instead. 您需要使用getrating(this.id)代替。 Images don't have a value property. 图片没有value属性。

尝试这个:

 echo "<img src='".$row['filepath']."' id='".$row['ID']."' onclick='getrating(".$row['ID'].");'>";

Or you can pass this.id 或者您可以通过this.id

<img id="row_12" onclick="getrating(this.id)" alt="image"/>



function getrating(id){
    alert(id);
}

Or you can use the event object and the currentTarget propety 或者您可以使用事件对象和currentTarget属性

<img id="row_12" onclick="getrating(event)" alt="image"/>



function getrating(e){
    alert(e.currentTarget.id);
}

They way how you escape the ID could be the problem. 他们用这种方式来逃避ID可能是问题。 I know this is already answered but just in case for those people who needs another solution. 我知道已经解决了这个问题,但以防万一,这些人需要其他解决方案。

onclick="getrating(\''.$row['ID'].'\')"

value isn't a valid attribute of the img tag. value不是img标签的有效属性。 You could use the id , or just do 您可以使用id ,也可以

echo "<img ... onclick='getrating($row[ID]);'>";

An <img> doesn't have a value property. <img>没有value属性。

You are doing unnecessary work in your function too. 您也正在功能中执行不必要的工作。 Your code should look like this:- 您的代码应如下所示:

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <?php
        mysql_connect('localhost','root','');
        mysql_select_db("ajax");
        $query="SELECT * FROM xxxx";
        $result= mysql_query($query);

        while($row=  mysql_fetch_array($result)){
            echo "<img src='".$row['filepath']."' value='".$row['ID']."' id='".$row['ID']."'   onclick='getrating(this);'>";

            echo "<br>";
       }
    ?>

    <script type="text/javascript" >
        function getrating(element){
            alert(element);
        }
    </script>  
</body>
</html>

By passing this to your function through the onclick event, you already have the element you are looking for without needing to use document.getElementById() . 通过传递this给你的函数通过onclick事件,你已经拥有你所寻找的元素,而无需使用document.getElementById()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM