Jquery returns multiple checkbox values and asynchronously submits them to php. jquerycheckbox

Source: Internet
Author: User

Jquery returns multiple checkbox values and asynchronously submits them to php. jquerycheckbox

This example describes how jquery asynchronously submits multiple checkbox values to php. Share it with you for your reference. The specific implementation method is as follows:

Html code:

<tr>  <td><input type="checkbox" name="uid" value="<?=$item['mtaccount_id']?>"></td>  <td><?=$item['mtaccount_id']?></td>  <td><?=$item['account_id']?></td>  <td><?=$item['account_name']?></td>  <td><?=$item['server']?></td>  <td><?=$item['platform']?></td></tr>

My data in html is read from the database and can be understood as the following code:

<Li> <input type = "checkbox" name = "uid" value = "1"/> User 1 </li> <input type = "checkbox" name = "uid" value = "2"/> User 2 </li> <input type = "checkbox" name = "uid" value = "3"/> User 3 </li> <input type = "checkbox" name = "uid" value = "4"/> User 4 </li>

Jquery code:

Var mt4Ids = []; $ ('input [name = uid] '). each (function () {if (this. checked) {mt4Ids. push ($ (this ). val () ;}}); data = {mt4Ids: JSON. stringify (mt4Ids)}; var pUrl = "/a/manageUser.html"; $. post (pUrl, data, function (data) {if (data. state = 1) {alert (data. msg); location. href = "/h/permission.html";} else {alert ("operation failed") ;}, 'json ');

PHP code

$ Mt4Ids =! Empty ($ _ POST ['mt4kids'])? $ _ POST ['mt4kids']: false; $ stripMt4Ids = preg_replace ('/[\ "\ [\]/','', $ mt4Ids ); $ mt4IdsToArr = explode (',', $ stripMt4Ids); foreach ($ mt4IdsToArr as $ uid) {permission_relation: add ($ uid, $ gid );} $ data = array ('state' => 1, 'msg '=> 'Operation succeeded'); echo json_encode ($ data); return false; // $ gid can be ignored

I hope this article will help you with php programming.

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.