使复选框数组在提交表单后保持选中状态


Make checkbox array stay checked after submitting the form

我创建了一个大型的复选框,从数据库中获取它们的值。问题是,我需要找到一种方法,如何使浏览器记住某些复选框被选中。在提交表单后,它将发送值,另一个函数将发送值用于另一个函数。

这就是我创建复选框表单的方法:

$query .= "SELECT id, ediens FROM menu_edieni_otrie";
    $query1 .= "SELECT id, ediens FROM menu_edieni_zupas";
    echo "<body>'n";
    echo "<form name='otrie_edieni' action='' method='post'>'n";
    echo "<table border='1px'>'n";
    echo "<tr>'n";
    echo "<td>'n";
    /* execute multi query */
    if ($mysqli->multi_query($query)) {
        if ($result = $mysqli->store_result()) {
            while ($row = $result->fetch_row()) {
            echo "<input type='checkbox' name='ediens2[]' value='";
            printf ("%s", $row[1]);
            echo "' ";
            echo "/>";
            printf ("%s </br>'n ", $row[1]);
            }
            $result->free();
        }
    }
    echo "</td>'n";
    echo "<td>'n";
    if ($mysqli->multi_query($query1)) {
        if ($result = $mysqli->store_result()) {
            while ($row = $result->fetch_row()) {
            echo "<input type='checkbox' name='zupas2[]' value='";
            printf ("%s", $row[1]);
            echo "'/>";
            printf ("%s </br>'n ", $row[1]);
            }
            $result->free();
        }
    }
    echo "</td>'n";
    echo "</tr>'n";
    echo "</table>";
    echo "<input type='submit' name='formSubmit' value='Submit' />";
    echo "</form>'n";
    echo "</body>";

在提交按钮被按下后,我如何检查哪些复选框被选中,并在刷新后将它们选中?

_POST被这个函数接收:

$ediens2 = $_POST['ediens2'];
$zupas2 = $_POST['zupas2'];
  if(empty($ediens2) && empty($zupas2)) 
  {
    echo("Nav ievadīta neviena vērtība");
  } 
  else
  {
    $N = count($ediens2);
    $M = count($zupas2);
    $mysqli = @new mysqli('*', '*', '*', '*');
        $mysqli->query("SET NAMES 'utf8'", $connection);
        if ($mysqli->connect_errno) {
            die('Connect Error: ' . $mysqli->connect_errno);
        }
    for($i=0; $i < $N; $i++){
        $query3 = "UPDATE tmp_cirks SET name='$ediens2[$i]' WHERE id='$i'";
        $mysqli->multi_query($query3);
    }
    /* close connection */
    $mysqli->close();
    $date = date('d/m/Y', time());
    echo $date;
    echo("<br>");
    echo("<ul>");
    for($i=0; $i < $N; $i++)
    {
        echo("<li>");
        echo("<strong>" . $ediens2[$i] . "</strong>");
        echo("</li>");
    }
    echo("</ul>");
    echo("<ul>");
    for($i=0; $i < $M; $i++)
    {
        echo("<li>");
        echo("<strong>" . $zupas2[$i] . "</strong>");
        echo("</li>");
    }
    echo("</ul>");      
  }

到目前为止,我所知道的是,我将从_POST接收到的值存储到一个单独的数据库表中-使用如下:

for($i=0; $i < $N; $i++){
        $query3 = "UPDATE tmp_cirks SET name='$ediens2[$i]' WHERE id='$i'";
        $mysqli->multi_query($query3);
    }

我在想,然后我可以比较如果生成的复选框表单有任何值存储在这个单独的表。如果有,则生成"checked='checked'"函数。我的思路对吗?

数据库表是这样的:Id ediens edieni_ru edieni_eng cena cena_puseotr1 Kotletes Котлеты Сutlet 2,001,30otr2 Gulašs Гуляш炖牛肉2,201,40otr3 Slinkie tīteņi Ленивые голубцы切碎的卷心菜炒肉末1,70 1,15otr4 Plovs Плов Pilaff 1,701,15

为了生成复选框值,我只使用"ediens"列。我创建的新表如下所示(保存值的表):

id名称1 Kotletes2咽喉š年代3 Karbonāde>

以我想的是,像这样:If (name==ediens) echo 'checked=checked';(当然这只是一个不工作的例子)

有什么建议吗?我甚至不知道我是否在正确的轨道上。

提前感谢您的帮助!

都在这里工作…有任何问题请问。我来修。

这段代码显示存储在两个表中的条目中的两个菜单。它允许用户选择他们想要的条目,并通过检查$_POST数组在输出屏幕上设置'checked'属性来持久化'checked'条目。

这是所提供的代码,经过调试并使其以最小的更改工作。

修复:1) 'multi_query'没有正确处理第二个结果集。

2)单独的SQL查询需要以';'结束。

下面是测试代码(PHP 5.3.18)

我改变了'fetch'语句,所以我可以通过'列名'访问数据。

我保留了显示$_POST数组的调试代码,以便您可以看到它的工作情况。只需删除标有/* debug */的代码,以停止显示它们。

<?php
    /* debug */ var_dump($_POST);
    $mysqli = new mysqli('localhost', 'test', 'test', 'testmysql');
    $query  = '';
    $query .= "SELECT id, ediens FROM menu_edieni_otrie order by id;";
    $query .= "SELECT id, ediens FROM menu_edieni_zupas order by id;";
    echo "<body>'n";
    echo "<form name='otrie_edieni' action='' method='post'>'n";
    echo "<table border='1px'>'n";
    echo "<tr>'n";
    echo "<td>'n";
    /* execute multi query */
    if ($mysqli->multi_query($query)) {
        if ($result = $mysqli->store_result()) {
            while ($row = $result->fetch_array()) {
                echo "<input type='checkbox' name='ediens2[]' value='";
                printf ("%s", $row['id']);
                echo "' ";
                echo isset($_POST['ediens2']) && in_array($row['id'], $_POST['ediens2']) ? ' checked="checked"' : '';
                echo "/>";
                printf ("%s </br>'n ", $row[1]);
            }
            $result->free();
        }
    }
    echo "</td>'n";
    echo "<td>'n";
    // try and get the next results (zupas)
    if ($mysqli->more_results()) {          // any results from the zupas menu?
        $mysqli->next_result();             // get them...
        $result = $mysqli->store_result();  // access them...
        while ($row = $result->fetch_array()) { // process them
            echo "<input type='checkbox' name='zupas2[]' value='";
            printf ("%s", $row['id']);
            echo "' ";
            // we need to set the 'checked' attribute if the user ticked the box.
            // we check the $_POST array to see if the current value is in there ans set it checked if it is.
            echo isset($_POST['zupas2']) && in_array($row['id'], $_POST['zupas2']) ? ' checked="checked"' : '';
            echo "'/>";
            printf ("%s </br>'n ", $row[1]);
        }
        $result->free();
    }
    echo "</td>'n";
    echo "</tr>'n";
    echo "</table>";
    echo "<input type='submit' name='formSubmit' value='Submit' />";
    echo "</form>'n";
    echo "</body>";
?>

在post函数中使用setcookie并在刷新后使用$_COOKIE来确定是否被检查。

setcookie("Checkbox1", "checked", time()+3600);
if($_COOKIE["Checkbox1"] == "checked") echo "checkbox1 checked";

编辑:但是,当然,如果您保存它们,您可以从数据库加载复选框值。