dongwen9975 2015-06-01 09:13
浏览 50

获取存储在多个数组中的值。 复选框涉及

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."
";
            $importHtml .= "$raspuns <input type='checkbox' name='i_".$key."_importance[] id='$raspuns' value='$cheie' />";
}
echo "". $importHtml."<br>
";
}

 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>
";
    foreach($importance as $cheie=>$valoare){
        $print .="cheie".$cheie."valoare =".$valoare;
        //echo "cheie".$cheie."valoare =".$valoare;
        echo "<br>
";
    }
    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,

  • 写回答

1条回答 默认 最新

  • duangou2028 2015-06-01 09:26
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度