dtgj8529 2016-01-30 19:47
浏览 36

刷新process.php后,多个复选框值消失

I have a checkbox group in one file and the results that are ticked in another file. After I press submit the selected item shows up in the process file OK, and then the values go to the database.

But after I refresh the process page the selected values disappears. How can I stop this? I will need them to show on a user profile.

I have tried putting the two parts in the same file, but I get the same effect.

Here is the process.php

<?php
include("connection.php");

extract($_POST);
$check_exist_qry="select * from interests";
$run_qry=mysqli_query($con,$check_exist_qry);
$total_found=mysqli_num_rows($run_qry);
if ($total_found >0)
{
    $my_value=mysqli_fetch_assoc($run_qry);
    $my_stored_interests=explode(',',$my_value['interest_name']);
}

if (isset($submit))
{
    $all_interests_value = implode(",",$_POST['interests']);
    if ($total_found >0)
    {
        //update
        $upd_qry="UPDATE interests SET interest_name='".$all_interests_value."'";
        mysqli_query($con,$upd_qry);

    }
    else
    {
        //insert
        $ins_qry="INSERT INTO interests(interest_name) VALUES('".$all_interests_value."')";
        mysqli_query($con,$ins_qry);
    }
}

if (!empty($_POST['submit'])) {
    echo "<ul>";
    foreach ($_POST['interests'] as $value) {
        echo "<li>$value</li>";
    }
    echo "</ul>";
} else {
    echo "none";
}
?> 
<form action="checkbox1.php" method="post">
<table width="900">
<tr>
    <td width="300"><label><h3><input type="checkbox" name="interests[]"  value="option1" <?php if(isset($_POST['interests'])) { foreach($_POST['interests'] as $tmp) { if($tmp == "") { echo "checked=\"checked\"option1"; break; }}} ?>/>&nbsp;&nbsp;option1</h3></label></td>
    <td width="300"><label><h3><input type="checkbox" name="interests[]" value="option2" <?php if(isset($_POST['interests'])) { foreach($_POST['interests'] as $tmp) { if($tmp == "") { echo "checked=\"checked\"option2"; break; }}} ?>/>&nbsp;&nbsp;option2</h3></label></td>
    <td width="300"><label><h3><input type="checkbox" name="interests[]" value="option3" <?php if(isset($_POST['interests'])) { foreach($_POST['interests'] as $tmp) { if($tmp == "") { echo "checked=\"checked\"option3"; break; }}} ?>/>&nbsp;&nbsp;option3</h3></label></td>
</tr>
</table>
</form>
  • 写回答

2条回答 默认 最新

  • dqqt31923 2016-01-30 19:49
    关注

    use <?php if(in_array("option1", $_POST['interests'])) echo "checked"; ?> http://www.w3schools.com/php/func_array_in_array.asp check out the tutorial for more info..

    in your usage

    <td width="300"><label><h3><input type="checkbox" name="interests[]" value="option3" <?php if(in_array("option3", $_POST['interests'])) echo "checked"; ?> />&nbsp;&nbsp;option3</h3></label></td>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染