复选框使用变量值作为电子邮件地址

时间:2013-07-30 15:40:07

标签: php

我正在创建一个由数据库中提取的数据填充的表单。该信息是公司用户的联系方式。例如手机号码和电子邮件地址。我尝试做的是使用复选框选择哪些用户发送消息。表单在表格中创建,该表格显示复选框,ID,姓氏,姓名,移动,电子邮件和部门。复选框值= =相同行信息的ID。如何在输入字段中显示值。这是我到目前为止的代码。

<form name="form1" id="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']    ?>">
<?php

// setting variables
$db_host = "localhost";
$username = "userN";
$password = "PassW";
$db_name = "DB_name";

//Connect to db
$link = mysql_connect("$db_host","$username","$password");

// check connection
if (!$link) {
   die('Could not connect: ' . mysql_error());
}

// select table in db
$db = mysql_select_db("$db_name");

// check if db selected 
if (!$db) {
    die ('Can\'t use ' . $db_name . mysql_error());
}

// query data
$res = mysql_query('SELECT ID, Surname, Names, Mobile, EMail, Department FROM rars  limit 0, 10');

// check if query is working
if (!$res) {
    die('Invalid query: ' . mysql_error());
}


// setting table and heading
echo "<table height='50px' width='100%' border='1' align='center' cellpadding='0' cellspacing='4' bgcolor='#EEEEEE'>
        <tr align='center'>
        <td width='10'><font color='#222222' size='2'><strong></font></strong></td>
        <td><font color='#222222' size='2'><strong>ID</font></strong></td> 
        <td><font color='#222222' size='2'><strong>Surname</font></strong></td>
        <td><font color='#222222' size='2'><strong>Name</font></strong></td>
    <td><font color='#222222' size='2'><strong>Mobile</font></strong></td>
    <td><font color='#222222' size='2'><strong>EMail</font></strong></td>
    <td><font color='#222222' size='2'><strong>Department</font></strong></td>

      </tr>";

// populating array
while($row = mysql_fetch_array($res))
{

 echo "<tr align='center'>";
//populating rest of columns
    echo "<td><input type='checkbox' name='checkbox' value='$row[ID]'>" . "</td>";
    echo "<td><font color='#111111' size='2'>" . $row['ID'] . "</font></strong></td>";
    echo "<td><font color='#111111' size='2'>" . $row['Surname'] . "</font></strong>   </td>";
    echo "<td><font color='#111111' size='2'>" . $row['Names'] . "</font></strong></td>";
    echo "<td><font color='#111111' size='2'>" . $row['Mobile'] . "</font></strong></td>";
    echo "<td><font color='#111111' size='2'>" . $row['EMail'] . "</font></strong></td>";
    echo "<td><font color='#111111' size='2'>" . $row['Department'] . "</font></strong></td>";
 echo "</tr>";
}
echo "</table>"; 

// closing db    
mysql_close($link); 

?>

<br><br><input type="submit" name="submit" value="Submit" /> <br><br>
<?php
if($_POST['submit'])
{
    if(isset($_POST['checkbox']))
    {
        $emails = $_POST['checkbox'];
        echo "The following email addresses have been selected: <br />";
        foreach($emails as $key => $value)
        {
            echo $value . "<br />";
        }

    }
    else
    {
         echo "no email address selected";
    }
}
?> 

</form>

当您选中某些复选框并单击提交时,不会显示任何ID。 如果你能提供帮助,我们将不胜感激。 谢谢Danny

1 个答案:

答案 0 :(得分:1)

我是否正确地认为你的意思是id不在复选框值中输出?如果是这样,只需更改如下所示的行:

echo "<td><input type='checkbox' name='checkbox' value='$row[ID]'>" . "</td>";

并改为:

echo "<td><input type='checkbox' name='checkbox' value='".$row['ID']."'></td>";

请注意,您在此脚本中使用的mysql函数现已弃用。现在使用改进的函数,例如mysqli和PDO。