Incomplentence
Incomplentence

Reputation: 27

Php not receiving data from form

I have a excerpt of my form, as follows:

// If logged in shows this box (it's backed behind a include php file that makes the login check

<form action="./saveList.php" method="post" id="ListaComprasForm">
<div class="submitButtonEncap">
  <button type="submit" method="POST" name="submitButton" class="btn btn-primary roundedBorder" form="ListaComprasForm" value="submit">
      <span class="glyphicon glyphicon-floppy-disk" aria-hidden="true"></span> Save List
  </button>
</div>

<input type="text" id="Limpeza0" name="Limpeza[]" class="listInput" autocomplete="off" maxlength="50">
<input type="checkbox" class="x" id="L_chck_0" name="L_chckb_[]" value="check"/>

<input type="text" id="Limpeza1" name="Limpeza[]" class="listInput" autocomplete="off" maxlength="50">
<input type="checkbox" class="x" id="L_chck_1" name="L_chckb_[]" value="check"/>

<input type="text" id="Limpeza2" name="Limpeza[]" class="listInput" autocomplete="off" maxlength="50">
<input type="checkbox" class="x" id="L_chck_2" name="L_chckb_[]" value="check"/>

<input type="text" id="Limpeza3" name="Limpeza[]" class="listInput" autocomplete="off" maxlength="50">
<input type="checkbox" class="x" id="L_chck_3" name="L_chckb_[]" value="check"/>

<input type="text" id="Limpeza4" name="Limpeza[]" class="listInput" autocomplete="off" maxlength="50">
<input type="checkbox" class="x" id="L_chck_4" name="L_chckb_[]" value="check"/>

<input type="text" id="Limpeza5" name="Limpeza[]" class="listInput" autocomplete="off" maxlength="50">
<input type="checkbox" class="x" id="L_chck_5" name="L_chckb_[]" value="check"/>

<input type="text" id="Limpeza6" name="Limpeza[]" class="listInput" autocomplete="off" maxlength="50">
<input type="checkbox" class="x" id="L_chck_6" name="L_chckb_[]" value="check"/>

</form>

This form, on submit, will submit to a DB wether or not checkboxes and TextBox fields have been filled or not. The checkboxes that have been checked or not, he'll submit them over as Y and N or nullo for the Textboxes respectively through some sorting code as follows:

if (isset($_POST['submitButton'])){

    $L_chck[] = array();
    $H_chck[] = array();
    $V_chck[] = array();
    $F_chck[] = array();
    $Cong_chck[] = array();
    $Cons_chck[] = array();
    $Outros_chck[] = array();

    $Outros = array();

    for ($counter = 0; $counter <= 6; $counter++){
        $L_chck[$counter] = (isset($_POST['L_checkb_'.$counter]) ? 'Y' : 'N');
        $H_chck[$counter] = (isset($_POST['H_checkb_'.$counter]) ? 'Y' : 'N');
        $V_chck[$counter] = (isset($_POST['V_chckb_'.$counter]) ? 'Y' : 'N');
        $F_chck[$counter] = (isset($_POST['F_chckb_'.$counter]) ? 'Y' : 'N');
        $Cong_chck[$counter] = (isset($_POST['Cong_chckb_'.$counter]) ? 'Y' : 'N');
        $Cons_chck[$counter] = (isset($_POST['Cons_chckb_'.$counter]) ? 'Y' : 'N');
        $Outros_chck[$counter] = (isset($_POST['Outros_chckb_'.$counter]) ? 'Y' : 'N');

    if (empty($_POST['Outros'.$counter])) {
            $Outros[$counter] = $_POST['Outros'.$counter] = 'nullo';
        } else {
        $Outros[$counter] = $_POST['Outros'.$counter];
        }

    }


for ($i=0; $i <= 4; $i++) { 
        /*if ((isset($_POST['Limpeza'.$i])) == false) {*/
        if (empty($_POST['Limpeza'.$i])) {
            $Limpeza[$i] = $_POST['Limpeza'.$i] = 'nullo';
        } else {
            $Limpeza[$i] = $_POST['Limpeza'.$i];
        }

        if (empty($_POST['Higiene'.$i])) {
            $Higiene[$i] = $_POST['Higiene'.$i] = 'nullo';
        } else {
            $Higiene[$i] = $_POST['Higiene'.$i];
        }

        if (empty($_POST['Vegetais'.$i])) {
            $Vegetais[$i] = $_POST['Vegetais'.$i] = 'nullo';
        } else {
            $Vegetais[$i] = $_POST['Vegetais'.$i];
        }

        if (empty($_POST['Fruta'.$i])) {
            $Fruta[$i] = $_POST['Fruta'.$i] = 'nullo';
        } else {
            $Fruta[$i] = $_POST['Fruta'.$i];
        }

        if (empty($_POST['Congelados'.$i])) {
            $Congelados[$i] = $_POST['Congelados'.$i] = 'nullo';
        } else {
            $Congelados[$i] = $_POST['Congelados'.$i];
        }

        if (empty($_POST['Conservas'.$i])) {
            $Conservas[$i] = $_POST['Conservas'.$i] = 'nullo';
        } else {
            $Conservas[$i] = $_POST['Conservas'.$i];
        }
    }

This data is then passed on as arguments to a function to be serialized and sent to DB fields, all of them varchar(255):

 savingListData($L_chck, $H_chck, $V_chck, $F_chck, $Cong_chck, $Cons_chck, $Outros_chck, $Limpeza, $Higiene, $Vegetais, $Fruta, $Congelados, $Conservas, $Outros);

So far, every piece of data, being it checked/filled or not, it transposes over to the DB as null. In the case of the checkboxes, N, and in the case of the Textboxes nullo.

I'm led to believe that something is wrong in the sorting but I'm puzzled and can't figure out why. Would really appreciate some help!!

EDIT: Added more of the form in a shortened manner. (It's a big form!)

Upvotes: 0

Views: 64

Answers (1)

Himanshu Upadhyay
Himanshu Upadhyay

Reputation: 6565

I think you are trying to retrieving it using wrong name. Its L_chckb_[]

<input type="checkbox" class="x" id="L_chck_0" name="L_chckb_[]" value="check"/>

So you need to use the loop counter outside the name key like this:

$L_chck[$counter] = (isset($_POST['L_chckb_'][$counter]) ? 'Y' : 'N');

The problem is: the name attribute in the tag is L_chckb_ but you are trying to retrieve it by L_checkb_ so that is the issue.

Because L_checkb_ is not set and so it is giving you N.

Upvotes: 1

Related Questions