繁体   English   中英

将具有不同名称的输入数组保存到数据库

[英]Save array of inputs with different names to database

这是一个WordPress插件。

因此,我有一个在创建新项目时创建的隐藏输入复选框。 可以使用滑动开关(复选框)打开/关闭这些项目。 通过打开/关闭某些项目进行测试后,使用var_dump($_POST)时似乎正在发布数组,

即:

array (size=3)
  'new_filter' => string '' (length=0)
  'dp_show_filter_15' => string 'yes' (length=3)
  'dp_show_filter_16' => string 'yes' (length=3) 

但仅当一项设置为yes时,no不返回任何内容。

即使转储显示了更新后的阵列,也不会将任何值发布到数据库,并且Firebug中的输入值为空。

这是我的PHP代码:

/*------------------------------------------*/
/*    Check if the filter is active or not  */
/*------------------------------------------*/

$settings="select * from wp_dp_settings";
$filters=$wpdb->get_results($settings,ARRAY_A);

/*------------------------------------------*/
/*    We'll use this to save active states  */
/*------------------------------------------*/

if($_POST){
    ${active}=isset($_POST["dp_show_filter_[$i]"]);
    var_dump($_POST);
    $update="UPDATE wp_dp_settings SET active_filter = '".$active."' WHERE id"; 
    //die( $qry );
    $ok=$wpdb->query($update);

    if($ok==0)
    {
        echo mysql_error();
    }
    else
    { 
        header("Location: admin.php?page=profolio_theme");
        /** Just lettin you know that there was a successful save **/
        echo "<div class='updated settings-error' id='setting-error-settings_updated'> 
        <p><strong>Active State Saved.</strong></p></div>";
    }
}


?>
<td><label for="new_filter">Create a New Filter :<span class="tip">Must be one word</span></label></td>
<td><input type="text" name="new_filter" id="new_filter"/></td>
<td>
</tr>
<?php $object = new stdClass(); ?>
<?php foreach( $filters as $key => $value ) {
    ?>
    <tr>
        <td><label for="show_filter">Show <?php echo $object->$key = $value['filter']; ?> Filter</label></td>        
        <td><input type="checkbox" id="show_filter_<?php echo $object->$key = $value['id']; ?>" <?php //if ( $row['active_filter'] === 'yes' ) { ?>checked="checked"<?php //} ?> name="dp_show_filter_<?php echo $object->$key = $value['id']; ?>" value="<?php echo $object->$key = $value['active_filter']; ?>"/><?php echo $object->$key = $value['active_filter']; ?></td>
        <td><a class="button-primary" href="?page=profolio_theme&del=<?php echo $object->$key = $value['id'];?>">Delete</a></td>
    </tr>
    <?php
    $i++;
}
?>

当我将id附加到输入名称时,就引入了此问题,因为它以前是name="dp_show_filter"而不是name="dp_show_filter_15"但是由于我在javascript中引用输入名称时,导致所有项目自然更新。

如果有帮助,您可以看一下:

jQuery(document).ready(function(e) {
    var $ = jQuery.noConflict();

        $i = 0;
        customCheckbox = $('.dp_wrap input[type="checkbox"]');
        values = [];

            return customCheckbox.each(function(i) {

                // the dynamic id
                var id = parseInt(this.id.replace("show_filter_", ""));
                var showFilter = $("#show_filter_" + id);

                // the element
                var el = this;

                // Hide checkbox
                $(this).hide();

                // Replace element
                var rep = $('<a href="#"><span></span></a>').addClass('dp-checkbox').insertAfter(this);

                // default state
                if( $(showFilter[i]).val() === 'yes'){
                        $(showFilter).prop('checked', true);
                        $(rep).removeClass('off').addClass('on');
                    } else {
                        $(showFilter).prop('checked', false);
                        $(rep).removeClass('on').addClass('off');
                    }
                if($(this).is(':checked') ) {
                    $(rep).addClass('on');
                } else {
                    $(rep).addClass('off');
                }

                // Click event
                $(rep).click(function(e) {

                    e.preventDefault();

                    if( $(el).is(':checked') ) {

                        values.push($(showFilter).val('no'), ++$i);

                        $(el).prop('checked', false);
                        $(rep).removeClass('on').addClass('off');
                    } else {

                        values.push($(showFilter).val('yes'), ++$i);

                        $(el).prop('checked', true);
                        $(rep).removeClass('off').addClass('on');
                    }
                });
            });
});

因此,当我打开/关闭项目时,将复选框的值设置为是/否,这些值应发布到数据库中。 注意:当我打开/关闭时,我注意到Firebug中的值分别更改为yes / no,所以我知道我的JavaScript可以正常工作。

非常感谢您的任何帮助!

1)您使用${active}=isset($_POST["dp_show_filter_[$i]"]); isset()将返回一个布尔值,您无法将其回显,请尝试:

$i=3;
$active=isset($_POST["dp_show_filter_[$i]"]);
var_dump($active);
echo $active;

因此,不会在查询中显示$ active。

2) ${active}不是使用$active声明变量的正确方法。

3)即使设置了$i$_POST["dp_show_filter_[$i]"]也会使用键dp_show_filter [3]($ i = 3)引用$ _POST。 您的var_dump显示应为dp_show_filter_3

因此,尝试类似的方法:

$id = 17;
$active=isset($_POST['dp_show_filter_'.$id]);
var_dump($_POST);
$update="UPDATE wp_dp_settings SET active_filter = '".($active)?'true':'false'."' WHERE id=".$id; 
$ok=$wpdb->query($update);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM