Limon Limon - 1 month ago 6
PHP Question

passing checkbox data to php and then to a query

I have multiple checkboxes so i created a 2D array to store those inputs.

I should make a query with all this information, but i can't figure out how to handle this 2D array in order to get the data from the Data Base. Conditions for the query can be variable as the parameters are variable, so i am really confused..

How should i handle the checkbox data to send it all together to the query?

this is the view:

<div class="control-group">
<label class="control-label">Tipo de Socio</label>
<div class="controls">
<label class="checkbox line">
<input type="checkbox" id="registered" value="2" name="type[]" /> Registrado
</label>
<label class="checkbox line">
<input type="checkbox" value="1" name="type[]"/> Pre-Registrado
</label>
</div>
</div>
<div class="hide" id="content">
<div class="control-group">
<label class="control-label">Estado del Socio</label>
<div class="controls">
<label class="checkbox line">
<input type="checkbox" value="1" name="act[]" checked /> Activo
</label>
<label class="checkbox line">
<input type="checkbox" value="0" name="act[]" checked/> No Activo
</label>
</div>
</div>
</div>


and this is my admin.php:

public function formReport()
{
// var_dump($_POST);die();
$conditions = array();

if(isset($_POST['type']))
{
******should ask here about the fields that comes from the checkboxes and prepare data for the query*******
}
}

Answer

I hope I have understood your question, and what you have to do is the following.

First of all in my local code I have modified the values of your checkboxes and I have the following code:

<div class="control-group">
    <label class="control-label">Tipo de Socio</label>
    <div class="controls">
        <label class="checkbox line">
            <input type="checkbox" id="registered" value="registrado" name="type[]" /> Registrado
        </label>
        <label class="checkbox line">
            <input type="checkbox" value="pre_registrado" name="type[]"/> Pre-Registrado
        </label>
    </div>
</div>
<div class="hide" id="content">
    <div class="control-group">
        <label class="control-label">Estado del Socio</label>
        <div class="controls">
            <label class="checkbox line">
                <input type="checkbox" value="activo" name="act[]" checked /> Activo
            </label>
            <label class="checkbox line">
                <input type="checkbox" value="no_activo" name="act[]" checked/> No Activo
            </label>
        </div>
    </div>
</div>

First of all If I check all the checkboes, and then submit the form, the $_POST variable contains the following code:

Array
(
    [type] => Array
        (
            [0] => registrado
            [1] => pre_registrado
        )

    [act] => Array
        (
            [0] => activo
            [1] => no_activo
        )

)

If I submit the form with no checkboxes checked the result is the following:

Array
(
)

So the best solution is to test if the values are into array. For example:

public function formReport()
{
    // var_dump($_POST);die();
    $conditions    =    array();

    // Do some validation first
    if(empty($_POST))
    {
        //    There is no check box selected
    }
    elseif(!isset($_POST['type'] || empty($_POST['type']))
    {
        //    User didn't checked any type
    }
    elseif(!isset($_POST['act'] || empty($_POST['act']))
    {
        //    User didn't checked any Estato
    }
    else
    {
        $registrado     = (isset($_POST['type']['registrado']) ? 1 : 0;
        $preRegistrado  = (isset($_POST['type']['pre_registrado']) ? 1 : 0;
        $activo         = (isset($_POST['act']['activo']) ? 1 : 0;
        $noActivo       = (isset($_POST['act']['no_activo']) ? 1 : 0;

        // In this state the variables above have the value 1, if the
        // end user has checked the appropriate checkbox, and the value 0
        // if the end user has not checked the appropriate checkbox

        // In this place now you can use the above variables to perform
        // any logical operation you like, and/or insert the values
        // in your database via a simple INSERT query.
    }
}
Comments