As soon as I came in, I saw two forms related to the problem, in fact, I am also directed at the form of the problem.

Source: Internet
Author: User
As soon as I came in, I saw two form problems, but I was also looking for a form problem.
Have the following data sheet

ID----name----show
1-----AA------1
2-----BB------0
4-----DD------0
----FF------1


The operation form of the output after reading is as follows:
AA
BB
DD
FF



Program Purpose: After submission, according to check the status of Reset all the fields in the show value, such as AA unchecked, after committing to set it to 0, and DD is checked, after committing to set it to 1
But here is a problem, post the type array only with tick elements, such as the AA's show is 1, the output controller in the output will default to Checked= "checked", which is checked, this time I want to set the AA show to 0, so close the check, and uncheck the DD itself (want to set the DD Show to 1), submit the form, the type array is as follows:
Array
(
[0] = 4
[1] = 33
)

That is, the data only came out of the tick two elements, the No. 0 element is actually DD, the 1th element is the FF, the front of the AA,BB did not tick to get this array,
will be based on the value of the array to find the corresponding ID of the record set to 1, but did not come to set to 0, how to do?

===============
Another scenario, the name value of the form is named with an ID, not an array, i.e. the following form (this is my stupid method, with a hidden record the maximum ID value)
AA
BB
DD
FF


After the submission, the check is submitted, I use the CI framework, so to get.
$MAXID = Maximum ID obtained from the database;
$types = Array ();
for ($i = 1; $i <= $maxid; $i + +) {
$types [$i] = ($this->input->post (' type '. $i) ==false)? 0:1;
If not checked, such as post (' type2 ') will return false, because type2 this form of the table is not transmitted at all. And the pass is checked, so the non-false will return 1
}

The problem is that the for loop adds many non-existent discontinuity IDs, and of course the workaround is to get the ID group foreach ID values from the array that exist.
Is there any other way? In the end, I actually wanted an array like this: The tick state of all the options in the form is described. Now I can only get to the checked, not checked to actually do not pass the variable come over I was dizzy dead.

------Solution--------------------
Only the selected checkbox will be submitted the first one is reasonable
------Solution--------------------
You can attach the name value of all the checkboxes to a string, such as: "Type1,type2 ...", upload it to the background, PHP with explode to split it, the resulting array and the received post key name to take the difference set, the resulting is not checked
------Solution--------------------
In fact, the two approaches are similar:

The first type: PHP prepares an array, array (1, 2, 4, 33); Traverse this array to check if Array[i] is within $_post[' type '), you can use Isset or In_array.

The second type: the same as the first one. Just used a bit of string concatenation. Array (1, 2, 4, 33), traversing the array, checking array[i] for the existence of $_post[' type '. $array [i]]
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.