Hello everyone , i just need to insert into my database the state of 4 checkbox , i'm trying now only for two . For this, i have two file formulaire.html and form.php , I've created for each checkbox a column in my database , so , what i want to get finaly is to know the state of each checkbox , if its checked in checkbox1 column i want find "yes" or "1" , if it's not checked "no" or "0" , and the same for all checkboxs .

this is the code for the form in html :

<form style="height: 943px;" method="post" action="form11111.php" name="monformulaire"><br>
  <label style="position: absolute; z-index: 10; top: 73px; left: 50px;">Nom
:</label> <input size="10" name="Nom" style="position: absolute; z-index: 10; top: 70px; left: 170px;">
  <input size="10" name="Prenom" style="position: absolute; z-index: 10; top: 70px; left: 417px;"><label style="position: absolute; z-index: 10; top: 73px; left: 330px;">Prénom
:</label>
  <input size="10" name="Fonction" style="position: absolute; z-index: 10; top: 110px; left: 417px;"><label style="position: absolute; z-index: 10; top: 113px; left: 330px;">Fonction
:</label>
  <input size="10" name="Datedemande" style="position: absolute; z-index: 10; top: 70px; left: 783px;"><label style="position: absolute; z-index: 10; top: 73px; left: 610px;"> Date
de la demande :</label> <label style="position: absolute; z-index: 10; top: 113px; left: 50px;">Département
:</label>
<input size="10" name="Departement" style="position: absolute; z-index: 10; top: 110px; left: 170px;">
<label style="position: absolute; z-index: 10; top: 603px; left: 50px;">Téléphone :</label> 
<input size="10" name="phone" style="position: absolute; z-index: 10; top: 600px; left: 160px;">
<label style="position: absolute; z-index: 10; top: 603px; left: 370px;">N1</label> 
<label style="position: absolute; z-index: 10; top: 603px; left: 470px;">N2</label> 
<input type="checkbox" name="boxes[]" value="" style="position: absolute; z-index: 10; top: 603px; left: 550px;" ></input>
<input type="checkbox" name="boxes[]" value="" style="position: absolute; z-index: 10; top: 603px; left: 450px;" ></input>
<input value="Envoyer" style="position: absolute; z-index: 10; top: 603px; left: 750px;" type="submit"><br>
</form>

And this is the code of php , with test in printings the state of checkboxs in the php web page form11111.php :

<!DOCTYPE html>
<html>
    <head><title>Example</title></head>
    <body>
       test checkbox
        <pre>
<?php
$db = mysql_connect('localhost', 'root', 'mysql');  
mysql_select_db('InfoPerso',$db);
 
// Recuperation des valeurs :
 
$Nom = $_POST['Nom'];
$Departement = $_POST['Departement'];
$Prenom = $_POST['Prenom'];
$Fonction = $_POST['Fonction'];
$Datedemande = $_POST['Datedemande'];
$phone = $_POST['phone'];
$_boxValue = $_POST['boxes'];


if(isset($_POST['boxes']))
{
    foreach ($_POST['boxes'] as $_boxValue)
    {
        echo "Box #{$_POST['boxes']} was selected!\n";
    }
}


            

echo "Box #{$phone} was selected!\n";                   
               
 
 
// Insertion des valeurs :
$insertion = "INSERT into Personnes (Nompersonne, NomDep, Prenompersonne, Fonction, DateDemande, Telephone, N1 ) values 
('$Nom', '$Departement','$Prenom','$Fonction','$Datedemande','$phone','$_boxValue')";
mysql_query($insertion) or die ('Error : '. mysql_error());



 
?>
</pre>
    </body>
</html>

Thank you very much for your answers .

First define values to checkboxes since values of the cliked checkboxes are returned in POST. And note HTML input tag does not have aclosing tag (see below).

<input type="checkbox" name="boxes[]" value="N1" style="position: absolute; z-index: 10; top: 603px; left: 550px;" />
<input type="checkbox" name="boxes[]" value="N2" style="position: absolute; z-index: 10; top: 603px; left: 450px;" />

Then check for values (in form11111.php) but first test if the POST exists and is not empty (if none of checkboxes were checked the array will be empty).

if(isset($_POST['boxes']) && !empty($_POST['boxes'])) {

    foreach ($_POST['boxes'] as $_boxValue) {
        
        // if($_boxValue == 'CB1')
        echo "Box #$_boxValue was selected!\n";
    }
    
} else {

    echo 'No boxes were selected.';
}

Hello Broj , thank you for answering , i did the modifications u talled me , but , still nothing inserted to database , and it print nothing the form11111.php page !!!
these are the two codes :

formulaire.html :

<form style="height: 943px;" method="post" action="form11111.php" name="monformulaire"><br>
  <label style="position: absolute; z-index: 10; top: 73px; left: 50px;">Nom
