Home  >  Article  >  Backend Development  >  jquery gets the values ​​of multiple checkboxes and submits them to php asynchronously

jquery gets the values ​​of multiple checkboxes and submits them to php asynchronously

WBOY
WBOYOriginal
2016-08-08 09:22:28903browse

html code:

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

My data in html is read from the database, which can be understood as the following code

<span><</span><span>li</span><span>><</span><span>input </span><span>type</span><span>="checkbox"</span><span> name</span><span>="uid"</span><span>  value</span><span>="1"</span><span>/></span>用户1<span></</span><span>li</span><span>></span><span><</span><span>li</span><span>><</span><span>input </span><span>type</span><span>="checkbox"</span><span> name</span><span>="uid"</span><span>  value</span><span>="2"</span><span>/></span>用户2<span></</span><span>li</span><span>></span><span><</span><span>li</span><span>><</span><span>input </span><span>type</span><span>="checkbox"</span><span> name</span><span>="uid"</span><span>  value</span><span>="3"</span><span>/></span>用户3<span></</span><span>li</span><span>></span><span><</span><span>li</span><span>><</span><span>input </span><span>type</span><span>="checkbox"</span><span> name</span><span>="uid"</span><span>  value</span><span>="4"</span><span>/></span>用户4<span></</span><span>li</span><span>></span>

jquery code:

<span> 1</span><span>var</span> mt4Ids =<span> [];
</span><span> 2</span> $('input[name=uid]').each(<span>function</span><span>() {
</span><span> 3</span><span>if</span>(<span>this</span><span>.checked) {
</span><span> 4</span>         mt4Ids.push($(<span>this</span><span>).val());
</span><span> 5</span><span>    }
</span><span> 6</span><span>});
</span><span> 7</span> data =<span> {
</span><span> 8</span><span>    mt4Ids : JSON.stringify(mt4Ids)
</span><span> 9</span><span>};
</span><span>10</span><span>var</span> pUrl = "/a/manageUser.html"<span>;
</span><span>11</span> $.post(pUrl, data, <span>function</span><span>(data){
</span><span>12</span><span>if</span>(data.state == 1<span>){
</span><span>13</span><span>        alert(data.msg);
</span><span>14</span>         location.href = "/h/permission.html"<span>;
</span><span>15</span>     }<span>else</span><span>{
</span><span>16</span>         alert("操作失败"<span>);
</span><span>17</span><span>    }
</span><span>18</span> }, 'json');

PHP code

<span> 1</span><span>$mt4Ids</span> = !<span>empty</span>(<span>$_POST</span>['mt4Ids']) ? <span>$_POST</span>['mt4Ids'] : <span>false</span><span>;
</span><span> 2</span><span> 3</span><span>$stripMt4Ids</span> = <span>preg_replace</span>('/[\"\[\]]/', '', <span>$mt4Ids</span><span>);
</span><span> 4</span><span>$mt4IdsToArr</span> = <span>explode</span>(',', <span>$stripMt4Ids</span><span>);
</span><span> 5</span><span> 6</span><span>foreach</span>(<span>$mt4IdsToArr</span><span>as</span><span>$uid</span><span>){
</span><span> 7</span>     permission_relation::add(<span>$uid</span>, <span>$gid</span><span>);
</span><span> 8</span><span>}
</span><span> 9</span><span>$data</span> = <span>array</span><span>(
</span><span>10</span>     'state' => 1,
<span>11</span>     'msg'   => '操作成功'
<span>12</span><span>);
</span><span>13</span><span>echo</span> json_encode(<span>$data</span><span>);
</span><span>14</span><span>return</span><span>false</span>;

// $gid can be ignored

The above introduces jquery to obtain the values ​​of multiple checkboxes and submit them to php asynchronously, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Nginx cache cleaningNext article:Nginx cache cleaning