1

I was wondering how to convert my radio buttons into checkboxes because I want to insert multiple options for the same record... for example: I want a game to be in the "Strategy" and "Action" category, so only a checkbox can make me achieve that.

here is my code for inserting a game with radio buttons:

<?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>

Anyone?

EDIT: While solving this problem I found an error in my code, I have much more then "action" and "sports" categories in my website, I forgot to add more categories to this lines "$action_status = 'unchecked';", but for some reason I didn't got any errors, and everything worked perfectly... strange O.o

Jonas
  • 121,568
  • 97
  • 310
  • 388
davidgpilot
  • 69
  • 1
  • 2
  • 10
  • Convert?.. wha? automatically? `preg_replace()` maybe. Or "search and replace" in your favorite editor. ;-) – Funk Forty Niner Feb 18 '14 at 18:47
  • What's stopping you from just editing the html to make them checkboxes? – Skrivener Feb 18 '14 at 18:48
  • 1
    Here's a quick method to change them "on-the-fly"*as it were*. First create a variable called `$choices` then assign it to, say `$choices="checkbox";` then in your `type="radio"` you could just do `type=""` and you could change it back to `$choices="radio";` anytime afterwards, avoiding to do all the grunt work. All this "from the top of my head". ;-) – Funk Forty Niner Feb 18 '14 at 18:51
  • doesn't a simple `` work? – charlee Feb 18 '14 at 18:51
  • Its not the problem to change the HTML, the problem is that I tried it once, and I got only 1 record insteed of multi for the same game... any idea? – davidgpilot Feb 18 '14 at 18:52
  • Then you need to use `name="game_category[]"` in brackets in conjunction with the checkbox option; that's why you were only getting the "one". @davidgpilot – Funk Forty Niner Feb 18 '14 at 18:54
  • You can either do a search & replace or you could do it "my way" so you won't have to go over the trouble all over again, then make another variable with `$game_cat="game_category[]";` and echo it the same way I posted in another comment. Theoretically, this one should work also. @davidgpilot - All in the name of "speed" ;-) – Funk Forty Niner Feb 18 '14 at 19:00
  • You're welcome. And this will also be required `foreach($_POST['game_category'] as $value)` etc. So you have a bit more work ahead of you. @davidgpilot Taken/borrowed from http://stackoverflow.com/a/18469405/ – Funk Forty Niner Feb 18 '14 at 19:04
  • Oh god... thats really painfull :D – davidgpilot Feb 18 '14 at 19:09
  • *"but for some reason I didn't got any errors, and everything worked perfectly... strange"* - Hey, if it works ;-) – Funk Forty Niner Feb 18 '14 at 19:09
  • *"Oh god... thats really painfull"* - Yeah, nobody said any of this would be easy ;-) Possible, yes. Easy, no. – Funk Forty Niner Feb 18 '14 at 19:10
  • 1
    I mean before I wanted to change to checkboxes, I used the radio buttons with only this 2 categories on this "unchecked" variable, and for some reason all the categories added successfully to my database... thats strange XD – davidgpilot Feb 18 '14 at 19:11

3 Answers3

2

If all you are looking to do is replace your RADIO buttons with CHECKBOXES, just change the HTML to reflect that, like so:

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

You will then need to change all the "else if"'s in your PHP to just "if"'s, as you can iterate over multiple values, like so:

if($game_category == 'multiplayer'){
     $multiplayer_status = 'checked';
}
if($game_category == 'new'){
    $new_status = 'checked';
}
binaryatrocity
  • 956
  • 5
  • 10
  • Thanks I will try that, if it will work I will post the full code at the answer :) – davidgpilot Feb 18 '14 at 18:55
  • I've edited my comment. You'll need to change your "else if"'s at the top in PHP to allow multiple values to be "checked" at once. – binaryatrocity Feb 18 '14 at 18:59
  • This didn't work, I tried this and I got in my database only the last category that I've checked... The problem is that I don't know how to make my website read the same game in multi categories... – davidgpilot Feb 18 '14 at 19:22
  • Ah, beacuse we must declare game_category as an array, I updated the answer. `` - now you will have $_POST['game_category'] as an array, with all values included. – binaryatrocity Feb 18 '14 at 19:30
  • Now, insteed of getting 4 categories that I've picked for the same game, I get in my database under "game_category" this: Array. – davidgpilot Feb 18 '14 at 19:34
  • Did you use [`this?`](http://stackoverflow.com/questions/21862693/convert-radio-button-into-a-checkbox#comment33098628_21862693) @davidgpilot – Funk Forty Niner Feb 18 '14 at 19:36
  • What do you mean by: 'this?' ? xD – davidgpilot Feb 18 '14 at 19:37
  • Yes, because $_POST['game_category'] is an array, containing all of the values therein. You will need to do a little extra work in your PHP before forming the $insert_query to test which values were checked. For a very easy method you can use PHP's implode method: `$game_keywords = implode(",", $_POST['game_keywords']);` this will create a comma-delimited string. – binaryatrocity Feb 18 '14 at 19:38
  • I'm very confused, because I already got a variable in my code named game_keywords for the search engine :( even when I tested and changed my "game_category" record in my database to: "action, space, strategy," I didn't seen the game after that in any of this categories... – davidgpilot Feb 18 '14 at 19:40
  • AAhhhhh good, `Another Happy Ending` - You're welcome @davidgpilot – Funk Forty Niner Feb 18 '14 at 19:48
  • Glad that worked out for you! Please accept the answer if everything is working properly. :D – binaryatrocity Feb 18 '14 at 19:49
  • I posted the full answer (a "changes answer" :D) and gave you guys credit and a +1 :) – davidgpilot Feb 18 '14 at 19:53
  • There is still a last problem, I still can't see the same game in different categories... its seems that the page can't read now what category it is... game_categories now has more then one value, therefore the page can't decide which one to use, he only knows for example: "strategy", he doesn't know: "strategy,action,space"... how to fix that? – davidgpilot Feb 18 '14 at 19:55
2

The solution:

I change this line for example from this:

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

into this:

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

Then, I edited the if statements from this:

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

 $three_d_status = 'checked';

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

 $action_status = 'checked';

 }

Into this:

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

 $three_d_status = 'checked';

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

 $action_status = 'checked';

 }

and finally I changed this line from this:

$game_category = $_POST['game_category'];

Into this:

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

Hope that helped someone out there :)

Credit goes to: "binaryatrocity" and "Fred-ii-" :D

Community
  • 1
  • 1
davidgpilot
  • 69
  • 1
  • 2
  • 10
0

If you got only one, maybe the NAME element must be an array :

Checkbox:<input type="checkbox" checked="checked" name="myCheck[]">
Emilio Borraz
  • 516
  • 3
  • 17