简体   繁体   中英

Getting values stored in mutiple arrays. Checkboxes involved

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 wish to retrieve values stored into every array named i_$key_importance using a foreach loop or a for loop. The result is not as I expected to. In the i_$key_importance i want to store 0 value if the client has not checked the box and the key value from associative array ratings if has done so.

The expected outcome:

 $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 stored in an table with Id = key and corresponding column for every answer will contain 0 (if client hasn't selected an answer) or a number (if has selected an answer).

Any help great appreciation,

You should maybe consider a tag instead of the regular checkboxes.

In order to do what you want to do with the checkboxes, you can just test if the value returned by the checkbox is equal to the value linked to it, if not that means the user didn't check it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM