2015-02-08 2 views
1

Как реализовать «select/unselect all» флажок в php?

<html> 
 
<head> 
 
<meta name="viewport" content="width=device-width, initial-scale=1" /> 
 
<title>View Users</title> 
 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<style> 
 
body { 
 
    font: normal medium/1.4 sans-serif; 
 
} 
 
div.greetblock, div.serverresponse { 
 
    border-collapse: collapse; 
 
    width: 100%; 
 
    margin-left: auto; 
 
    margin-right: auto; 
 
    align: center; 
 
} 
 
tr > td { 
 
    padding: 0.25rem; 
 
    text-align: center; 
 
    border: 1px solid #ccc; 
 
} 
 
tr:nth-child(even) { 
 
    background: #fff; 
 
    
 
} 
 
tr:nth-child(odd) { 
 
    background: #FA9A8B; 
 
    color: #fff; 
 
} 
 
tr#header{ 
 
background: #F78371; 
 
} 
 

 
div#norecord{ 
 
margin-top:10px; 
 
width: 15%; 
 
margin-left: auto; 
 
margin-right: auto; 
 
} 
 
input,select{ 
 
cursor: pointer; 
 
} 
 
img{ 
 
margin-top: 10px; 
 
height: 200px; 
 
width: 300px; 
 
} 
 
select{ 
 
width: 200px 
 
} 
 
div.leftdiv{ 
 
width: 100%; 
 
padding: 0 10px; 
 
float: center; 
 
border: 1px solid #ccc; 
 
margin: 5px; 
 
height: 320px; 
 
text-align:center; 
 
} 
 
div.rightdiv{ 
 
width: 45%; 
 
padding: 0 10px; 
 
float: right; 
 
border: 1px solid #ccc; 
 
margin: 5px; 
 
height: 320px; 
 
text-align:center; 
 
} 
 
hidediv{ 
 
display: none; 
 
} 
 
p.header{ 
 
height: 40px; 
 
background-color: #EB5038; 
 
padding: 10px; 
 
color: #fff; 
 
text-align:center; 
 
margin: 0; 
 
margin-bottom: 10px; 
 
} 
 
textarea{ 
 
font-size: 25px; 
 
font-weight: normal; 
 
} 
 

 
</style> 
 
<script> 
 

 
function sendMsg(){ 
 
var msgLength = $.trim($("textarea").val()).length; 
 
var checkedCB = $("input[type='checkbox']:checked").length; 
 
if(checkedCB == 0){ 
 
\t alert("You must select atleast one User to send message"); 
 
}else if(msgLength == 0){ 
 
\t alert("You left the message field blank, please fill it"); 
 
}else{ 
 
\t var formData = $(".wrapper").find("input").serialize() + "&imgurl="+ $("#festival").val() + "&message=" + $("textarea").val(); \t 
 
\t $.ajax({type: "POST",data: formData, url: "processmessage.php", success:function(res){ 
 
\t \t $(".greetblock").slideUp(1000); 
 
\t \t $(".serverresponse").prepend(res).hide().fadeIn(2000); 
 
\t }}); 
 
} 
 
} 
 
$(function(){ 
 
\t $(".serverresponse").hide() 
 
\t $("input[type='checkbox']").click(function(){ 
 
\t \t if($(this).is(':checked')){ 
 
\t \t \t $(this).parent().css("border","3px solid red"); 
 
\t \t }else{ 
 
\t \t \t $(this).parent().css("border","0px"); 
 
\t \t } 
 
\t }); 
 
\t 
 
\t $("div.leftdiv, div.rightdiv").hover(function(){ 
 
\t \t $(this).css("background","#FAFAFA"); 
 
\t },function(){ 
 
\t \t $(this).css("background","#fff"); 
 
\t }); 
 
\t 
 
\t $("#festival").change(function(){ 
 
\t \t $("img").attr("src",$(this).val()); 
 
\t }); 
 
\t 
 
\t $("#sendmsg").click(function(){ 
 
\t \t $(".serverresponse").fadeOut(300,function(){ 
 
\t \t \t $(".greetblock").fadeIn(1000); 
 
\t \t }); \t \t 
 
\t }); 
 
}); 
 
