How to retrieve value from the Check boxes?

只谈情不闲聊 提交于 2020-01-11 11:21:55

问题


I'm trying to get the emails corresponding to the checkbox using the following codes. But, I'm not getting the correct checked emails in the new variable. Can anyone please check ??

<?php
include("connection.php");
$username=$_SESSION['username'];
$query=mysql_query("SELECT * FROM contacts WHERE username='$username'");
$num=mysql_num_rows($query);
$info=mysql_fetch_array($query);
$i=0;
$msg='';
?>
  <table width="672" border="0">
<?php 
$i=0;
while($info)
{
?>
<form action="compose.php" method="post">
<tr style="font-size:14px;">
    <td width="21" bgcolor="#f2f2f2"> <input type="checkbox" name="add" onSelect="<?php $msg=$msg.$info['email'].", ";?>"/> </td>
    <td width="229" bgcolor="#f2f2f2"> <?php  echo $info['email']; ?> </td>
    <td width="408" bgcolor="#f2f2f2"> <?php  echo $info['name']; ?> </td>
  </tr>
<?php 
$info=mysql_fetch_array($query);
$i++;
}

$_SESSION['contacts']=$msg;
?>
<tr><td></td><td></td><td><br />
<input class="new-button" type="submit" value="Insert & Compose" name="submit" /></td>
</tr>
</form>



</table>

回答1:


To get any value back for checkboxes they must have a value=. In your case you probably would want the value to be the according email address.

One problem with your code is using onSelect= instead of value=, and second you didn't print the actual value into the page. Rewrite it to:

<td width="21" bgcolor="#f2f2f2">
    <input type="checkbox" name="add"
     value="<?php print $info['email']; ?>"/> </td>

If you need the $msg variable to do something, assemble it after the output.




回答2:


<input type="checkbox" name="add" value="<?php echo $msg.$info['email'];?>"/>

checkbox does not have onSelect event probobly you got value in mind and in PHP code you should echo and what .", " is for?



来源:https://stackoverflow.com/questions/5320645/how-to-retrieve-value-from-the-check-boxes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!