How can I obtain the selected checkbox value?

Source: Internet
Author: User
How can I obtain the selected checkbox value? This is the front-end page, where "department" and "deputy name" are the data obtained by jquery from another page, and the returned result is the following checkbox. I tried it. I couldn't find these checkboxes from this page using js, so I only used php, but I couldn't do it. Below is some code: the user is the name of the checkbox.

$ User = $ _ POST ['user'];
$ Arr = array ();
$ Darr = array ();
$ Darr = array_diff ($ arr, $ user );
If (! Empty ($ user )){
For ($ I = 0; $ I If ($ user = "checked ")
// Echo "script" alert ('dfh') script ";
$ Db-> query ("update members set fuchu = '". $ _ POST ['fuchu']. "'Where uid = '". $ user [$ I]. "'");
}
}

If (! Empty ($ darr )){
For ($ j = 0; $ j <= count ($ darr); $ j ++ ){
$ Rowf = $ db-> get_one ("select * from members where fuchu =". $ _ POST ['fuchu']);
If ($ darr [$ j] = $ rowf ['uid'])
Unset ($ darr [$ j]);
Else
$ Db-> query ("update members set fuchu = '0' where uid = '". $ darr [$ j]. "'");
}
}

I use the array_diff method to distinguish the selected and unselected checkboxes. However, I tried it many times. The main problem is that $ _ POST ['user'] returns the clicked data. whether selected or not, if it is selected, if I want to cancel the selected data, I cannot. how can I pick out the data to cancel the selected data?


Reply to discussion (solution)

Please post the html code to identify how the data is submitted

Or you can paste the result in print_r ($ _ POST );

Please post the html code to identify how the data is submitted

Or you can paste the result in print_r ($ _ POST );




Print_r ($ _ POST); the result is two select values, which are useless.
Array ([depart] => 2 [fuchu] => 112 [submit] => submit)

This is the jquery code for obtaining the checkbox.
// City onchange
$ ('# Fuchu'). change (function (){
Var city = $ (this). val ();
Var province = $ ("# depart"). val ();
$. Get ("getdepart. php", {category: 'user', city: city, province: province}, function (data ){
Certificate ('{result'}.html (data );
// Alert (data );
});
});

This is html code
If ($ _ REQUEST ["city"]! = ""){
$ SQL = "select * from members left join userclass on members. flag = userclass. flagid where admitright = 4 and groupid =". $ _ REQUEST ['Province '];
$ Result = $ db-> query ($ SQL );
$ Str = "";
If ($ db-> num_rows ($ result)> 0 ){
While ($ row = $ db-> fetch_array ($ result )){
If ($ row ['fuchu'] =_ _ REQUEST ['city']) {
$ Str. = $ row ['username'];
$ Str. =" ";
} Else {
$ Str. = $ row ['username'];
$ Str. =" ";
}


}
}

Mysql_free_result ($ result );
}

The submitted data Array ([depart] => 2 [fuchu] => 112 [submit] => submit) does not contain user items.
If it is not submitted, it cannot be processed.

# The lower part of 2 is only the code for php to generate the check box string. it does not mean that the check box must be put into the form.

The submitted data Array ([depart] => 2 [fuchu] => 112 [submit] => submit) does not contain user items.
If it is not submitted, it cannot be processed.

# The lower part of 2 is only the code for php to generate the check box string. it does not mean that the check box must be put into the form.



Why does print_r ($ user) and print_r ($ darr) have data?

The submitted data Array ([depart] => 2 [fuchu] => 112 [submit] => submit) does not contain user items.
If it is not submitted, it cannot be processed.

# The lower part of 2 is only the code for php to generate the check box string. it does not mean that the check box must be put into the form.



So how can this form of page be more appropriate?

In this way, you use the submitted data: $ user =$ _ POST ['user'];
If print_r ($ _ POST); only display Array ([depart] => 2 [fuchu] => 112 [submit] => submit)
How do you use $ user ???

In this way, you use the submitted data: $ user =$ _ POST ['user'];
If print_r ($ _ POST); only display Array ([depart] => 2 [fuchu] => 112 [submit] => submit)
How do you use $ user ???



This code is in if (isset ($ _ POST ['user']), where the code can run

In this way, you use the submitted data: $ user =$ _ POST ['user'];
If print_r ($ _ POST); only display Array ([depart] => 2 [fuchu] => 112 [submit] => submit)
How do you use $ user ???



Yes. the content is available only after the checkbox is selected:
Array ([1] => 117) Array ([depart] => 2 [fuchu] => 112 [user] => Array ([0] => 124) [submit] => submit)

In this way, you use the submitted data: $ user =$ _ POST ['user'];
If print_r ($ _ POST); only display Array ([depart] => 2 [fuchu] => 112 [submit] => submit)
How do you use $ user ???



Now the question is, does the uncheck fail?

The check box is not selected and will not be submitted.
That is to say, whether $ _ POST ['user'] has values to be processed separately

The check box is not selected and will not be submitted.
That is to say, whether $ _ POST ['user'] has values to be processed separately



However, the result after I use the array_diff statement is not what I want. I don't know what went wrong. please help me. thank you.

You mean $ darr = array_diff ($ arr, $ user); this?
Your $ arr is an empty array regardless of the elements in the $ user array.
$ Darr is an empty array. I don't know why you want to do this.

You mean $ darr = array_diff ($ arr, $ user); this?
Your $ arr is an empty array regardless of the elements in the $ user array.
$ Darr is an empty array. I don't know why you want to do this.



I used another method to write the checkbox to the front-end without getting it from the background. now I have encountered some problems:

$ ("# BtnSubmit"). bind ("click", function (){
Var result = new Array ();

$ ("[Name = user]: checkbox"). each (function (){
If ($ (this). is (": checked ")){
Result. push ($ (this). attr ("value "));
}
});

$ ("# Hdtxt"). val (result. join (","));
Var userid = ;
$ ("# Hdfuchu"). val (userid );
// Alert ($ ("# hdtxt"). val ());
Alert (userid );

});

The select statement in the red text does not respond. you can delete it. why? On the above echo $ userid, we can see that there is a value. If you replace $ userid with a number, you can run it. what is the problem?

You mean $ darr = array_diff ($ arr, $ user); this?
Your $ arr is an empty array regardless of the elements in the $ user array.
$ Darr is an empty array. I don't know why you want to do this.



Let's take a look. The last point of this project is a problem.

This is solved in another way, but I still don't understand why $ _ GET variables cannot be assigned to jquery.

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.