I have a form which I used to make a basic calculator but at the time of executing the script CGI
in PERL
redirects me to a white page and only prints the answer.
I would like to press the submit
button to execute the script CGI
of PERL
and without changing the page I would print the answer below the button submit
or in some space of the page without this to be redirected to the white page.
My form is this:
<form id="formu" action="multiplicar.cgi" method="POST" class="form register">
<h2 class="form_titulo"> Calculadora </h2>
<div class="contenedor-inputs">
<br>
<input type="text" name="numero1" id="nombre" placeholder="Ingresa el número " maxlength="10" class="input-100" required />
<center>
<select name="operador">
<option value="sumar">+</option>
<option value="restar">-</option>
<option value="multiplicar">x</option>
<option value="dividir">/</option>
</select>
</center>
<input type="text" name="numero2" id="nombre" placeholder="Ingresa el número " maxlength="10" class="input-100" required />
<center>
<input class="form-btn" type="submit" id="btnenviar" value="Ingresar"/>
</center>
</div>
</form>
And the script CGI
of PERL
is the following:
#!"C:\xampp\perl\bin\perl.exe"
use strict;' #se utiliza el modo estricto de perl
use CGI;' #Libreria CGI de perl
# Recoge los parametros del formulario
my $cgi = new CGI;
my $n1 = $cgi->param('numero1');
my $n2 = $cgi->param('numero2');
my $op = $cgi->param('operador');
# comprueba si los parametros son numeros
if ($n1 !~ /[0-9]+$/ or $n2 !~ /[0-9]+$/ ) {
print $cgi->header("text/html");
print "Error ha ingresado datos incorrectos";
return;
}
# declara variable total
my $total;
# se verifica la opción que el usuario eligio para mostrar el resultado
if($op eq "sumar"){
$total=$n1+$n2;
}elsif($op eq "restar"){
$total=$n1-$n2;
}elsif($op eq "multiplicar"){
$total=$n1*$n2;
}elsif($op eq "dividir"){
if($n2<=0){
$total="No se puede dividir entre 0"
}else{
$total=$n1/$n2;}
}
# cabecera http
print $cgi->header("text/html");
#muestra el resultado
print "El resultado es " . $total;