:</label> <input size="10" name="Nom" style="position: absolute; z-index: 10; top: 70px; left: 170px;">
  <input size="10" name="Prenom" style="position: absolute; z-index: 10; top: 70px; left: 417px;"><label style="position: absolute; z-index: 10; top: 73px; left: 330px;">Prénom
:</label>
  <input size="10" name="Fonction" style="position: absolute; z-index: 10; top: 110px; left: 417px;"><label style="position: absolute; z-index: 10; top: 113px; left: 330px;">Fonction
:</label>
  <input size="10" name="Datedemande" style="position: absolute; z-index: 10; top: 70px; left: 783px;"><label style="position: absolute; z-index: 10; top: 73px; left: 610px;"> Date
de la demande :</label> <label style="position: absolute; z-index: 10; top: 113px; left: 50px;">Département
:</label>
<input size="10" name="Departement" style="position: absolute; z-index: 10; top: 110px; left: 170px;">
<label style="position: absolute; z-index: 10; top: 603px; left: 50px;">Téléphone :</label> 
<input size="10" name="phone" style="position: absolute; z-index: 10; top: 600px; left: 160px;">
<label style="position: absolute; z-index: 10; top: 603px; left: 370px;">N1</label> 
<label style="position: absolute; z-index: 10; top: 603px; left: 470px;">N2</label> 
<input type="checkbox" name="boxes[]" value="N1" style="position: absolute; z-index: 10; top: 603px; left: 550px;" />
<input type="checkbox" name="boxes[]" value="N2" style="position: absolute; z-index: 10; top: 603px; left: 450px;" />
<input value="Envoyer" style="position: absolute; z-index: 10; top: 603px; left: 750px;" type="submit"><br>
</form>

form11111.php :

<!DOCTYPE html>
<html>
    <head><title>Example</title></head>
    <body>
       test checkbox
        <pre>
<?php
$db = mysql_connect('localhost', 'root', 'mysql');  
mysql_select_db('InfoPerso',$db);
 
// Recuperation des valeurs :
 
$Nom = $_POST['Nom'];
$Departement = $_POST['Departement'];
$Prenom = $_POST['Prenom'];
$Fonction = $_POST['Fonction'];
$Datedemande = $_POST['Datedemande'];
$phone = $_POST['phone'];
$_boxValue = $_POST['boxes']
echo "Box #$_boxValue was selected!\n";

if(isset($_POST['boxes']) && !empty($_POST['boxes'])) {

    foreach ($_POST['boxes'] as $_boxValue) {
        
        // if($_boxValue == 'CB1')
        echo "Box #$_boxValue was selected!\n";
    }
    
} else {

    echo 'No boxes were selected.';
}

            

echo "Box #{$phone} was selected!\n";                   
               
 
 
// Insertion des valeurs :
$insertion = "INSERT into Personnes (Nompersonne, NomDep, Prenompersonne, Fonction, DateDemande, Telephone, N1 ) values 
('$Nom', '$Departement','$Prenom','$Fonction','$Datedemande','$phone','$_POST['boxes']')";
mysql_query($insertion) or die ('Error : '. mysql_error());



 
?>
</pre>
    </body>
</html>

Thank you very much for helping me in this .

What is the type of the N1 field in the database? The $_POST is an array and you can not store arrays in database. If it is a string you have to convert it to a string (using implode() or serialize() functions).

if(isset($_POST['boxes']) && !empty($_POST['boxes'])) {

    // field to store in database
    $n_field = implode(',', $_POST['boxes']);

    foreach ($_POST['boxes'] as $_boxValue) {
 
        // if($_boxValue == 'CB1')
        echo "Box #$_boxValue was selected!\n";
    }
 
} else {

    // field to store in database is empta string
    $n_field = '';
 
    echo 'No boxes were selected.';
}
...

$insertion = "INSERT into Personnes (Nompersonne, NomDep, Prenompersonne, Fonction, DateDemande, Telephone, N1 ) values
('$Nom', '$Departement','$Prenom','$Fonction','$Datedemande','$phone','$n_field')";
mysql_query($insertion) or die ('Error : '. mysql_error());

the type of N1 and N2 in database is Varchar

But It don't work always , i can't know what is the source of the problem

OK (I noticed you stated this in your first post :-).

In this case you have to test for a value of each checkbox whether it is set or not.

// set default values for field values to 0 (do this for as many checkboxes you need)
$n1 = 0;
$n2 = 0;


if(isset($_POST['boxes']) && !empty($_POST['boxes'])) {
 
    foreach ($_POST['boxes'] as $_boxValue) {
 
        switch($_boxValue) {

            // do a check for all checkboxes
            case 'N1' : $n1 = 1; break;
            case 'N2' : $n2 = 1; break;
        }

        echo "Box #$_boxValue was selected!\n";
    }
} 

