I’ve such a situation , want to retrieve row ids from mysql to input hidden elements. let’s take a look at script
php =>
<?php
$con = new mysqli("host","user","pswd","db");
if (isset($_POST['some'])){
echo $_POST['some'];
}
echo "<form name='a' method='post' action='index.php'";
if (!$con->connect_error){
if ($con->set_charset("utf8")){
if ($r = $con->query("SELECT * FROM tb")){
while ($row = $r->fetch_assoc()){
echo "<input type='hidden' name='some' value='" . $row['id'] . "'><a href='javascript: void(0)' id='j'>" . $row['file_title'] . "</a><br>";
}
$r->free();
}
}
}
echo "</form>";
?>
and javascript
<script type="text/javascript">
document.getElementById("j").addEventListener("click",function(){
document.a.submit();
});
</script>
from this script I’m expect that when I’ll click to $row['file_title'] it must echo clicked row id but it get id only from first element and this id belongs to last row in database. what is wrong here , how can I solve this problem ? thanks
Here while loop is overwriting hidden element and link Ids every time.
See, Here every link has id ‘j’. Every input element has name ‘some’.
Better is to assign a class on this link and append $row[‘id’] in hidden element name in order to make elements unique like this :
Javascript will be something like this :
where ‘alink’ is the class assigned to the link.
Hope, it’ll help you..