NuMan Posted March 28, 2009 Share Posted March 28, 2009 Hello, Im trying to insert some information into my database from checkboxes, but my $_POST does not work. This is what I am using: $checkbox = $_POST['checkbox[]']; Is this the correct way of doing this? Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/151531-checkbox-with-_post/ Share on other sites More sharing options...
sgt.virus Posted March 28, 2009 Share Posted March 28, 2009 Hi, This is a copy from my site that I use, but pretty much looks like yours. <input type="checkbox" name="confirm"> ... blah blah ... $checkbox = $_POST["confirm"] Quote Link to comment https://forums.phpfreaks.com/topic/151531-checkbox-with-_post/#findComment-795871 Share on other sites More sharing options...
redarrow Posted March 28, 2009 Share Posted March 28, 2009 Can i see the form please, it seems your using arrays are you.. example if it arrays <?php foreach($_POST'array_name'] as $result){ echo $result; } ?> or another array way. <?php $result=implode(' ',$_POST['array_name']); echo $result; ?> Quote Link to comment https://forums.phpfreaks.com/topic/151531-checkbox-with-_post/#findComment-795873 Share on other sites More sharing options...
NuMan Posted March 28, 2009 Author Share Posted March 28, 2009 Hi, This is a copy from my site that I use, but pretty much looks like yours. <input type="checkbox" name="confirm"> ... blah blah ... $checkbox = $_POST["confirm"] This is my input: <input type=checkbox name=chekbox[] value={$checkboxinfo[i].id}> which works pretty good to show the information. I tried what you said but it didn't work. Can i see the form please, it seems your using arrays are you.. example if it arrays <?php foreach($_POST'array_name'] as $result){ echo $result; } ?> or another array way. <?php $result=implode(' ',$_POST['array_name']); echo $result; ?> Could you explain a little more please? I gave more information in this post about my problem. Quote Link to comment https://forums.phpfreaks.com/topic/151531-checkbox-with-_post/#findComment-795877 Share on other sites More sharing options...
wildteen88 Posted March 28, 2009 Share Posted March 28, 2009 If you have named you checkbox as checkbox[] Then use $_POST['checkbox'][0] - first checbox $_POST['checkbox'][1] - secound checbox $_POST['checkbox'][2] - third checbox etc. You can easily loop through your checkboxes too, foreac($_POST['checkbox'] as $cbox) { echo $chox; } Quote Link to comment https://forums.phpfreaks.com/topic/151531-checkbox-with-_post/#findComment-795878 Share on other sites More sharing options...
NuMan Posted March 28, 2009 Author Share Posted March 28, 2009 If you have named you checkbox as checkbox[] Then use $_POST['checkbox'][0] - first checbox $_POST['checkbox'][1] - secound checbox $_POST['checkbox'][2] - third checbox etc. You can easily loop through your checkboxes too, foreac($_POST['checkbox'] as $cbox) { echo $chox; } Oh, is there anyway i can do $checkbox = the loop? of it? sorry im not very good i this Quote Link to comment https://forums.phpfreaks.com/topic/151531-checkbox-with-_post/#findComment-795899 Share on other sites More sharing options...
NuMan Posted March 28, 2009 Author Share Posted March 28, 2009 by the way, each one of the checkboxes represent a number so im wondering if there's anyway to count up to the number of the one thats checked and record that number. as a variable? Quote Link to comment https://forums.phpfreaks.com/topic/151531-checkbox-with-_post/#findComment-795921 Share on other sites More sharing options...
wildteen88 Posted March 28, 2009 Share Posted March 28, 2009 Post an example of your form here. Not quite understanding you fully from what you said above. Quote Link to comment https://forums.phpfreaks.com/topic/151531-checkbox-with-_post/#findComment-795937 Share on other sites More sharing options...
NuMan Posted March 28, 2009 Author Share Posted March 28, 2009 Post an example of your form here. Not quite understanding you fully from what you said above. <input type=checkbox name=chlist[] value={$chinfo.id}>{$chinfo.ch} Its part of a script. It shows the channels that are in a database. Quote Link to comment https://forums.phpfreaks.com/topic/151531-checkbox-with-_post/#findComment-795939 Share on other sites More sharing options...
wildteen88 Posted March 28, 2009 Share Posted March 28, 2009 When the form is submitted what do you want to do with the checkboxes? Quote Link to comment https://forums.phpfreaks.com/topic/151531-checkbox-with-_post/#findComment-795942 Share on other sites More sharing options...
NuMan Posted March 28, 2009 Author Share Posted March 28, 2009 i just want it to be recorded into the databased. Quote Link to comment https://forums.phpfreaks.com/topic/151531-checkbox-with-_post/#findComment-796015 Share on other sites More sharing options...
wildteen88 Posted March 28, 2009 Share Posted March 28, 2009 Then use // loop through the checkboxes foreach($_POST['checkbox'] as $values) { // inset each one into database $sql = "INSERT INTO your_table SET your_field='$value'"; mysql_query($sql) or die('Query Error: ' .$sql.'<br />'.mysql_error()); } Quote Link to comment https://forums.phpfreaks.com/topic/151531-checkbox-with-_post/#findComment-796016 Share on other sites More sharing options...
NuMan Posted March 29, 2009 Author Share Posted March 29, 2009 Then use // loop through the checkboxes foreach($_POST['checkbox'] as $values) { // inset each one into database $sql = "INSERT INTO your_table SET your_field='$value'"; mysql_query($sql) or die('Query Error: ' .$sql.'<br />'.mysql_error()); } Hello, I am trying a more simpler way now without databases. This is what i have: if($_POST['Reggaeton']) { $checkbox = 1; } elseif($_POST['Merengue']) { $checkbox = 2; } elseif($_POST['Dominican Hip-Hop']) { $checkbox = 3; } elseif($_POST['Rap']) { $checkbox = 4; } elseif($_POST['Rock']) { $checkbox = 5; } And this is my form: <input type="checkbox" name="Reggaeton" value="Reggaeton">Reggaeton<br> <input type="checkbox" name="Merengue" value="Merengue">Merengue<br> <input type="checkbox" name="Dominican Hip-Hop" value="Dominican Hip-Hop">Dominican Hip-Hop<br> <input type="checkbox" name="Rap" value="Rap">Rap<br> <input type="checkbox" name="Rock" value="Rock">Rock<br> It is still not working. Any help. Quote Link to comment https://forums.phpfreaks.com/topic/151531-checkbox-with-_post/#findComment-796329 Share on other sites More sharing options...
wildteen88 Posted March 29, 2009 Share Posted March 29, 2009 The code you posted does not make sense. If you're user selected Hip-Hop and Rap only this part of your if statement will execute if($_POST['Reggaeton']) { $checkbox = 1; } elseif($_POST['Merengue']) { $checkbox = 2; } elseif($_POST['Dominican Hip-Hop']) { $checkbox = 3; } elseif($_POST['Rap']) { $checkbox = 4; } elseif($_POST['Rock']) { $checkbox = 5; } Quote Link to comment https://forums.phpfreaks.com/topic/151531-checkbox-with-_post/#findComment-796335 Share on other sites More sharing options...
NuMan Posted March 29, 2009 Author Share Posted March 29, 2009 The code you posted does not make sense. If you're user selected Hip-Hop and Rap only this part of your if statement will execute if($_POST['Reggaeton']) { $checkbox = 1; } elseif($_POST['Merengue']) { $checkbox = 2; } elseif($_POST['Dominican Hip-Hop']) { $checkbox = 3; } elseif($_POST['Rap']) { $checkbox = 4; } elseif($_POST['Rock']) { $checkbox = 5; } Yes, thats what i want it to do. If they select hip hop i want the value of $checkbox to be 3 or if they select rap i want it to be 4. Oh yeah and they can only select 1 genre i have a check for it. Maybe ill change it to radios. Quote Link to comment https://forums.phpfreaks.com/topic/151531-checkbox-with-_post/#findComment-796338 Share on other sites More sharing options...
wildteen88 Posted March 29, 2009 Share Posted March 29, 2009 So you want one selection to be made. if so you should use radio buttons instead rather than checkboxes. <input type="radio" name="genre" value="Reggaeton">Reggaeton<br> <input type="radio" name="genre" value="Merengue">Merengue<br> <input type="radio" name="genre" value="Dominican Hip-Hop">Dominican Hip-Hop<br> <input type="radio" name="genre" value="Rap">Rap<br> <input type="radrio" name="genre" value="Rock">Rock<br> Now use $_POST['genre'] to get the selection. Quote Link to comment https://forums.phpfreaks.com/topic/151531-checkbox-with-_post/#findComment-796347 Share on other sites More sharing options...
NuMan Posted March 29, 2009 Author Share Posted March 29, 2009 So you want one selection to be made. if so you should use radio buttons instead rather than checkboxes. <input type="radio" name="genre" value="Reggaeton">Reggaeton<br> <input type="radio" name="genre" value="Merengue">Merengue<br> <input type="radio" name="genre" value="Dominican Hip-Hop">Dominican Hip-Hop<br> <input type="radio" name="genre" value="Rap">Rap<br> <input type="radrio" name="genre" value="Rock">Rock<br> Now use $_POST['genre'] to get the selection. tried this with this and didn't work.: if($_POST['genre'] == Reggaeton) { $radio = 1; } elseif($_POST['genre'] == Merengue) { $radio = 2; } elseif($_POST['genre'] == DominicanHipHop) { $radio = 3; } elseif($_POST['genre'] == Rap) { $radio = 4; } elseif($_POST['genre'] == Rock) { $radio = 5; } Im sorry for bothering so much. Quote Link to comment https://forums.phpfreaks.com/topic/151531-checkbox-with-_post/#findComment-796357 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.