将单选按钮转换为复选框

时间:2014-02-18 18:46:00

标签: php mysql checkbox radio

我想知道如何将我的单选按钮转换为复选框,因为我想为同一条记录插入多个选项...例如:我希望游戏处于“策略”和“操作”类别,所以只有一个复选框可以让我实现这一点。

这是我使用单选按钮插入游戏的代码:

<?php
session_start();

if(!isset($_SESSION['user_name'])){

header("location:login.php");

}
else {
?>
<?php
include("../includes/connect.php");

$action_status = 'unchecked';
$sports_status = 'unchecked';

if(isset($_POST['submit'])){

$game_name = $_POST['game_name'];
$game_category = $_POST['game_category'];
$game_keywords = $_POST['game_keywords'];
$game_image = $_FILES['game_image']['name'];
$image_tmp = $_FILES['game_image']['tmp_name'];
$game_code = $_FILES['game_code']['name'];
$code_tmp = $_FILES['game_code']['tmp_name'];
$game_file = $_FILES['game_file']['name'];
$file_tmp = $_FILES['game_file']['tmp_name'];
$game_desc = $_POST['game_desc'];

if($game_name=='' or $game_category=='' or $game_keywords=='' or $game_image=='' or $game_code=='' or $game_file==''){

echo "<script>alert('Please enter all the fields below!')</script>";

exit();

}
else {

 $path = "../games/$game_name";

 mkdir("$path", 0777);

 move_uploaded_file($image_tmp,"../images/games_images/$game_image");

 move_uploaded_file($code_tmp,"$path/$game_code");

 move_uploaded_file($file_tmp,"$path/$game_file");

 $insert_query = "insert into games (game_name,game_category,game_keywords,game_image,game_code,game_file,game_desc) values ('$game_name','$game_category','$game_keywords','$game_image','$game_code','$game_file','$game_desc')";

 if($game_category == '3d'){

 $three_d_status = 'checked';

 }else if($game_category == 'action'){

 $action_status = 'checked';

 }else if($game_category == 'adventure'){

 $adventure_status = 'checked';

 }else if($game_category == 'arcade'){

 $arcade_status = 'checked';

 }else if($game_category == 'brain'){

 $brain_status = 'checked';

 }else if($game_category == 'cards'){

 $cards_status = 'checked';

 }else if($game_category == 'destruction'){

 $destruction_status = 'checked';

 }else if($game_category == 'education'){

 $education_status = 'checked';

 }else if($game_category == 'extreme'){

 $extreme_status = 'checked';

 }else if($game_category == 'fighting'){

 $fighting_status = 'checked';

 }else if($game_category == 'flying'){

 $flying_status = 'checked';

 }else if($game_category == 'girls'){

 $girls_status = 'checked';

 }else if($game_category == 'holidays'){

 $holidays_status = 'checked';

 }else if($game_category == 'hidden_objects'){

 $hidden_objects_status = 'checked';

 }else if($game_category == 'motocross'){

 $motocross_status = 'checked';

 }else if($game_category == 'multiplayer'){

 $multiplayer_status = 'checked';

 }else if($game_category == 'new'){

 $new_status = 'checked';

 }else if($game_category == 'puzzle'){

 $puzzle_status = 'checked';

 }else if($game_category == 'racing'){

 $racing_status = 'checked';

 }else if($game_category == 'fps'){

 $fps_status = 'checked';

 }else if($game_category == 'space'){

 $space_status = 'checked';

 }else if($game_category == 'sports'){

 $sports_status = 'checked';

 }else if($game_category == 'strategy'){

 $strategy_status = 'checked';

 }else if($game_category == 'stunt'){

 $stunt_status = 'checked';

 }else if($game_category == 'top_rated'){

 $top_rated_status = 'checked';

 }else if($game_category == 'words'){

 $words_status = 'checked';

 }else if($game_category == 'zombies'){

 $zombies_status = 'checked';

 }

 if(mysql_query($insert_query)){

 echo "<script>alert('The Game Uploaded Successfully!')</script>";

 echo "<script>window.open('view_games.php','_self')</script>";

 }

}

}

?>

<?php } ?>

<!DOCTYPE HTML>
<html>
<head>
<link href="css/insert_game.css" rel="stylesheet" type="text/css">
</head>
<body>

<form method="post" id="insert_form" action="insert_games.php" enctype="multipart/form-data">

<table id="insert_games_table" width="680" border="1" align="center">

<tr>
 <td id="insert_games_table_title" colspan="2" align="center">Insert New Game</td>
</tr>

<tr>
 <td id="insert_cat_name">Game name:</td>
 <td><input type="text" name="game_name" size="80"></td>
</tr>

