0

Overview:

Here's what it looks like on html page.

enter image description here

So each row of checkboxes has it's different names, for example name="doctor_0[]", it increments every row. So the next row would be name="doctor_1[]" their corresponding default value would be 0

So in eye friendly code view it would be

// First row
<td><input type="checkbox" name="doctor_0[]" class="day_cbox" value="0"></td>
<td><input type="checkbox" name="doctor_0[]" class="day_cbox" value="0"></td>

// Second row
<td><input type="checkbox" name="doctor_1[]" class="day_cbox" value="0"></td>
<td><input type="checkbox" name="doctor_1[]" class="day_cbox" value="0"></td>

and so on...

Now into the problem

My problem is when the checkbox aren't checked, the data isn't passed into $_POST.

I know that normally it doesn't work that way, so the not-so-good alternative is to create input field of hidden so it would get the value when the checkbox aren't check. But the problem is, this one's an array.

I've tried several solutions that was told in my questions that are similar to this below, but it doesn't work that way though. Well it's not meant to be dynamic or using arrays. Correct me if I'm wrong, my bad about that.

So, are there any solutions for this stuff?

A help or suggestion or advice would be good.

Thanks.

Questions that are similar to this.

Get values of unchecked checkboxes

Post the checkboxes that are unchecked

How get value for unchecked checkbox in checkbox elements when form posted?

how to get the unchecked value from checkbox?

http://www.webmasterworld.com/php/4449836.htm

Community
  • 1
  • 1
Wesley Brian Lachenal
  • 4,381
  • 9
  • 48
  • 81
  • why do you even need to know the names of the unchecked ones? –  Sep 08 '14 at 02:32
  • "My problem is when the checkbox aren't checked, the data isn't passed into $_POST." then you wouldn't know if they were checked or not – Class Sep 08 '14 at 02:33
  • @Dagon I kind of need all the date if it's checked or not, to see if they choose the date / not. – Wesley Brian Lachenal Sep 08 '14 at 02:34
  • maybe better to use a radio buttons? – Class Sep 08 '14 at 02:34
  • @Class, `then you wouldn't know if they were checked or not` not quite sure on what you've said but, well if they checked the checkbox and submit it, the checked will only output the data not the unchecked ones. :( – Wesley Brian Lachenal Sep 08 '14 at 02:35
  • I've already thought of that radio button thing, but it doesn't apply to what I need though. – Wesley Brian Lachenal Sep 08 '14 at 02:35
  • you dynamical create the above ,you can use the same approach for knowing what was unchecked. all you need is a doctor count on the back end and a lst of the checked, to work out the unchecked –  Sep 08 '14 at 02:35
  • `if(isset($_POST['value'])){$value = "something";} else { $value = "something else";}` or use a ternary operator. – Funk Forty Niner Sep 08 '14 at 02:37
  • Is there a reason why you're not checking for the absence of the checkboxes for your logic? That is, essentially, what you are trying to do? – eluong Sep 08 '14 at 02:38
  • I've tried the ternary operator thing to check if the checkbox isn't checked or not. But the problem is, it's on array though. so if I `$_POST['doctor_0']`, it would only pass an array and would not be able to validate with it. Well in my knowledge have no idea on that. – Wesley Brian Lachenal Sep 08 '14 at 02:40
  • @bad_boy, hang on, will try on that. – Wesley Brian Lachenal Sep 08 '14 at 02:41
  • 1
    Glad I helped. And I don't know who told you that input of hidden type is a bad idea since most major frameworks handle unchecked values via this – Yang Sep 08 '14 at 02:48
  • 1
    be *very careful* with invalid html, it may be working today (on the browser and os you use), and fail on the next browser update or another browser. –  Sep 08 '14 at 02:49
  • @Dagon, I will, well I think it's fine on this one, since the php version that I'm working on and the company's browser version are so outdated. I'll take note of that. :) – Wesley Brian Lachenal Sep 08 '14 at 02:57

1 Answers1

0

Thanks to people who helped me in the comment section of my question.

Here's my final version of my code.

So I'm doing the usual while loop to retrieve data using mysql. Well don't get mad at me I was assigned to years old of web application for certain company.

Anyhow, I used @Bad_boy's logic to solve this issue.

The old code that I had is,

Note: $ctr is the increment thing-y per row.

echo '
    <td>
        <input type="checkbox" name="doctor_'. $ctr .'[]" class="day_cbox" value="1">
    </td>
';

And tada! I'm not good at explaning how it works but yeah, it worked! Hahahaha! Thanks!

while ($result = mysql_fetch_array($row)) {

    echo '
        <tr class="row-doctor">
            <td align="center">'. ucwords(strtolower($result['doctor_name'])) .'</td>
        ';

            for ($i = 1; $i <= 25; $i++) {

                echo '
                    <td>
                        <input type="hidden" name="doctor_'. $ctr .'['. $i .']" value="0">
                        <input type="checkbox" name="doctor_'. $ctr .'['. $i .']" class="day_cbox" value="1">
                    </td>
                ';
            }

    echo '
            <td align="center" class="blue strong"><span class="frequency_'. $ctr .'">0</span></td>
        </tr>
    ';

    $ctr++;
}
Wesley Brian Lachenal
  • 4,381
  • 9
  • 48
  • 81
  • 1
    This code still needs to be improved. You should prepare data somewhere level above, then iterated over prepared data in a template without `echoing 'WHOLE HTML MARK UP'`. And also I'm not a BAB_BOY – Yang Sep 08 '14 at 02:56
  • Will do, thanks for the advice. :) *edits* I just noticed it. HAHAHAHA! fixed your name. XD – Wesley Brian Lachenal Sep 08 '14 at 02:58