0

好的,第一个问题是“单击提交后如何显示复选框”。

我有一个代码在同一页面上显示用户名字段和复选框。我想检查“用户名”是否在数据库中,然后显示带有选项的复选框,如果他们已经检查了选项,则显示它们以修改数据库上的记录。

我的代码是这样的。(显示表单的表单,我不想只显示用户名字段然后是复选框)。

<form name="form1" action="test3.php" method="post">
        <table width="100%" >

    Please enter your Username: <input type = "Text" value ="" name = "username"><br />

    Please select the category that you want to receive on your moval.edu e-mail account.<br /><br />
    <b>Athletics:</b><br><br />
    <b>MENS</b><br />

    <input type="checkbox" name="mode[]" id="mode" value="Football">Football<br>
    <input type="checkbox" name="mode[]" id="mode" value="Baseball">Baseball<br>
    <input type="checkbox" name="mode[]" id="mode" value="Mens Basketball">Basketball<br>
    <input type="checkbox" name="mode[]" id="mode" value="Mens Cheer">Cheer<br>
    <input type="checkbox" name="mode[]" id="mode" value="Mens Cross Country">Cross Country<br>
    <input type="checkbox" name="mode[]" id="mode" value="Mens Golf">Golf<br>
    <input type="checkbox" name="mode[]" id="mode" value="Mens Lacrosse">Lacrosse<br>
    <input type="checkbox" name="mode[]" id="mode" value="Mens Rodeo">Rodeo<br>
    <input type="checkbox" name="mode[]" id="mode" value="Mens Soccer">Soccer<br>
    <input type="checkbox" name="mode[]" id="mode" value="Mens Tennis">Tennis<br>
    <input type="checkbox" name="mode[]" id="mode" value="Mens Track">Track<br>
    <input type="checkbox" name="mode[]" id="mode" value="Mens Volleyball">Volleyball<br>
    <input type="checkbox" name="mode[]" id="mode" value="Mens Wrestling">Wrestling<br><br />

    <b>WOMENS</b><br />
    <input type="checkbox" name="mode[]" id="mode" value="Womens Basketball">Basketball<br>
    <input type="checkbox" name="mode[]" id="mode" value="Womens Cheer">Cheer<br>
    <input type="checkbox" name="mode[]" id="mode" value="Womens Cross Country">Cross Country<br>
    <input type="checkbox" name="mode[]" id="mode" value="Dance">Dance<br>
    <input type="checkbox" name="mode[]" id="mode" value="Womens Golf">Golf<br>
    <input type="checkbox" name="mode[]" id="mode" value="Womens Lacrosse">Lacrosse<br>
    <input type="checkbox" name="mode[]" id="mode" value="Womens Rodeo">Rodeo<br>
    <input type="checkbox" name="mode[]" id="mode" value="Womens Soccer">Soccer<br>
    <input type="checkbox" name="mode[]" id="mode" value="Softball">Softball<br>
    <input type="checkbox" name="mode[]" id="mode" value="Womens Tennis">Tennis<br>
    <input type="checkbox" name="mode[]" id="mode" value="Womens Track">Track<br>
    <input type="checkbox" name="mode[]" id="mode" value="Womens Volleyball">Volleyball<br>
    <input type="checkbox" name="mode[]" id="mode" value="Womens Wrestling">Wrestling<br><br />

    <b>STUDENT AFFAIRS ACIVITIES</b><br><br />
    <input type="checkbox" name="mode[]" id="mode" value="Student Affairs"> Sign me up for Student Affairs Activities<br /><br />

    <b>INTERNAIONAL CLUB ACTIVITIES</b><br /><br />
    <input type="checkbox" name="mode[]" id="mode" value="International Club"> Sign me up for International Club Activities<br /><br />

    <b>HISTORICAL ACTIVITIES</b><br><br />
    <input type="checkbox" name="mode[]" id="mode" value="Historical Activities"> Sign me up for Hisorical Activities<br /><br />

    <b>CONTESTS</b><br><br />
    <input type="checkbox" name="mode[]" id="mode" value="Contests"> Sign me up for Contests<br /><br />

    <b>LIBRARY EVENTS</b><br><br />
    <input type="checkbox" name="mode[]" id="mode" value="Library Events"> Sign me up for Library Evens<br /><br />

    <b>VIKING EXCHANGE</b><br /><br />
    <input type="checkbox" name="mode[]" id="mode" value="Viking Exchange"> Sign me up for Viking Exchange News<br /><br />


</table>





<input name="cmd_submit" type="submit" id="submit" value="Submit" />

</form>

这里是行动。

<?php
if(isset($_POST['mode'])) {
$mode = implode(",", $_POST['mode']);   
} else {
$mode = "";
}
?>


<?php
$dbhost  = 'localhost';
$dbname  = 'db_name';
$dbuser  = 'username';
$dbpass  = 'pass'; 

$con = mysql_connect($dbhost, $dbuser, $dbpass);

if($con == FALSE)
{
    echo 'Cannot connect to database' . mysql_error();
}
else
{
    echo 'Connected to database';
}

mysql_select_db($dbname, $con);
// Get values from form 
$username = mysql_escape_string($_POST['username']);

$sql="INSERT INTO email(username, mode) VALUES('$username', '$mode')";
$result=mysql_query($sql);

if (!mysql_query($sql, $con))
{
    die('Error: ' . mysql_error());
}
echo "1 record added";

mysql_close($con)
?>

先谢谢了。

4

2 回答 2

0

用于checked="checked"默认选中复选框。

于 2013-10-03T15:17:41.227 回答
0

在开始表单之前打开一个while循环,从数据库中选择已经选择的复选框字段。并检查如下所示的条件是否等于表单中的复选框。如果是这样,请将其设为选中。一个例子如下所示:

    $result=mysql_query("SELECT checkbox_fields FROM table_name");
    while($row=mysql_fetch_assoc($row))
    {



  <input type="checkbox" name="football" id="football" value="football" <?php echo ($row['checkbox_fields']==football ? 'checked' : '');?>>Footbal<br>

     <input type="checkbox" name="baseball" id="baseball" value="baseball" <?php echo ($row['checkbox_fields']==baseball ? 'checked' : '');?>>Baseball<br>
etc...

    }
于 2013-10-03T16:42:09.287 回答