Jump to content

Checkbox with $_POST


NuMan

Recommended Posts

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.

Link to comment
Share on other sites

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;

?>

Link to comment
Share on other sites

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
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
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
Share on other sites

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.

Link to comment
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
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
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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.