$insertion = "INSERT into Personnes 
    (Nompersonne, NomDep, Prenompersonne, Fonction, DateDemande, Telephone, N1, N2)
values
    ('$Nom', '$Departement','$Prenom','$Fonction','$Datedemande','$phone','$n1', '$n2')";

mysql_query($insertion) or die ('Error : '. mysql_error());

You can use values Yes and No instead of 1 and 0 but later require less storage than former. Varchar is a bit of waste for these kind of values. Use tinyint (for 0 and 1), char(3) (for Yes No) or ENUM type.

Hii Broj1 , thank you very much :) , finaly it works , but when i check for exemple the two checkboxs , in the page : form11111.php it is printed only N2 , ah , its don't work for N1 , even if i choose N1 checkbox only :(

Sorry I fixed only the database part. Echo statements have to be fixed too.

// set default values for field values to 0 (do this for as many checkboxes you need)
$n1 = 0;
$n2 = 0;

// set default value for string to be displayed
$cb_display = '';
 
if(isset($_POST['boxes']) && !empty($_POST['boxes'])) {
 
    foreach ($_POST['boxes'] as $_boxValue) {
 
        switch($_boxValue) {
 
            // do a check for all checkboxes
            case 'N1' : $n1 = 1; break;
            case 'N2' : $n2 = 1; break;
        }
 
        // add a checkbox value to display string
        $cb_display .= ' $_boxValue';
    }

    echo "The following boxes were selected:$cb_display.";

} else {

    echo 'Nothing was selected!';
}

Its me who is sorry , thank you very much sir , bur it still don't work , with this code of form.php :

<!DOCTYPE html>
<html>
    <head><title>Example</title></head>
    <body>
       test checkbox
        <pre>
<?php
$db = mysql_connect('localhost', 'root', 'mysql');  
mysql_select_db('InfoPerso',$db);
 
// Recuperation des valeurs :
 
$Nom = $_POST['Nom'];
$Departement = $_POST['Departement'];
$Prenom = $_POST['Prenom'];
$Fonction = $_POST['Fonction'];
$Datedemande = $_POST['Datedemande'];
$phone = $_POST['phone'];


// set default values for field values to 0 (do this for as many checkboxes you need)
$n1 = 0;
$n2 = 0;

// set default value for string to be displayed
$cb_display = '';
 
if(isset($_POST['boxes']) && !empty($_POST['boxes'])) {
 
    foreach ($_POST['boxes'] as $_boxValue) {
 
        switch($_boxValue) {
 
            // do a check for all checkboxes
            case 'N1' : $n1 = 1; break;
            case 'N2' : $n2 = 1; break;
        }
 
        // add a checkbox value to display string
        $cb_display .= ' $_boxValue';
    }

    echo "The following boxes were selected:$cb_display.";

} else {

    echo 'Nothing was selected!';
}

$insertion = "INSERT into Personnes 
    (Nompersonne, NomDep, Prenompersonne, Fonction, DateDemande, Telephone, N1, N2)
values
    ('$Nom', '$Departement','$Prenom','$Fonction','$Datedemande','$phone','$n1', '$n2')";

mysql_query($insertion) or die ('Error : '. mysql_error());


 
?>
</pre>
</body>
</html>

in the page when i select the two checkboxs and press the button , in the page form.php there is :

The following boxes were selected: $_boxValue.

, and it add 1 for N2 and 0 for N1 in the database .
And if i select only N1 in form.php there is :

Nothing was selected!

Sorry, a typo. Change single quotes to double quotes on line 40.

$cb_display .= " $_boxValue";

I am working on the other issue.

and it add 1 for N2 and 0 for N1 in the database

The reason for inserting wrong values is in positioning your labels. Label for N1 is positioned to N2 and vice versa.Try without styles. I don't think using of styles is correct the way you do it. Use divs instead and style them accordingly.

I don't know how to do this , i'm just new in web-devellopemment , and i liked to place my objects( labels and inputs ) with (x= and y=) , so can u help me how to deal with that plz ?
there is still be a problem with N1 when it is selected alone , it print : "Nothing was selected!" !!!

In other way ; how could I select a place (position) with x= and y= for all the form ?

I am not an expert in css positioning. It is quite an art and it takes some time to master it (at least with me). I usually find an example on internet and adopt it to suit my needs. From my experience positioning is usually done with elements placing in divs which are just boxes (can be floated) and divs applied appropriate css attributes. And I don't think labels need much positioning. They are most useful if they stick with the element they belong to.

Concerning CSS it is better to ask on Web design formum on Daniweb. And it is good idea to get basics first from various articles on the web or in books. As I said I am happy to help with PHP code but concernig CSS I am more or less useless.

Ok , thank you very much for your help and ideas , i just will do some tests now , and tell you where i have problem .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.