jquery Gets the value of multiple checkbox asynchronous submit to Php method _php tips

Source: Internet
Author: User

The example in this article describes how jquery gets the value of multiple checkbox asynchronously submitted to PHP. Share to everyone for your reference. The implementation methods are 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 HTML data is read from the database, this can be understood as the following code

<li><input type= "checkbox" Name= "UID" value= "1"/> user 1</li> <li><input
"Type=" CheckBox "Name=" UID "value=" 2 "/> user 2</li>
<li><input type=" checkbox "Name=" UID "value=" 3 "/ > User 3</li>
<li><input type= "checkbox" Name= "UID" value= "4"/> User 4</li>

jquery Code:

var mt4ids = [];
 $ (' input[name=uid] '). each (the 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

$MT 4Ids =!empty ($_post[' mt4ids '])? $_post[' Mt4ids ']: false;
$stripMt 4Ids = preg_replace ('/[\ "\[\]]/', ', $mt 4Ids);
$MT 4IDSTOARR = Explode (', ', $stripMt 4Ids);
foreach ($mt 4IdsToArr as $uid) {
   permission_relation::add ($uid, $gid);
$data = Array (
   ' state ' => 1,
   ' msg '  => ' operation succeeded '
);
echo Json_encode ($data);
return false;
$gid can ignore

I hope this article will help you with your PHP program design.

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.