2

I need the following functionality :

"When a checkbox is checked, the corresponding textbox must be highlighted and user could be able to enter some text into that.."

Checkboxes are working, but when a checkbox is checked, the corresponding textbox is not activating, how to solve this?

Here is my php code :

<?php
if ($handle = opendir('/year_no/albums')) {
    $blacklist = array('.', '..');
    while (false !== ($file = readdir($handle))) {
        if (!in_array($file, $blacklist)) {
            echo "<form name='form1'>
            <div class='controlset-pad'>
            <font color='black'><label><input type='checkbox' name='album[]' value='$album' onclick='enable_text(this.checked)' class='medium'/><span>$album</span></label><br/></div>​";
            echo"<script> 
            function enable_text(status)
            {
                status=!status;
                document.form1.other_name.disabled = status;
            }
            </script>";
            echo "<div class='field4'><label>Add your comment here</label><input type='text' name='other_name' class='medium' disabled='disabled'/></div></form>";
        }
    }
    closedir($handle);
}
?>

MODIFIED CODE:

<script> 
function enable_text(status, sub)
{
status=!status;
document.getElementById(sub).disabled = status;
}
</script>
<form name='form1'>
<?php
if ($handle = opendir('/year_no/albums')) {
    $blacklist = array('.', '..');
    while (false !== ($file = readdir($handle))) {
        if (!in_array($file, $blacklist)) {
echo "<div class='controlset-pad'>
<font color='black'><label><input type='checkbox' name='file[]' value='$file' onclick='enable_text(this.checked, $file)' class='medium'/><span>$file</span></label>
</div>";
echo "<div class='field4'><label>Add your comment here</label><input type='text' name='sub' id='$file' class='medium' disabled='disabled'/></div>";
        }
    }
    closedir($handle);
}
?>
</form>
13
  • Use heredocs or drop out of PHP mode to output large amounts of HTML. Commented Dec 25, 2012 at 7:18
  • The font tag is long deprecated. Use CSS instead. Commented Dec 25, 2012 at 7:19
  • Make sure to escape possibly unsafe strings, like $album, with a function like htmlentities to prevent accidental (or malicious) XSS. Commented Dec 25, 2012 at 7:21
  • Why would you print a function n times? You'll have to put the Javascript code in the header or footer of your page. Assign classes/ids to the checkboxes/textfields in your while loop, and write your JS code to use these ids/classes for doing stuff. Commented Dec 25, 2012 at 7:21
  • your code is also working on my system Commented Dec 25, 2012 at 7:30

3 Answers 3

6

Try This:

As you said, the below code has functionality that enables the textbox when checkbox is checked, and disables it again when it is unchecked.

Here is a working Example:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js">    </script>
<!-- include JQuery using above line -->

<!-- Here is your small javascript -->
<script type="text/javascript">
$(document).ready(function(e) {
    $("input[type=checkbox]").on("click",function(){
        if($(this).is(':checked')){
                    //If checkbox is checked do whatever you want to do here.
            $("#txt_"+$(this).attr("id")).removeAttr("disabled");
                    $("#drpdwn_"+$(this).attr("id")).removeAttr("disabled");
        } else {
                   // If unchecked.
            $("#txt_"+$(this).attr("id")).attr("disabled","disabled");
                    $("#drpdwn_"+$(this).attr("id")).attr("disabled","disabled");
        }
    });
});
</script>
<?php
$dir = "/year_no/albums";

if(is_dir($dir)) {
    if ($dh = opendir($dir)) {
        $blacklist = array('.', '..');
        $count=1;
        while (($file = readdir($dh)) !== false) {
            if (!in_array($file, $blacklist)){  
                echo "<div class='controlset-pad'>
                        <font color='black'>
                            <label>
                                <input type='checkbox' name='file[]' id='".$count."' value='enabletxtbox' class='medium'/>
                                <span>".$file."</span>
                            </label>
                    </div>";
                echo "<div class='field4'>
                        <label>Add your comment here</label>
                        <input type='text' name='sub' id='txt_".$count."' class='medium' disabled='disabled'/>";

                if(filetype($dir.$file) == "dir"){
                        $dir2 = $dir.$file;
                        $dh2=opendir($dir2);
                        $drpdwn="<select id='drpdwn_".$count."' disabled='disabled'>";
                        while(($file2 = readdir($dh2)) !== false){
                            if(!in_array($file2, $blacklist)){
                                $drpdwn.='<option>'.$file2.'</option>';
                            }
                        }
                        $drpdwn.="</select>";
                        closedir($dh2);
                        echo $drpdwn;

                }
                echo"</div>";
            }
            $count++;
        }
        closedir($dh);
    }
}
?>
Sign up to request clarification or add additional context in comments.

6 Comments

@highlander141 - pleasure ! NJoi !! :)
bro, i need some more info, can I get ur mail_id pls ?
thanks again, what I need actually is, check box is related to dir listing upto dir albums, later when a checkbox is chosen, in that associated textbox, a dropdown menu must appear which lists all the sub-dir's of chosen checkbox, can u plz guide me...
i am not getting you dude. what do you mean by in that associated textbox, a dropdown menu must appear??
when a checkbox is checked, in the place of activating textbox, we have to activate a dropdown list which contains the sub-dir of the chosen checkbox..
|
2

use this example

    <form name="form1">
    <script>
    function enable_text(status)
            {
            alert(status)
            status=!status;
            document.form1.other_name.disabled = status;
            }
    </script>
        <input type='checkbox' name='album' value='hi' onclick='enable_text(this.checked)' class='medium'/>
        <input type='text' name='other_name' class='medium' disabled='disabled'/>
    </form>

Comments

2

Your code is perfect , just add a small change as followed.

Instead of id

document.getElementById('other_name').disabled = status;

Note that id must be unique for each elements.

Edit use this code

<?php
if ($handle = opendir('/year_no/albums')) {
    $blacklist = array('.', '..');
    while (false !== ($file = readdir($handle))) {
        if (!in_array($file, $blacklist)) {
echo "<form name='form1'>
<div class='controlset-pad'>
<font color='black'><label><input type='checkbox' name='album[]' value='$album' onclick='enable_text(this.checked, $album)' class='medium'/><span>$album</span></label><br/>
</div>?";
echo"<script> 
function enable_text(status, album)
{
status=!status;
document.getElementById(album).disabled = status;
}
</script>";
echo "<div class='field4'><label>Add your comment here</label><input type='text' name='other_name' id = '$album' class='medium' disabled='disabled'/></div></form>";
        }
    }
    closedir($handle);
}
?>

Note that $album value can't be null

10 Comments

Thanks for your reply, can you show me in the code where to add?
inside function enable_text()
yes i had modified, but the same problem, when checkbox is checked, the textbox is not activated..
@ManishGoyal - He is repeating function enable_text() in while loop. You can't define functions with same name as it won't work. While loop is defining same function with same name.
@highlander141 - are you comfortable with JQuery?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.