Jump to content

how to loop javascript inside the mysql_fetch_array


claro

Recommended Posts

greetings! I have here codes that have two checkboxes, if 1 checkbox is checked, the other and textbox is disabled. I think my codes in js is functional, my problem is how to put it inside the mysql_fetch_array, I want to put those checkbox in every data fetch from my db. only the first data is affected ..i really appreciate your help guys!

 

<?php  include('connect-db.php');?>

<html>

<head>

<script language = "Javascript" type="text/javascript" >

function checkbox_disabled(tocheck,todisable,todisable2)

{

  var x = document.getElementById(tocheck);

  if(x.checked){

      document.getElementById(todisable).disabled=true;

      document.getElementById(todisable2).disabled=true;

  document.getElementById(todisable2).checked = false;

     

     

  }else{

      document.getElementById(todisable).disabled=false;

      document.getElementById(todisable2).disabled=false;

  document.getElementById(todisable2).checked = true;

    }

  }

</script>

</head>

<body>

<?php

echo "<table>";

 

$query = mysql_query("SELECT * FROM tbl_user")

or die (mysql_error());

while ($row = mysql_fetch_array($query))

{

echo '<tr>';

echo '<td>'.$row['user_Id'].'</td>';

echo '<td>'. $row['user_Fname'].'</td>';

echo '<td>'.$row['user_Lname'].'</td>';

echo "<td><input type='checkbox' id='checkbox1' value ='checked' onclick=checkbox_disabled('checkbox1','textbox1','checkbox2')></td>";

echo '<td><input type="checkbox" id = "checkbox2"><input type="text" id="textbox1" /><td>';

echo '</tr>';

 

}

echo "</table>";

?>

 

 

 

 

 

</body>

</html>

I'm really not sure what you mean, but it sounds like you just need to escape your quotes properly and add a little incremental variable to keep changing the checkbox names... Try this: (untested)

 

<?php
$i = 1;
while ($row = mysql_fetch_array($query)){
   echo "<tr>";
   echo "<td>".$row['user_Id']."</td>";
   echo "<td>". $row['user_Fname']."</td>";
   echo "<td>".$row['user_Lname']."</td>";
   echo "<td><input type='checkbox' id='checkbox".$i."' value ='checked' onclick='checkbox_disabled(\'checkbox".$i."\',\'textbox".$i."\',\'checkbox".$i."2\');'></td>";
   echo "<td><input type='checkbox' id='checkbox".$i."2'><input type='text' id='textbox".$i."' /><td>";
   echo "</tr>";
   $i++
}
?>

thank you for your help!! but i still it does'nt work.

 

attached is my expected output. It did work to my first data record, my problem is it doesnt work to other.

 

<?php

echo "<table>";

 

$query = mysql_query("SELECT * FROM tbl_user")

or die (mysql_error());

while ($row = mysql_fetch_array($query))

{

echo '<tr>';

echo '<td>'.$row['user_Id'].'</td>';

 

echo "<td><input type='checkbox' id='checkbox1' value ='checked' onclick=checkbox_disabled('checkbox1','textbox1','checkbox2')></td>";

echo '<td><input type="checkbox" id = "checkbox2"><input type="text" id="textbox1" /><td>';

echo '</tr>';

 

}

echo "</table>";

?>

 

[attachment deleted by admin]

try this: (changed quotes to double in javascript call. Should work now)

 

echo "<tr>";
   echo "<td>".$row['user_Id']."</td>";
   echo "<td>". $row['user_Fname']."</td>";
   echo "<td>".$row['user_Lname']."</td>";
   echo "<td><input type='checkbox' id='checkbox".$i."' value ='checked' onclick='checkbox_disabled(\"checkbox".$i."\",\"textbox".$i."\",\"checkbox".$i."2\");'></td>";
   echo "<td><input type='checkbox' id='checkbox".$i."2'><input type='text' id='textbox".$i."' /><td>";
   echo "</tr>";
   $i++;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.