Send form values using Jquery and return response

0

First of all a cordial greeting! I have one forms where the user enters a series of numerical values. What I want to do is send these values to a PHP file using Jquery so that PHP can perform a series of calculations with them (subtraction, multiplication and division) and return me the results to the page where I am and shown in a table of results.

How does it work? Each form has a send button, which will pass the data to the PHP file using Jquery for it and to execute the operation that I have already mentioned, that is, the user can make a sending of several forms, each with different values, and the idea is that each one is processed separately in the PHP and the results returned will be shown one below the other in a table and then send this data by email. Without further ado, I will show you the important pieces of code hoping you can help me:

FORMS:

<tr>
<form id="form_calcular<?php echo $a;?>" name="form_calcular<?php echo $a;?>" method="post">

<td><input type="number" name="cod" id="cod" value="<?php echo $row['cod'];?>" class="form-control" readonly></td>
				  
<td><input type="text" name="producto" id="producto" value="<?php echo $row['producto'];?>" class="form-control" readonly></td>
				  
<td><input type="number" name="costo" id="costo" value="<?php echo $row['costo'];?>" class="form-control" readonly></td>
				  
<td><input type="number" name="sn_iva" id="sn_iva" value="<?php echo $row['sn_iva'];?>" class="form-control" readonly></td>
				  
 <td><input type="number" name="iva" id="iva" value="<?php echo $row['iva'];?>" class="form-control"></td>
				  
<td><input type="number" name="pvp" id="pvp" value="<?php echo $row['pvp'];?>" class="form-control" readonly></td>
				  
<td><input type="number" name="benef" id="benef" value="<?php echo $row['benef'];?>" class="form-control" readonly></td>
				  
<td><input type="number" name="g_med" id="g_med" value="<?php echo $row['g_med'];?>" class="form-control" readonly></td>
				  
<td><input type="number" name="ing_comp" id="ing_comp" value="<?php echo $row['ing_comp'];?>" class="form-control" readonly></td>
				  
<td><input type="number" name="hc" id="hc" value="<?php echo $row['hc'];?>" class="form-control" readonly></td>
				  
<td><input type="number" name="cant" id="cant" value="<?php echo $row['cant'];?>" class="form-control"></td>
				  
<td><button type="button" id="calcular" name="calcular" class="btn btn-primary"><i class="fa fa-shopping-cart"></i></button></td>
				  
</form>
</tr>

As you can see, there are only 2 fields that can be edited by the user while the others are values that I extract from the DB and are not modifiable (In this table) , the idea is for the user to enter the discount margin (Sip, it must be like that hehe) and the quantity of the product, then I send these values to the code PHP and I make some formulas to calculate the PVP SIN VAT , COST, ETC and show the final results in the second table that would be the results, what I try is to simulate something like Excel formulas work that when entering the values of the DISCOUNT and AMOUNT the rest is calculated only, but in this case they would be calculated by pressing the respective button on each form.

JQUERY CODE THAT SENDS THE FORM TO THE PHP FILE

$(document).ready(function()
{
	$("#calcular").click(function()
{
	var url = "php/calcular_valores.php";
	$.ajax
	({
	type: "POST",
	url: url,
	data: $("#form_calcular<?php echo $a;?>").serialize(),
	beforeSend: function()
{
    $('#imagen-espere').show();
    $('#msj').html('Calculando...');
    $('#msj3').html('');
    $('#msj2').html('');
},
    success:function(respuesta)
{
    $('#imagen-espere').hide();
    if (respuesta==1)
{
    $('#msj3').html('¡No pueden haber valores vacíos!');
    $('#msj').html('');
}
	else if (respuesta==2)
{
/* AQUI NECESITO SABER COMO EXTRAER LOS RESULTADOS DE LAS OPERACIONES PARA MOSTRARLOS EN LA TABLA DESTINO */
}                            
    else if (respuesta==3)
{
    $('#msj3').html('¡Oh! Ha ocurrido un error al procesar su solicitud');
    $('#msj').html('');
}
}
});
	return false;
});
});

In this previous code I would like you to help me know how I can extract the results of the calculations that PHP has done to show them in the target table (Ideally I think it would be to return them in an array to build the table, I do not know suggest me) and well, I have not been using Jquery for a long time so I would greatly appreciate your help ;-) I have managed to implement it perfectly to make a CRUD operations, but in this case I still like if you do not see hehe

THIS WOULD BE THE PHP TO RUN THE OPERATIONS:

<?php 

include('conexion.php');

sleep(2);


	$cod = $_POST['cod'];
	$producto = $_POST['producto'];
	$costo = $_POST['costo'];
	$sn_iva = $_POST['sn_iva'];
	$iva = $_POST['iva'];
	$pvp = $_POST['pvp'];
	$benef = $_POST['benef'];
	$g_med = $_POST['g_med'];
	$ing_comp = $_POST['ing_comp'];
	$hc = $_POST['hc'];
	$cant = $_POST['cant'];

	if ($cod =='' or $producto =='' or $costo =='' or $sn_iva =='' or $iva =='' or $pvp =='' or $benef =='' or $g_med =='' or $ing_comp =='' or $hc =='' or $cant =='')
{
echo 1;
}
else
{

$res_sin_iva = $pvp/1.21;
$res_iva = $pvp-$res_sin_iva;
$res_benef = $pvp-$res_iva-$costo;
$res_g_med = $res_benef*42/100+$res_iva;
$res_ing_comp = $res_benef*58/100;

$resultados = "$cod, $producto, $costo, $res_sin_iva, $res_iva, $pvp, $res_benef, $res_g_med, $res_ing_comp, $hc, $cant";
	
echo 2;

}

}
else
{
echo 3;
}
?>

As you can see, there are operations that depend on previous results, such as: $res_iva = $pvp-$res_sin_iva; that depends on the result of "$ res_sin_iva" which we subtract from the value of " PVP ", I see it from the point as I have coded it, where I assign the result of an operation to a variable and then manipulate it with following operations.

PS: If you suggest a better way to achieve this operation, I'm happy to hear everything

    
asked by Anto J 02.01.2018 в 07:58
source

0 answers