</script> 
 
</head> 
 
<body> 
 
<?php 
 
    include_once 'db_functions.php'; 
 
    $db = new DB_Functions(); 
 
    $users = $db->getAllUsers(); 
 
    if ($users != false) 
 
     $no_of_users = mysql_num_rows($users); 
 
    else 
 
     $no_of_users = 0; \t 
 
?> 
 
<?php 
 
    if ($no_of_users > 0) { 
 
?> 
 

 
<div class="greetblock"> 
 
<div class="leftdiv"> 
 
<p class="header">Select Users to whom you want to send an announcement 
 
</p> 
 
<table style="width:100%"> 
 
<tr id="header"><td>Id</td><td>EmailId</td><td>Send Message?</td></tr> 
 
<?php 
 
    while ($row = mysql_fetch_array($users)) { 
 
?> 
 
<tr> 
 
<td><span><?php echo $row["id"] ?></span></td> 
 
<td><span><?php echo $row["emailid"] ?></span></td> 
 
<td><span class="wrapper"><input type="checkbox" name="sendmsg[]" value="<?php echo $row["emailid"] ?>"/></span></td> 
 
</tr> 
 
<?php } ?> 
 
</table> 
 
</div> 
 

 
<div class="leftdiv"> 
 
<p class="header">Type your message 
 
</p> 
 
<textarea cols="15" rows="5" value="txtarea"> 
 

 
</textarea> 
 

 
<center> 
 
<button onclick="sendMsg()">Send Message</button> 
 
</center> 
 

 

 
</div> 
 

 
</div> 
 
<div class="serverresponse hidediv"> 
 
<center><button id="sendmsg">Send Message Again</button></center> 
 
</div> 
 
<?php }else{ ?> 
 
<div id="norecord"> 
 
No records in MySQL DB 
 
</div> 
 
<?php } ?> 
 

 
</body> 
 
</html> 
 
          
 

Приведенный выше код показывает список пользователей, которым я могу отправить сообщение с помощью Google Cloud Messaging. Каждый пользователь отображается в таблице с пометкой для каждого из них. Я могу выбрать пользователей из таблицы, чтобы отправить им сообщение Я хочу добавить к нему флажок select/unselect. Может кто-нибудь мне помочь?

Спасибо заранее.

+1

Вы уверены, что хотите использовать PHP, чтобы выбрать/снять несколько флажков HTML? Похоже, что JavaScript будет лучшим решением. – Mex

+0

См. [Link] (http://www.plus2net.com/javascript_tutorial/checkbox-checkall.php) – Lal

ответ

0

Как примечание @Mex и @Lai, большинство людей используют Javascript для этого. Вы можете использовать чистый подход Javascript, как в ссылке @ Lai или jQuery, если вы используете это.

Если по какой-то причине вам нужно использовать PHP для этого, вы, вероятно, захотите установить флажок «Выбрать все». Традиционно это находится в заголовке таблицы над отдельными ячейками.

Когда кто-то отправляет форму, вы можете установить флажок «Выбрать все» и, если она отмечена, обрабатывать форму так, как если бы пользователь проверил все флажки отдельно.

+0

Благодарю вас за ответ. Я новичок в кодировании. Я хочу добавить флажок «Выбрать все», как вы предлагали. можете ли вы помочь мне с кодом? –

0

попробовать это, это работа

<html> 
<head><title>View Users</title> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
<style> 
body { 
    font: normal medium/1.4 sans-serif; 
} 
div.greetblock, div.serverresponse { 
    border-collapse: collapse; 
    width: 60%; 
    margin-left: auto; 
    margin-right: auto; 
    align: center; 
} 
tr > td { 
    padding: 0.25rem; 
    text-align: center; 
    border: 1px solid #ccc; 
} 
tr:nth-child(even) { 
    background: #fff; 

} 
tr:nth-child(odd) { 
    background: #FA9A8B; 
    color: #fff; 
} 
tr#header{ 
background: #F78371; 
} 

