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>

Link to comment
Share on other sites

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++
}
?>

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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++;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.