Jump to content

Checkbox with $_POST


NuMan

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/151531-checkbox-with-_post/#findComment-795877
Share on other sites

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;
}

 

 

Link to comment
https://forums.phpfreaks.com/topic/151531-checkbox-with-_post/#findComment-795878
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/151531-checkbox-with-_post/#findComment-795899
Share on other sites

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());
}

Link to comment
https://forums.phpfreaks.com/topic/151531-checkbox-with-_post/#findComment-796016
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/151531-checkbox-with-_post/#findComment-796329
Share on other sites

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;

}

Link to comment
https://forums.phpfreaks.com/topic/151531-checkbox-with-_post/#findComment-796335
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/151531-checkbox-with-_post/#findComment-796338
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/151531-checkbox-with-_post/#findComment-796347
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/151531-checkbox-with-_post/#findComment-796357
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.