div#norecord{ 
margin-top:10px; 
width: 15%; 
margin-left: auto; 
margin-right: auto; 
} 
input,select{ 
cursor: pointer; 
} 
img{ 
margin-top: 10px; 
height: 200px; 
width: 300px; 
} 
select{ 
width: 200px 
} 
div.leftdiv{ 
width: 45%; 
padding: 0 10px; 
float: left; 
border: 1px solid #ccc; 
margin: 5px; 
height: 320px; 
text-align:center; 
} 
div.rightdiv{ 
width: 45%; 
padding: 0 10px; 
float: right; 
border: 1px solid #ccc; 
margin: 5px; 
height: 320px; 
text-align:center; 
} 
hidediv{ 
display: none; 
} 
p.header{ 
height: 40px; 
background-color: #EB5038; 
padding: 10px; 
color: #fff; 
text-align:center; 
margin: 0; 
margin-bottom: 10px; 
} 
textarea{ 
font-size: 25px; 
font-weight: bold; 
} 

</style> 
<script> 

function sendMsg(){ 
var msgLength = $.trim($("textarea").val()).length; 
var checkedCB = $("input[type='checkbox']:checked").length; 
if(checkedCB == 0){ 
    alert("You must select atleast one User to send message"); 
}else if(msgLength == 0){ 
    alert("You left the message field blank, please fill it"); 
}else{ 
    var formData = $(".wrapper").find("input").serialize() + "&imgurl="+ $("#festival").val() + "&message=" + $("textarea").val(); 
    $.ajax({type: "POST",data: formData, url: "processmessage.php", success:function(res){ 
     $(".greetblock").slideUp(1000); 
     $(".serverresponse").prepend(res).hide().fadeIn(2000); 
    }}); 
} 
} 

$(function(){ 
    $(".serverresponse").hide() 
    $("input[type='checkbox']").click(function(){ 
     if($(this).is(':checked')){ 

     }else{ 

      $(this).parent().css("border","0px"); 
     } 
    }); 

$("#select_all").click(function(){ 
     var checked_status = this.checked; 
     $("input[name='sendmsg[]']").each(function(){ 
     this.checked = checked_status; 
     }); 
    }); 


    $("div.leftdiv, div.rightdiv").hover(function(){ 
     $(this).css("background","#FAFAFA"); 
    },function(){ 
     $(this).css("background","#fff"); 
    }); 

    $("#festival").change(function(){ 
     $("img").attr("src",$(this).val()); 
    }); 

    $("#sendmsg").click(function(){ 
     $(".serverresponse").fadeOut(300,function(){ 
      $(".greetblock").fadeIn(1000); 
     });  
    }); 
}); 
</script> 
</head> 
<body> 
<?php 
    include_once 'db_functions.php'; 
    $db = new DB_Functions(); 
    $users = $db->getAllUsers(); 
    if ($users != false) 
     $no_of_users = mysql_num_rows($users); 
    else 
     $no_of_users = 0; 
?> 
<?php 
    if ($no_of_users > 0) { 
?> 

<div class="greetblock"> 
<div class="leftdiv"> 
<p class="header">Select Users to send message 
</p> 

<td><input type="checkbox" id="select_all"/>Select All</td> 
<table> 
<tr id="header"><td>Id</td><td>EmailId</td><td>Send Message?</td></tr> 
<?php 
    while ($row = mysql_fetch_array($users)) { 
?> 
<tr> 

<td><span><?php echo $row["id"] ?></span></td> 
<td><span><?php echo $row["phones"] ?></span></td> 
<td><span class="wrapper"><input type="checkbox" name="sendmsg[]" id="chk" value="<?php echo $row["phones"] ?>"/></span></td> 
</tr> 
<?php } ?> 

</table> 
</div> 
<div class="rightdiv"> 
<p class="header">Type your message 
</p> 
<textarea cols="15" rows="5" value="txtarea"> 
</textarea> 
<br/> 

<center> 
<button onclick="sendMsg()">Send Message</button> 
</center> 
</div> 

</div> 
<div class="serverresponse hidediv"> 
<center><button id="sendmsg">Send Message Again</button></center> 
</div> 
<?php }else{ ?> 
<div id="norecord"> 
No records in MySQL DB 
</div> 
<?php } ?> 

</body> 
</html> 
Смежные вопросы