简体   繁体   English

在循环内使用Jquery检索HTMl元素的PHP变量Value

[英]Retrieve The PHP variable Value of a HTMl Element Using Jquery inside a Loop

I have a strange problem. 我有一个奇怪的问题。 I am getting the table values using PHP MySQL using a while loop and stored them in a array. 我正在使用while循环使用PHP MySQL获取表值,并将它们存储在数组中。 Then I am displaying it one after another in a single page. 然后,我在一个页面中一个接一个地显示它。

    $sql = "select * from newsfeed order by id DESC";
$result = $db->query($sql);

$posts_array = array();  // Storing the result to an Custom Array
$index = 0;

if($result->num_rows > 0)
{
    while($row = $result->fetch_assoc()) {

        $posts_array[$index] = $row;
        $index++; 
    }
}

I have successfully set the $post_id of the posts on to the HTML elements using the following code. 我已经使用以下代码成功将帖子的$ post_id设置到HTML元素上。

    <?php 
                $array_size = sizeof($posts_array);
                for($i = 0; $i < $array_size; $i++){
            ?>
            <div class='wrapper' id='wrapper'>
            <div class='user'>
<!-- Some Code -->
                <?php
                    $post_id = $posts_array[$i][id];
                 ?>
             <input type='image' name='like' id='like' width='40' height='40' src='like_btn.png' value='<?php echo ($post_id);'?> />

            <?php } ?>

Now I want to take that value using Jquery and do some Database Work. 现在,我想使用Jquery来获取该值并进行一些数据库工作。 I have accessed the value using the following code: 我已经使用以下代码访问了该值:

    <script type="text/javascript">
    function justsomething() {
        alert($(this).val());
    }
</script>

The problem is I am only getting the last post's id as alert() for every post rather than getting different id's . 问题是我只为每个帖子获取最后一个帖子的id作为alert(),而不是获取不同的id。 Please help me. 请帮我。 I am confused whether the PHP script is loading every time I click and call the justsomething() function. 每次单击并调用justsomething()函数时,PHP脚本是否正在加载,我感到困惑。

Note: I can display the $post_id of every post alone without any problem. 注意:我可以单独显示每个帖子的$ post_id,没有任何问题。

Beacuse the id should be unique to one html element try changing your code to this : 因为id对于一个html元素应该是唯一的,请尝试将代码更改为此:

<?php $array_size = sizeof($posts_array); for($i = 0; $i < $array_size; $i++){ ?>
<input type='image' name='like_<?php echo $i ?>' id='like_<?php echo $i ?>' width='40' height='40' src='like_btn.png' value='<?php echo $post_id;?>' onclick='justsomething(this);' />
<?php } ?>
<script>
function justsomething(el) {
    var img = el;
    alert(img.value);
}
</script>

here is an example 这是一个例子

 function justsomething(el) { var img = el; alert(img.value); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type='image' name='like_1' id='like_1' width='40' height='40' src='like_btn.png' value='15454' onclick='justsomething(this);' /> 

It works for loop, I hope You can make use of it 它适用于循环,希望您可以使用它

$("input[id=like]").each(function(i){ alert($(this).val()); }); $(“ input [id = like]”)。each(function(i){alert($(this).val());});

It will alert values from every <input id="like"... 它将警告每个<input id="like"...

Try this code: 试试这个代码:

onclick='justsomething(this)'

function justsomething(obj) {
        alert($(obj).val());
    }

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

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