如何让用户按时点击按钮然后禁用按钮点击? (拉拉维尔 8)

发布于 2025-01-15 11:57:26 字数 2011 浏览 0 评论 0原文

我有 Laravel 项目(投票系统),它允许用户为表单投票,

我的问题是,如何防止用户为每个表单多次投票?

这是用户数据库:

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('student_id');
        $table->string('student_faculity');
        $table->string('email')->unique();
        $table->string('usertype')->nullable();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->foreignId('current_team_id')->nullable();
        $table->string('profile_photo_path', 2048)->nullable();
        $table->timestamps();
    });
}

这是表单表:

public function up()
{
    Schema::create('files', function (Blueprint $table) {
        $table->id();
        $table->string('name')->nullable();
        $table->string('file_path')->nullable();
        $table->string('name_as_id')->nullable();
        $table->string('email')->nullable();
        $table->string('position')->nullable();
        $table->text('bio')->nullable();
        $table->string('approve')->nullable();
        $table->string('votes')->default('0');
        $table->string('image')->nullable();
        $table->timestamps();
    });
}

表单 balde(投票按钮):

<form method="post" id="my-form" action="{{ route('votes.upVote', $row->id) }}">
    @method('PUT')
    @csrf
    <input id="btn-submit" class="" name="submitbutton" value="Vote" type="submit">
    </form>

“我正在使用 foreach 循环来显示所有表单”

表单控制器(投票功能):

public function upVote(Request $request, $id)
{
    File::find($id)->increment('votes', 1);
    return redirect()->back();
}

有没有一种方法可以阻止用户向投票按钮发送垃圾邮件?

注意:用户可以为每个表单投票一次,因此如果用户投票给特定表单,则不会禁用其他表单的投票按钮,

实现此目的的最佳方法是什么?

I have Laravel project (voting system) that will allow the user to vote for forms

my question here is, how can i prevent the user from voting more than once for each form?

here is the user database:

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('student_id');
        $table->string('student_faculity');
        $table->string('email')->unique();
        $table->string('usertype')->nullable();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->foreignId('current_team_id')->nullable();
        $table->string('profile_photo_path', 2048)->nullable();
        $table->timestamps();
    });
}

and here is the forms table:

public function up()
{
    Schema::create('files', function (Blueprint $table) {
        $table->id();
        $table->string('name')->nullable();
        $table->string('file_path')->nullable();
        $table->string('name_as_id')->nullable();
        $table->string('email')->nullable();
        $table->string('position')->nullable();
        $table->text('bio')->nullable();
        $table->string('approve')->nullable();
        $table->string('votes')->default('0');
        $table->string('image')->nullable();
        $table->timestamps();
    });
}

forms balde (vote button):

<form method="post" id="my-form" action="{{ route('votes.upVote', $row->id) }}">
    @method('PUT')
    @csrf
    <input id="btn-submit" class="" name="submitbutton" value="Vote" type="submit">
    </form>

"I'm using foreach loop to display all forms"

forms controller (vote function):

public function upVote(Request $request, $id)
{
    File::find($id)->increment('votes', 1);
    return redirect()->back();
}

is there a way that i can prevent the user from spamming the vote button?

note: the user can vote once for each form so the vote button won't be disabled for other forms if the user voted for a specific form

what is the best approach to achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

携君以终年 2025-01-22 11:57:26

使用 jQuery 您可以在提交表单后禁用提交按钮

$('#my-form').submit(function(){
   $('#btn-submit').attr('disabled', true);
})

with jQuery You can disable submit button after submit form

$('#my-form').submit(function(){
   $('#btn-submit').attr('disabled', true);
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文