Am trying to run a javascript inside my php while loop but its not working, what i want is that when i click on the button with id check, it will hide that button and show the button with id change but its not working this is my code
<?php
$s = mysqli_query($conn, "SELECT * FROM test");
while($d = mysqli_fetch_array($s))
{
$id = $d['id'];
echo '
<button id="check'.$id.'" onclick="respond('.$n.');">check'.$id.'</button><br>
<button id="change'.$id.'" style="display:none;">change'.$id.'</button>
';
}
?>
<script>
function respond(username)
{
$("#check'"+<?php echo $id; ?>"'").hide();
$("#change'"+<?php echo $id; ?>"'").show();
$.post("ana.php",
{
username:username
});
}
</script>
Use the jquery
's click
function, and use javascript's this
to identify the selected row. You shouldn't have multiple functions doing the same thing with 1 static value being changed. Here's a rough implementation that I think will fix most of your issues:
<?php
$s = mysqli_query($conn, "SELECT * FROM test");
while($d = mysqli_fetch_array($s))
{
$id = $d['id'];
echo '<button class="respond" id="check'.$id.'">check'.$id.'</button><br>
<button id="change'.$id.'" style="display:none;">change'.$id.'</button>';
}
?>
<script>
$('.respond').click(function() {
var id = $(this).attr('id').replace('check', '');
$("#check" + id).hide();
$("#change" + id).show();
$.post("ana.php", {username:username});
});
</script>
Store whatever n
is as a value in the element and get it the same way I am getting your id
.