Jump to content

Disable/Enable checkboxes


CcXD

Recommended Posts

Hi everybody.

I come with a question that has been bothering me for a while, and i cant find a way around it.

 

This is what happens. I have 'n' ammount of input text fields, each on its own row.  Each text field is accompanied by a checkbox.  The boxes and the fields are created Dinamycally by the following code:

 

$result=mysql_query($query);
$numrows=mysql_numrows($result);

for($i=0; $i<$numrows; $i++){
echo"<tr>"
echo "<td><input type=\"text\" name=\"qty[]\" size=\"5\" maxlength=\"5\"></td>";
echo "<td><input type=\"checkbox\" name=\"selected_pn_id[]\" value=\"$pn_id\"></td>";
echo "</tr>";
}

 

I want to be able to disable the field that is located next to the specific checkbox, and erase whatever was inside this field. The thing is, im not sure how to identify the fields or the boxes individuallly

 

i tried something like :

<?php
//some code
echo "<td><input type=\"checkbox\" name=\"selected_pn_id[]\" value=\"$pn_id\" onclick=\"disableField();\"></td>";
//rest of code
?>

<script type="text/javascript">
function disableField(){
if(document.AddPCBParts.selected_pn_id.checked == true) 
{
document.AddPCBParts.qty.disabled = false ;
}
else 
{
document.AddPCBParts.qty.disabled = true ;
}
}
</script>

 

without any lluck. Any help?

Link to comment
Share on other sites

Going off of that code, your checkbox could reference its text input like this:

$result=mysql_query($query);
$numrows=mysql_numrows($result);

for($i=0; $i<$numrows; $i++){
echo"<tr>"
echo "<td><input type=\"text\" name=\"qty[]\" size=\"5\" maxlength=\"5\"></td>";
echo "<td><input type=\"checkbox\" name=\"selected_pn_id[]\" value=\"$pn_id\" onclick=\"this.parentNode.previousSibling.firstChild.disabled=true\"></td>";
echo "</tr>";
}

 

This is very specific to how your table is set up, though, so if you changed what items were in the cells, you might have to change the code, too.

Link to comment
Share on other sites

so what should i do if i want to write something like:

<?php
//some code
onclick="ClickBox()"
//rest of code
?>

<script>
function ClickBox(){
if(this.parentNode.previousSibling.firstChild.disabled==false){
   this.parentNode.previousSibling.firstChild.disabled=true;
}

else{
  this.parentNode.previousSibling.firstChild.disabled=false;
}
}
</script>

 

this is not working =(

Link to comment
Share on other sites

awww...

 

well i tried what u mentioned, and its not working.... i got the following code

 

<?php
//some code
echo "<td><input type=\"text\" name=\"qty[]\" id=\"$i\" size=\"5\" maxlength=\"5\" DISABLED></td>";
echo "<td><input type=\"checkbox\" name=\"selected_pn_id[]\" value=\"$pn_id\" id=\"$pn_id\" onclick=\"enableField($i,$pn_id);\"></td>";
//rest of code
?>

<script type="text/javascript">
function enableField(id,pn_id){
var qty = document.getElementById( 'id' );
var chk = document.getElementById( 'pn_id' );

chk.checked ? qty.disabled = false : qty.disabled = true; //enable if chk is checked, disabled if chk is unchecked
}
</script>

 

and its not working...  ??? ???

Link to comment
Share on other sites

Use IDs! Much easier IMO ;)

 

<script type="text/javascript">
function toggle ( i ) {
	var txt = document.getElementById( 'txt_'+i );
	var chk = document.getElementById( 'chk_'+i );
	if ( txt != null && chk != null ) {
		if ( chk.checked == true )
			{ txt.value = ''; txt.disabled = true; }
		else
			{ txt.disabled = false; }
	}
}
</script>
<?php

for( $i=0; $i<10; $i++ ) {
echo '<input type="text" name="qty[]" value="someval" id="txt_'.$i.'" /> ';
echo '<input type="checkbox" name="selected_pn_id[]" value="xx" id="chk_'.$i.'" onclick="toggle('.$i.')" /><br /><br />';
}

?>

Link to comment
Share on other sites

To enable it again, just include this javascript in the header:

<script type="text/javascript">
function doDisable(obj)
{
if (obj.checked)
	obj.parentNode.previousSibling.firstChild.disabled=true;
else
	obj.parentNode.previousSibling.firstChild.disabled=false;
}
</script>

 

Then call that function for the onclick event:

$result=mysql_query($query);
$numrows=mysql_numrows($result);

for($i=0; $i<$numrows; $i++){
echo"<tr>"
echo "<td><input type=\"text\" name=\"qty[]\" size=\"5\" maxlength=\"5\"></td>";
echo "<td><input type=\"checkbox\" name=\"selected_pn_id[]\" value=\"$pn_id\" onclick=\"doDisable(this)\"></td>";
echo "</tr>";
}

Link to comment
Share on other sites

I dunno why so many people have the hate-on for javascript. It's a great way to make sites more interactive. Personally, I don't care if the rare corporate/public computer user can't view it properly. They should probably be working anyways ;)

 

I agree. Honestly, if you're not an expert in JavaScript, you should still be able to create codes and look up functions and easily integrate them. Same thing in PHP, you should be able to think of the logic behind PHP, and look up functions, and put it all together. You don't need to be an expert to create good codes, just well-thought out logic.

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.