<tr>
 <td id="insert_cat_name">Game category:</td>
 <td id="insert_cat_select"><input class="radio" type="radio" name="game_category" value="3d"<?php print $three_d_status; ?>/><span>3D</span><br>
 <input class="radio" type="radio" name="game_category" value="action"<?php print $action_status; ?>/><span>Action</span><br>
 <input class="radio" type="radio" name="game_category" value="adventure"<?php print $adventure_status; ?>/><span>Adventure</span><br>
 <input class="radio" type="radio" name="game_category" value="arcade"<?php print $arcade_status; ?>/><span>Arcade</span><br>
 <input class="radio" type="radio" name="game_category" value="brain"<?php print $brain_status; ?>/><span>Brain</span><br>
 <input class="radio" type="radio" name="game_category" value="cards"<?php print $cards_status; ?>/><span>Cards</span><br>
 <input class="radio" type="radio" name="game_category" value="destruction"<?php print $destruction_status; ?>/><span>Destruction</span><br>
 <input class="radio" type="radio" name="game_category" value="education"<?php print $education_status; ?>/><span>Education</span><br>
 <input class="radio" type="radio" name="game_category" value="extreme"<?php print $extreme_status; ?>/><span>Extreme</span><br>
 <input class="radio" type="radio" name="game_category" value="fighting"<?php print $fighting_status; ?>/><span>Fighting</span><br>
 <input class="radio" type="radio" name="game_category" value="flying"<?php print $flying_status; ?>/><span>Flying</span><br>
 <input class="radio" type="radio" name="game_category" value="girls"<?php print $girls_status; ?>/><span>Girls</span><br>
 <input class="radio" type="radio" name="game_category" value="holidays"<?php print $holidays_status; ?>/><span>Holidays</span><br>
 <input class="radio" type="radio" name="game_category" value="hidden_objects"<?php print $hidden_objects_status; ?>/><span>Hidden Objects</span><br>
 <input class="radio" type="radio" name="game_category" value="motocross"<?php print $motocross_status; ?>/><span>Motocross</span><br>
 <input class="radio" type="radio" name="game_category" value="multiplayer"<?php print $multiplayer_status; ?>/><span>Multiplayer</span><br>
 <input class="radio" type="radio" name="game_category" value="new"<?php print $new_status; ?>/><span>New Games</span><br>
 <input class="radio" type="radio" name="game_category" value="puzzle"<?php print $puzzle_status; ?>/><span>Puzzle</span><br>
 <input class="radio" type="radio" name="game_category" value="racing"<?php print $racing_status; ?>/><span>Racing</span><br>
 <input class="radio" type="radio" name="game_category" value="fps"<?php print $fps_status; ?>/><span>FPS</span><br>
 <input class="radio" type="radio" name="game_category" value="space"<?php print $space_status; ?>/><span>Space</span><br>
 <input class="radio" type="radio" name="game_category" value="sports"<?php print $sports_status; ?>/><span>Sports</span><br>
 <input class="radio" type="radio" name="game_category" value="strategy"<?php print $strategy_status; ?>/><span>Strategy</span><br>
 <input class="radio" type="radio" name="game_category" value="stunt"<?php print $stunt_status; ?>/><span>Stunt</span><br>
 <input class="radio" type="radio" name="game_category" value="top_rated"<?php print $top_rated_status; ?>/><span>Top Rated</span><br>
 <input class="radio" type="radio" name="game_category" value="words"<?php print $words_status; ?>/><span>Words</span><br>
 <input class="radio" type="radio" name="game_category" value="zombies"<?php print $zombies_status; ?>/><span>Zombies</span><br></td>
</tr>

<tr>
 <td id="insert_cat_name">Game keywords:</td>
 <td><textarea id="insert_text_area" name="game_keywords"></textarea></td>
</tr>

<tr>
 <td id="insert_cat_name">Game image:</td>
 <td><input type="file" name="game_image"></td>
</tr>

<tr>
 <td id="insert_cat_name">Game HTML:</td>
 <td><input type="file" name="game_code"></td>
</tr>

<tr>
 <td id="insert_cat_name">Game Flash File:</td>
 <td><input type="file" name="game_file"></td>
</tr>

<tr>
 <td id="insert_cat_name">Game description:</td>
 <td><textarea id="insert_text_area" name="game_desc"></textarea></td>
</tr>

<tr>
 <td colspan="2" align="center"><input type="submit" name="submit" value="Publish Game Now" id="insert_submit"></td>
</tr>

</form>

</body>
</html>

任何?

编辑:在解决这个问题的过程中,我发现代码中出现了错误,我的网站中有更多“动作”和“体育”类别,我忘了在这行添加更多类别“$action_status = 'unchecked';” ,但由于某种原因,我没有任何错误,一切都很完美...奇怪的Oo

3 个答案:

答案 0 :(得分:2)

如果您要做的只是用CHECKBOXES替换您的RADIO按钮,只需更改HTML即可反映出来:

 <input class="radio" type="checkbox" name="game_category[]" value="action"<?php print $action_status; ?>/><span>Action</span><br/>

然后,您需要将PHP中的所有&#34;否则更改为&#34;如果&#34;&#34;&#34; s,那么您可以迭代多个价值观,如下:

if($game_category == 'multiplayer'){
     $multiplayer_status = 'checked';
}
if($game_category == 'new'){
    $new_status = 'checked';
}

答案 1 :(得分:2)

解决方案:

我改变了这一行,例如:

<input class="radio" type="radio" name="game_category" value="3d"<?php print $three_d_status; ?>/><span>3D</span><br>

进入这个:

<input class="checkbox" type="checkbox" name="game_category[]" value="3d"<?php print $three_d_status; ?>/><span>3D</span><br>

然后,我编辑了if语句:

if($game_category == '3d'){

 $three_d_status = 'checked';

 }else if($game_category == 'action'){

 $action_status = 'checked';

 }

进入这个:

if($game_category == '3d'){

 $three_d_status = 'checked';

 }
 if($game_category == 'action'){

 $action_status = 'checked';

 }

最后我改变了这一行:

$game_category = $_POST['game_category'];

进入这个:

$game_category = implode(",",$_POST['game_category']);

希望帮助那里的人:)

归功于:"binaryatrocity""Fred-ii-":D

答案 2 :(得分:0)

如果你只有一个,那么NAME元素可能必须是一个数组:

Checkbox:<input type="checkbox" checked="checked" name="myCheck[]">