首页 > 解决方案 > 如何将 input_tag 的值存储在变量中?

问题描述

当我按下带有 mojolicious 模板的按钮时,我试图从 input_tag 发送数据。

use Mojolicious::Lite;

get '/' => 'index';

app->start;
__DATA__

@@ index.html.ep
<html lang="es">
  <head>
    <meta charset="utf-8"/>
    <title>Graphs</title>
  </head>
  <body>
    <%= tag div => (id => 'inputs') => begin %>
        <h1>Grafos dirigidos</h1>
        %= input_tag 'nodes', placeholder => '#Nodos', id => 'nodes'
        %= input_tag 'edges', placeholder => '#Edges', id => 'edges'
        %= input_tag 'start', placeholder => 'Start', id => 'start'
        %= submit_button 'Generate', id => 'render', onclick => 'renderGraph()'
        %= tag 'br'
        %= text_area 'dijkstra', cols => 40, rows => 40, id => 'dijkstra'
    <% end %>

当我按下“生成”按钮时,我想将输入中的数据发送到另一个 perl 脚本。

标签: javascripthtmlperlmojoliciousmojolicious-lite

解决方案


添加一个表单,获取输入值并将它们作为参数发送给 perl 脚本。

use Mojolicious::Lite;

get '/' => 'index';

post '/' => sub {
    my $c = shift;

    my ($nodes, $edges, $start) = map { $c->param($_) } qw(nodes edges start);

    system 'script.pl', '--nodes', $nodes, '--edges', $edges, '--start', $start;

    $c->render(text => 'done');
};

app->start;
__DATA__

@@ index.html.ep
<html lang="es">
  <head>
    <meta charset="utf-8"/>
    <title>Graphs</title>
  </head>
  <body>
    <form method="post">
        <%= tag div => (id => 'inputs') => begin %>
            <h1>Grafos dirigidos</h1>
            %= input_tag 'nodes', placeholder => '#Nodos', id => 'nodes'
            %= input_tag 'edges', placeholder => '#Edges', id => 'edges'
            %= input_tag 'start', placeholder => 'Start', id => 'start'
            %= submit_button 'Generate', id => 'render', onclick => 'renderGraph()'
            %= tag 'br'
            %= text_area 'dijkstra', cols => 40, rows => 40, id => 'dijkstra'
        <% end %>
    </form>

推荐阅读