Retrieving values ​​stored in multiple arrays. Checkboxes enabled

Heloo,

I have the following PHP code:

 <?php       
$ratings = array(
            1 => "All",
            2 => "Love", 
            3 => "Hate",
            4 => "Maybe",
            5 => "Super"
);

$question = array(
            1 => "Pizza",
            2 => "Prajitura",
            3 => "Placinta",

);


 echo '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">';
  echo '<p>How do you feel about each topic?</p>';
foreach ($question as $key=>$value){
    echo "".$value."<br>";
    echo "aici are valoarea".$key;
    $importHtml="";
    foreach ($ratings as $cheie => $raspuns) {
         //   echo "cheie: ".$key."cheie raspuns:".$cheie."raspuns".$raspuns."\n";
            $importHtml .= "$raspuns <input type='checkbox' name='i_".$key."_importance[] id='$raspuns' value='$cheie' />";
}
echo "". $importHtml."<br>\n";
}

 echo '<input type="submit" value="Save Questionnaire" name="submit" />';
  echo '</form>';

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


foreach ($question as $key => $value) {
    //if(isset( $_REQUEST["i_".$key."_importance"]))
    $print="";
     $importance = $_REQUEST["i_".$key."_importance"];
     //var_dump($importance);
     echo "cheie >".$key."<br>\n";
    foreach($importance as $cheie=>$valoare){
        $print .="cheie".$cheie."valoare =".$valoare;
        //echo "cheie".$cheie."valoare =".$valoare;
        echo "<br>\n";
    }
    echo $print;
}
    }

    ?>

And I want to get the values ​​stored in each array named i_ $ key_importance using a foreach loop or a for loop. The result is not what I expected. In i_ $ key_importance I want to save the value 0 if the client did not check the box and the key value from the evaluations of the associative array, if this was done.

Expected Result:

 $importance = $_REQUEST["i_".$key."_importance"];
$importance will be an array containing {
1=> 1,
2=> 0 (here the user hasn't selected an option),
3=> 3,
4=> 0 (here the user hasn't selected an option),
}

the result will be saved in the table with the key Id = and the corresponding column for each answer will contain 0 (if the client did not select the answer) or number (if the answer was chosen).

Any help is greatly appreciated

+4
source share
1 answer

Perhaps you should consider the tag instead of the usual checkboxes.

, , , , , , , .

0

All Articles