-1

I am presently using php to display a column of numbered checkboxes, one for each day of a given month and next to each I display a text box to enter optional notes for a given checked day. I wish to restrict the user to entering notes only for days checked by either showing/hiding the text box based on the adjacent checkbox state or toggling its disabled attribute. I use a php loop to display form elements for each day and checkboxes are named boxes[], text boxes are named comments[]. I am aware that I can discard unused input before inserting records into the database, but I do not want to leave the user with the impression that they can place notes that will be retained on days that are not selected.

 while ($day_count <= $num_days){


 echo"<input name='boxes[]' type='checkbox' value='".$prov_id."_".$day_count."' />".$day_count."</input>";

   echo "Comment: <input name='comments[]' type='text' size='15' maxlength='255' />";
   echo "<br>";

$day_count++;   

}

  • 2
    What is the specific problem or question? – charlietfl Oct 20 '15 at 20:41
  • My best attempt so far does not yield text boxes which are disabled when the accompanying checkbox is not checked. The following is the last method attempted: – Matt W. Anderson Oct 20 '15 at 20:52
  • `while ($day_count <= $num_days){ // echo " "; echo" "; echo "
    "; $day_count++; }`
    – Matt W. Anderson Oct 20 '15 at 20:55

1 Answers1

0

You can do this in Javascript by adding a function that gets fired when your checkbox is checked. Take a look at this Stack Overflow answer for an example of how to do that: Javascript checkbox onChange

Once there you can change the display values of your comment boxes for this, though I'd recommend wrapping those comment boxes inside of a div and show/hide the entire div.

Since PHP is server side, you're not going to be able to make any changes on the page as the user checks and unchecks things in PHP, just Javascript

To make specific names you can use any discreet value you have, in your case: $prov_id, with the pseudocode below:

<div id='$prov_id'>Input text code here</div>

Then in your onchange method for the checkbox, pass in that id.

Lastly, for the javascript, just select the div with the id passed into the argument. $('#id')

Community
  • 1
  • 1
DaOgre
  • 2,080
  • 16
  • 25
  • I've looked at multiple examples and attempted to work them into my code but I run into roadblocks because I am attempting to work on elements named things like comments[]. – Matt W. Anderson Oct 20 '15 at 20:46
  • Is there a way to reference a div named with [] like the comment boxes? Or otherwise delimit which of the 90 text box divs I would have on a page are hidden and which are shown? The examples I have found pertain to single or non-generated elements, not elements which are populated via a loop, so it is unclear how one would confine the show/hide function to the exact desired div. – Matt W. Anderson Oct 20 '15 at 21:10
  • Updated the edit above, hopefully that's clear enough to get you on the path but if not please let me know – DaOgre Oct 20 '15 at 22:21