I have a PHP file where POST type data is received to be displayed on the console, these are NIT and payment method.
PHP:
<?php
$sNit = $_POST["ingresoNIT"];
$sMetodoPago = $_POST["metodoPago"];
echo json_encode('Correcto: <br>NIT:'.$sNit);
echo json_encode('Correcto: <br>METODOPAGO:'.$sMetodoPago);
?>
The NIT and the payment method are in two different forms, and I need to send the content of the two forms to the PHP file to do the validations, my code is as follows:
var nitFinal = document.getElementById('nit');
var metodoPagoFact = document.getElementById('metodoPagoFactura');
var datos = new FormData(nitFinal);
var datos2 = new FormData(metodoPagoFact);
fetch('../controlador/nuevaFactura/facturaFinal/csInsertarFacturaFinal.php',{
method: 'POST',
body: datos
})
.then( res => res.json())
.then( data => {
console.log(data)
})
fetch('../controlador/nuevaFactura/facturaFinal/csInsertarFacturaFinal.php',{
method: 'POST',
body: datos2
})
.then( res => res.json())
.then( data => {
console.log(data2)
})
I need to send both form data to the same PHP, but only the last one is working for me, if I remove the first fetch, the data2 body that has the payment method is saved, and if I remove the second, the NIT is saved or sent, but I need that both are sent, thanks for the support.
You could use
FormData.append()
andFormData.entries()
together to get an expected result.In other words, you are getting both forms and mixing the values into one in order to send all the data together as if it were a single form.
Note: just be careful not to have fields under the same name in both forms as this could lead to unwanted behavior.
Official Documentation (MDN):