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


Multi checkbox values disappear after refresh process.php

>我在一个文件中有一个复选框组,在另一个文件中勾选的结果。按提交后,所选项目将显示在进程文件中确定,然后值转到数据库。

但是在我刷新流程页面后,所选值消失了。我怎样才能阻止这种情况?我需要它们显示在用户配置文件中。

我尝试将这两个部分放在同一个文件中,但我得到了相同的效果。

这是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>

使用<?php if(in_array("option1", $_POST['interests'])) echo "checked"; ?>http://www.w3schools.com/php/func_array_in_array.asp查看教程以获取更多信息..

在您的使用中

<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>

您可以使用以下逻辑验证和设置复选框:

<?php 
    if(isset($_POST['interests']) && in_array("option1", $_POST['interests'])){
        echo " checked='checked'";
    }
?>

以下是参考:

  • in_array()

所以你的代码应该是这样的:

// your code
<tr>
<td width="300"><label><h3><input type="checkbox" name="interests[]"  value="option1" <?php if(isset($_POST['interests']) && in_array("option1", $_POST['interests'])){ echo " checked='checked'"; } ?>/>&nbsp;&nbsp;option1</h3></label></td>
<td width="300"><label><h3><input type="checkbox" name="interests[]" value="option2" <?php if(isset($_POST['interests']) && in_array("option2", $_POST['interests'])){ echo " checked='checked'"; } ?>/>&nbsp;&nbsp;option2</h3></label></td>
<td width="300"><label><h3><input type="checkbox" name="interests[]" value="option3" <?php if(isset($_POST['interests']) && in_array("option3", $_POST['interests'])){ echo " checked='checked'"; } ?>/>&nbsp;&nbsp;option3</h3></label></td>
</tr>
// your code