查看: 722|回复: 3
|
PHP chechbox 复选问题!!!
[复制链接]
|
|
请问各位高手,当我选择多项checkbox时,我要如何写才能当我按“change”时,回到原本用户选择的选项,谢谢!
<?php session_start();
$_SESSION['Name'] = $_POST['Name'];
?>
<html>
<head>
<title>Testing</title>
<head>
<body>
<?php
if ($_POST['submit'] == 'submit')
{
$Name=$_POST['Name'];
if($_POST["hobby1"])
{ foreach($_POST['hobby1'] as $value)
{
$hobby2=join (', ', $_POST['hobby1']);
}
}
echo
("
Name: $Name<br>
Hobby: $hobby2<br>
");
echo
("<form name='frmtest' method = 'post' action='test.php'>
<input type = 'hidden' name = 'Name' value = '$Name'>
<br>
<input type = 'submit' name = 'change' value = 'Change' onclick='test.php'>
</form>");
exit;
}
?>
<form name="frmtest" method="post" action="test.php">
<p>
<strong>Name:</strong>
<input type="text" name="Name" value="<?php echo $_SESSION['Name'] ?>">
<br>
<br>
<strong>Hobby:</strong><br>
<input name="hobby1[]" type="checkbox" value="Gaming">
Gaming
<input name="hobby1[]" type="checkbox" value="Cooking" id="hobby1">
Cooking
<input name="hobby1[]" type="checkbox" value="Swimming">
Swimming
<br>
<br>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html> |
|
|
|
|
|
|
|
发表于 11-9-2007 10:11 AM
|
显示全部楼层
这是很粗造的做法,自己优化吧 
- <?php
- session_start();
- $_SESSION['Name'] = $_POST['Name'];
- ?>
- <html>
- <head>
- <title>Testing</title>
- <head>
- <body>
- <?php
- if ($_POST['change'] == 'change') {
- if (isset($_POST['Hobby']) && !empty($_POST['Hobby'])) {
- //unserialize back
- $hobby = stripslashes($_POST['Hobby']);
- $hobby = unserialize($hobby);
-
- //change key
- //Gaming - 0
- //Cooing - 1
- //Swimming - 2
- for($i=0;$i<3;$i++) {
- if ($hobby[$i]=='Gaming') {
- $temp = $hobby[$i];
- $hobby[$i] = $hobby[0];
- $hobby[0] = $temp;
- }
- if ($hobby[$i]=='Cooking') {
- $temp = $hobby[$i];
- $hobby[$i] = $hobby[1];
- $hobby[1] = $temp;
- }
- if ($hobby[$i]=='Swimming') {
- $temp = $hobby[$i];
- $hobby[$i] = $hobby[2];
- $hobby[2] = $temp;
- }
- }
- foreach($hobby as $key => $val) {
- if (empty($val)) {
- unset($hobby[$key]);
- }
- }
- print_r($hobby);
- }
- }
- if ($_POST['submit'] == 'submit')
- {
- $Name=$_POST['Name'];
- if($_POST["hobby1"])
- {
- foreach($_POST['hobby1'] as $value)
- {
- $hobby2=join (', ', $_POST['hobby1']);
- }
- $serializeHobbies = serialize($_POST["hobby1"]);
- }
- echo
- ("
- Name: $Name<br>
- Hobby: $hobby2<br>
- ");
- echo
- ("<form name='frmtest' method = 'post' action='test.php'>
- <input type = 'hidden' name = 'Name' value = '$Name'>
- <input type = 'hidden' name = 'Hobby' value = '$serializeHobbies'>
- <br>
- <input type = 'submit' name = 'change' value = 'change'>
- </form>");
- exit;
- }
- ?>
- <form name="frmtest" method="post" action="test.php">
- <p>
- <strong>Name:</strong>
- <input type="text" name="Name" value="<?php echo $_SESSION['Name'] ?>">
- <br>
- <br>
- <strong>Hobby:</strong><br>
- <input name="hobby1[]" type="checkbox" value="Gaming" <?php if (isset($hobby[0])) echo "checked"; ?>>
- Gaming
- <input name="hobby1[]" type="checkbox" value="Cooking" id="hobby1" <?php if (isset($hobby[1])) echo "checked"; ?>>
- Cooking
- <input name="hobby1[]" type="checkbox" value="Swimming" <?php if (isset($hobby[2])) echo "checked"; ?>>
- Swimming
- <br>
- <br>
- <input type="submit" name="submit" value="submit">
- </form>
- </body>
- </html>
复制代码 |
|
|
|
|
|
|
|

楼主 |
发表于 11-9-2007 12:09 PM
|
显示全部楼层
真的非常谢谢您! |
|
|
|
|
|
|
|

楼主 |
发表于 12-9-2007 01:03 AM
|
显示全部楼层
请问如果我想要hobby像Sex那样,当Name没输入时;错误讯息显示时,用户原来的选项还在,我要如何写呢?希望各位能够帮帮我,谢谢!
<?php
session_start();
$_SESSION['Name'] = $_POST['Name'];
$_SESSION['sex'] = $_POST['sex'];
?>
<html>
<head>
<title>Testing</title>
<meta http-equiv="Content-Type" c>
<head>
<body>
<?php
if ($_POST['change'] == 'change') {
if (isset($_POST['Hobby']) && !empty($_POST['Hobby'])) {
$hobby = stripslashes($_POST['Hobby']);
$hobby = unserialize($hobby);
for($i=0;$i<3;$i++) {
if ($hobby[$i]=='Gaming') {
$temp = $hobby[$i];
$hobby[$i] = $hobby[0];
$hobby[0] = $temp;
}
if ($hobby[$i]=='Cooking') {
$temp = $hobby[$i];
$hobby[$i] = $hobby[1];
$hobby[1] = $temp;
}
if ($hobby[$i]=='Swimming') {
$temp = $hobby[$i];
$hobby[$i] = $hobby[2];
$hobby[2] = $temp;
}
}
foreach($hobby as $key => $val) {
if (empty($val)) {
unset($hobby[$key]);
}
}
}
}
if ($_POST['submit'] == 'submit')
{
if (($_POST["Name"]!="")&&($_POST["hobby1"]!="")&&($_POST["sex"]!=""))
{
$Name=$_POST['Name'];
$sex=$_POST['sex'];
if($_POST["hobby1"])
{
foreach($_POST['hobby1'] as $value)
{
$hobby2=join (', ', $_POST['hobby1']);
}
$serializeHobbies = serialize($_POST["hobby1"]);
}
echo
(" Name: $Name<br>
Hobby: $hobby2<br>
Sex: $sex<br> ");
echo
("<form name='frmtest' method = 'post' action='test.php'>
<input type = 'hidden' name = 'Name' value = '$Name'>
<input type = 'hidden' name = 'sex' value = '$sex'>
<input type = 'hidden' name = 'Hobby' value = '$serializeHobbies'>
<br>
<input type = 'submit' name = 'change' value = 'change'>
</form>");
exit;
}
else
{$error="Cannot Empty!!!";}
}
?>
<?php echo $error;?>
<form name="frmtest" method="post" action="test.php">
<p>
<strong>Name:</strong>
<input type="text" name="Name" value="<?php echo $_SESSION['Name'] ?>">
<br>
<br>
<strong>Hobby:</strong><br>
<input name="hobby1[]" type="checkbox" value="Gaming" <?php if (isset($hobby[0])) echo "checked"; ?>>
Gaming
<input name="hobby1[]" type="checkbox" value="Cooking" id="hobby1" <?php if (isset($hobby[1])) echo "checked"; ?>>
Cooking
<input name="hobby1[]" type="checkbox" value="Swimming" <?php if (isset($hobby[2])) echo "checked"; ?>>
Swimming
<p><strong>Sex:</strong><br>
<input type="radio" name="sex" value="M"<?php echo (($_SESSION['sex'] == "M")?"CHECKED":""); ?>>
Male
<input type="radio" name="sex" value="F"<?php echo (($_SESSION['sex'] == "F")?"CHECKED":""); ?>>
Female<br>
<br>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html> |
|
|
|
|
|
|
| |
本周最热论坛帖子
|