提交带有复选框的表单后更新MySQL表

时间:2022-09-26 10:09:40

I am working on a web form.

我正在处理一个网络表单。

The form is loading records from a MySQL database.

表单正在从MySQL数据库加载记录。

This is the form:

这是形式:

<div class="panel-body">
    <form id="signupform" class="form-horizontal" role="form" action="<?php $_SERVER['PHP_SELF'] ?>" method="POST" autocomplete="off">
        <div id="signupalert" style="display:none" class="alert alert-danger">
            <p>Error:</p>   <span></span>
        </div>
        <div class="form-group">
            <label for="nombre" class="col-md-3 control-label">Marcas asociadas:</label>
            <div class="col-md-9">
                <?php 
                    global $mysqli;
                    $loop = m ysqli_query($mysqli, "SELECT * FROM tb_marcas_vendedor  LEFT JOIN tb_marcas  ON                                                       tb_marcas_vendedor.marca_vendedor = tb_marcas.id_marca
                                                    WHERE tb_marcas_vendedor.vendedor_marca =  '".$_SESSION[ 'id_usuario']. "' 
                                                    ORDER BY id_marca" ) or die (mysqli_error($mysqli)); 
                                                    while ($row=m ysqli_fetch_array($loop)) 
                                                    { 
                                                        if ($row[ 'seleccionado']==1 )
                                                        { 
                                                            echo "<label><input type='checkbox' value=".$row[ 'seleccionado']. " name='sport' checked> ".$row[ 'nombre_marca']. "</label>"; 
                                                        }
                                                        else
                                                        { 
                                                            echo "<label><input type='checkbox' value=".$row[ 'seleccionado']. " name='sport' > ".$row[ 'nombre_marca']. "</label>"; 
                                                        } 
                                                    } 
                ?>
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-3 col-md-9">
                <button id="btn-signup" type="submit" class="btn btn-info"><i class="icon-hand-right"></i>Actualizar</button>
            </div>
        </div>
    </form>
    <?php echo resultBlock($errors); ?>
</div>

And this is the output:

这是输出:

提交带有复选框的表单后更新MySQL表 提交带有复选框的表单后更新MySQL表

I need to update the mysql data. If the user checks a box,then the value for $row['seleccionado'] should be 1, if not checked, it should be 0. How can I get the id from the selected row and update the table after submitting the form?

我需要更新mysql数据。如果用户选中一个框,则$ row ['seleccionado']的值应为1,如果未选中,则应为0.如何从所选行中获取id并在提交表单后更新表?

EDIT

编辑

This is the table tb_marcas_vendedor structure:

这是表tb_marcas_vendedor结构:

提交带有复选框的表单后更新MySQL表

1 个解决方案

#1


1  

Add a hidden input with the id in the hidden element, and a normal checkbox that just updates the hidden element using native JavaScript:

在隐藏元素中添加带有id的隐藏输入,以及使用本机JavaScript更新隐藏元素的普通复选框:

<input type='hidden' name='" . $row['id_marca_vendedor'] . "' value='" . $row[ 'seleccionado'] . "'><input type="checkbox" onclick='this.previousSibling.value=1-this.previousSibling.value'>

Make sure this is no space in between these elements. This will ensure even unchecked values are accounted for.

确保这些元素之间没有空格。这将确保计算甚至未经检查的值。

If you append id_marca_vendedor to the name field on your input field, you can loop through the variables on the server side and get the results/id on the POST portion:

如果将id_marca_vendedor附加到输入字段的name字段,则可以循环访问服务器端的变量并获取POST部分的结果/ id:

// Loop over each item in the form.
foreach($_POST as $name => $value) {

    // Get the id and value
    $id = $name;
    $selection = $_POST[$name];

    // Database query goes here

}

Source

资源

Edit: Updated to include @jeff's point of unchecked values.

编辑:已更新以包含@ jeff未选中的值。

#1


1  

Add a hidden input with the id in the hidden element, and a normal checkbox that just updates the hidden element using native JavaScript:

在隐藏元素中添加带有id的隐藏输入,以及使用本机JavaScript更新隐藏元素的普通复选框:

<input type='hidden' name='" . $row['id_marca_vendedor'] . "' value='" . $row[ 'seleccionado'] . "'><input type="checkbox" onclick='this.previousSibling.value=1-this.previousSibling.value'>

Make sure this is no space in between these elements. This will ensure even unchecked values are accounted for.

确保这些元素之间没有空格。这将确保计算甚至未经检查的值。

If you append id_marca_vendedor to the name field on your input field, you can loop through the variables on the server side and get the results/id on the POST portion:

如果将id_marca_vendedor附加到输入字段的name字段,则可以循环访问服务器端的变量并获取POST部分的结果/ id:

// Loop over each item in the form.
foreach($_POST as $name => $value) {

    // Get the id and value
    $id = $name;
    $selection = $_POST[$name];

    // Database query goes here

}

Source

资源

Edit: Updated to include @jeff's point of unchecked values.

编辑:已更新以包含@ jeff未选中的值。