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.