繁体   English   中英

使用Perl和PostgreSQL保存复选框选择

[英]Saving check boxes choices with Perl and PostgreSQL

我正在尝试创建一个带有多个复选框的用户首选项页面,让用户决定他们希望如何接收其通知。 这是我当前的偏好设置HTML表单:

<form method="post">
  <table id="preferences" class = "preferences">
    <tr class="headers">
      <th class = "preferences">Preference</th>
      <th class = "preferences">Value</th>
      <th class = "preferences">Notification Preferences</th>
    </tr>
    <tr>
      <td class = "preferences">Default Post Markup</td>
      <td class = "preferences">
        <select name="default_markup">
          <% foreach(USystem::Markup->markup_types) {
                 print Usystem::Utils::option_tag($_->{id}, $_->{name}, $prefs->value(1));
             } %>
        </select>
      </td>
      <td>
        <input type = "checkbox" name = "notification-option" id = "all-post"> All Posts <br/>
        <input type = "checkbox" name = "notification-option" id = "others-post"> Other's Posts <br/>
        <input type = "checkbox" name = "notification-option" id = "client-post"> Cilent's Post <br/>
        <input type = "checkbox" name = "notification-option" id = "assign-post"> Task Assigned
      </td>
    </tr>
    <tr>
      <th colspan="3" class="submitrow"><input type="submit" name="submit" value="Save Preferences" /></th>
    </tr>
  </table>
</form>

我刚刚添加了4个复选框,我需要帮助将它们保存到数据库中,在这里我使用布尔值确定是否选中了它们。

当选择“保存首选项”按钮时,我的Perl脚本在HTML页面上运行:

my $form = $Request->Params;
my $prefs = USystem::UserPrefs->new($user->userid);

if($Request->{Method} =~ /POST/i) {
    my $dbh = USystem::DB->new;

    if($form->{submit} eq "Save Preferences") {
        $prefs->value($USER_PREF_MARKUPTYPE, $form->{default_markup});
        $prefs->update;
    }
    $Response->Redirect('/preferences');
}

$ Request-> Params与CGI-> Params()基本相同

这是UserPrefs:

package USystems::UserPrefs;

use strict;
use USystems::DB;

sub new {
    my $self = shift;
    my $class = ref($self) || $self;
    my $userid = shift;

    $self = bless {}, $class;
    $self->{dbh} = USystems::DB->new;

    if($userid) {
        $self->userid($userid);
        $self->__populate;
    }

    return $self;
}

sub __populate {
    my $self = shift;

    my $sth = $self->{dbh}->prepare("select prefid, value from user_preferences where userid=?");
    $sth->execute($self->userid);
    while(my $href = $sth->fetchrow_hashref()) {
        $self->value($href->{PREFID}, $href->{VALUE});
    }
    $sth->finish();
}

sub update {
    my $self = shift;

    my $sth = $self->{dbh}->prepare("delete from user_preferences where userid=?");
    $sth->execute($self->userid);
    $sth->finish;

    $sth = $self->{dbh}->prepare("insert into user_preferences(userid, prefid, value) values(?, ?, ?)");
    while(my ($prefid, $value) = each %{$self->{PREFS}}) {
        $sth->execute($self->userid, $prefid, $value);
    }
    $sth->finish;
}

sub userid {
    my $self = shift;
    $self->{USERID} = shift if(@_);
    $self->{USERID};
}

sub value {
    my $self = shift;
    my $prefid = shift;
    $self->{PREFS}->{$prefid} = shift if(@_);
    return $self->{PREFS}->{$prefid};
}

1

我将如何存储是否选中复选框的值,以及如何更新当前的Perl脚本以获取这些值。 我对Perl不太陌生,但是运气并不好。 如果您能解释为什么自己也做了我,那将是很棒的。

如果您需要更多信息,请告诉我。 谢谢

线

my $prefs = USystem::UserPrefs->new($user->userid)

创建一个新对象,并从数据库中复制该用户的当前首选项。 之后,您可以使用$prefs->value(value_name [, new_value)访问或更新各个首选项,调用$prefs->update会将当前值复制回数据库

你已经有了

$prefs->value($USER_PREF_MARKUPTYPE, $form->{default_markup})

并且您需要添加更多类似的调用才能为其他首选项设置值

$prefs->update

全部保存

暂无
暂无

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

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