1

I am trying to send data from an input_tag when I press a button with the templates in mojolicious.

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 %>

When I press the "generate" button I want to send that data in the inputs to another perl script.

Grinnz
  • 9,093
  • 11
  • 18
  • You tagged this with "javascript", but you aren't using any. Do you want a javascript form submission solution, as opposed to an HTML form? In other words, do you want to direct the user to a new page with the results of the form, or do you want to call an API from the form and update the page in place (requires javascript)? – Grinnz May 28 '19 at 18:25

1 Answers1

0

Add a form, grab the input values and send them e.g. as arguments to the perl script.

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>