Error in input [type = checkbox] During batch data modification in php

Source: Internet
Author: User
In php, a problem occurs when the input [typecheckbox] is used to modify data in batches. input [type = checkbox] I used js to convert the value 'on' into 1 and 0 for the convenience of input to the database.
$('input[type="checkbox"]').change(function(){    this.value ^= 1;});


The form is too complex to be pasted.
I also filled in the name field for all columns. []

The problem is that I can update the columns in batches except $ _ POST ["blank "].

The update order is incorrect.

The following is a detailed test. for example, five pieces of data on the page can be modified in batches.

1. ID1 ~ 5. set $ _ POST ["blank"] = 1 (tick)
Successful
2. ID1 ~ 5. set $ _ POST ["blank"] = 0 (not checked)
Successful

3. set ID 2 and 3 to $ _ POST ["blank"] = 1.

ID 1 and 2 will change to 1, but 3 will not change

4. try again... set ID 3, 4, 5 to $ _ POST ["blank"] = 1 (set all to 0 in advance)

As expected, the result is that ID 1, 2, 3 is changed to blank = 1, while ID4 5 is not changed.


In theory, PHP knows how many of my settings are set to blank = 1, Top of sortingSet to 1

Is the array pointer incorrect? But why can other columns be updated and modified very accurately and properly? Only this column cannot be modified?

The most annoying thing is that you know how to make an error, but you can't find the cause.



if (isset($_POST["edit_menu"])) {    $gid =$_POST["gid"];    $sql = "";    $menuCount = count($_POST["id"]);    for($i=0;$i<$menuCount;$i++) {        $title = $_POST["title"];        $url = $_POST["url"];        $id = $_POST["id"];        $blank = $_POST["blank"];        $sql .= "UPDATE `menu` set                         title='" . $title[$i] . "',                        url='" . $url[$i] . "',                        blank='" . $blank[$i] . "'                    WHERE id='" . $id[$i] . "' AND gid = '$gid';";    }    $stmt = $pdo->exec($sql);}


Reply to discussion (solution)

The checkbox that is not checked will not be submitted!
Php only knows how many checkboxes are selected, but does not know which ones are selected
Therefore, you need to specify the value of the checkbox to identify the selected checkbox, instead of converting it to 0 or 1.
Checkbox. value is usually the serial number or id of the standby option.

The checkbox that is not checked will not be submitted!
Php only knows how many checkboxes are selected, but does not know which ones are selected
Therefore, you need to specify the value of the checkbox to identify the selected checkbox, instead of converting it to 0 or 1.
Checkbox. value is usually the serial number or id of the standby option.


When I use checkbox to write SQL statements to this project (record), I want to set whether to open a new page

Because it is modified to the user in the list, it seems that only this option is available (with checkbox)

If you do not need 1/0, I receive On/'Null'

The result is the same as 1/0.

Is there any other solution?

Can it be solved on the client?
After the check, the target = "_ blank" is written into the corresponding link property,

$ ('Input [type = "checkbox"] '). click (function () {var link_id, checkbox_id; checkbox_id = $ this. id; // get the ID of the current check box and assign it to checkbox_id, convert it to the ID of the corresponding link, assign it to link_id, and then $ (link_id ). attr ("target", ($ (checkbox_id ). value = '1 ')? "_ Blank": "_ self ");});

Can it be solved on the client?
After the check, the target = "_ blank" is written into the corresponding link property,

$ ('Input [type = "checkbox"] '). click (function () {var link_id, checkbox_id; checkbox_id = $ this. id; // get the ID of the current check box and assign it to checkbox_id, convert it to the ID of the corresponding link, assign it to link_id, and then $ (link_id ). attr ("target", ($ (checkbox_id ). value = '1 ')? "_ Blank": "_ self ");});



I want to save _ blank to the database

Call to the foreground. this is a batch modification page in the background.

The problem is that no matter what the value of the checkbox is changed to, as long as it is not checked, HTML ($ _ POST) will not send any value to PHP for processing.

The nagging elder brother is right,
Check boxes are not used in this way,
The label will have a checked = "checked" attribute after it is selected instead of the selected value.

The value of the check box is used for locating. You can change the value of the check box to the same as the id.

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.