I have this file called crear_pedido_cliente.php, when nearly at the bottom there's a table with id="tabla". The user adds an article from a select, an ammount and the price, wich gives a total value per line; when the user clicks in the button "confirmar_linea", with jQuery those 4 fields are appended to the table. Here's the code:
<div id="contenido">
<?php echo mensaje(); ?>
<?php echo errores_de_formulario($errores); ?>
<h2>Nuevo Pedido</h2>
<form action="" name="form_pedido" method="post" novalidate>
<p>Codigo del Pedido:
<input type="text" class="encabezado_pedido" name="codigo_del_pedido" value="">
</p>
<p>Cliente:
<span class="select">
<select name="cliente_select" id="custom-select" class="encabezado_pedido">
<?php
$grupo_clientes = buscar_clientes();
foreach($grupo_clientes as $item) {
echo "<option value=\"{$item["idCliente"]}\">{$item["razonSocial"]}</option>";
}
?>
</select>
</span>
</p>
<p>Fecha:
<input type="text" class="encabezado_pedido" name="fecha_del_pedido" id="fecha" value="">
</p>
<button type="button" id="otro_pedido" name="otro_pedido" onclick="otro_pedido()">Otro Pedido</button>
<button type="button" id="confirmar_encabezado" name="confirmar_encabezado" onclick="encabezado_listo()">Confirmar Datos</button>
<div id="detalles">
<p>Articulo:
<select id="articulo_select">
<?php
$grupo_articulos = buscar_articulos();
foreach($grupo_articulos as $item) {
echo "<option value=\"{$item["idArticulo"]}\">{$item["descripcion"]}</option>";
}
?>
</select>
</p>
<p>Cantidad:
<input type="text" class="detalle_pedido" name="cantidad" id="cantidad">
</p>
<p>Precio:
<input type="text" class="detalle_pedido" id="precio" name="precio">
</p>
</div>
<button type="button" id="confirmar_linea" name="" onclick="">Aceptar Linea</button>
<input type="submit" id="guardar_pedido" name="submit" value="Guardar Pedido" onclick="">
<br>
<div id="detalles_actuales">
<table id="tabla" border="1">
<tr>
<td>
<label>Articulo</label>
</td>
<td>
<label>Cantidad</label>
</td>
<td>
<label>Precio Unitario</label>
</td>
<td>
<label>Precio de Linea</label>
</td>
</tr>
</table>
<p>Total:
<input type="text" id="total" name="precio_total" disabled value="0.00"/>
</p>
</div>
</form>
<br>
<a href="lista_pedidos.php">Cancelar</a>
</div>
<script src="js/jquery.js" type="text/javascript"></script>
<script>
$(document).ready(function () {
var lista = 0;
$('#confirmar_linea').click(function () {
var articulo = $("#articulo_select").val();
var cantidad = parseInt($("#cantidad").val(), 10);
var precio = parseFloat($("#precio").val());
var precio_t = cantidad * precio;
$('#tabla').append("<tr>" + "<td>" + articulo + "</td>" + "<td>" + cantidad + "</td>" + "<td>" + precio + "</td>" + "<td>" + precio_t.toFixed(2) + "</td>" + "</tr>");
var total = parseInt($("#total").val(), 10);
$('input[name=precio_total]').val((total + precio_t).toFixed(2));
var fila = [articulo, cantidad, precio, precio_t];
lista = lista+1;
});
</script>
My problem is that at the begginning of the file I want to read with PHP all those rows inserted with jQuery to insert in a database. I've tried the following in the same file:
if (isset($_POST['submit'])) {
$url = "crear_pedido_cliente.php";
function get_data_from_table($url)
{
// retrieve the content of that url
$content = file_get_contents($url);
// load into DOM
$dom = new DOMDocument();
$dom->loadHTML($content);
// make xpath-able
$xpath = new DOMXPath($dom);
$query = "//tr/td";
$elements = $xpath->query($query);
$element = $elements->item(0);
// get his parent element (tr)
$tr = $element->parentNode;
$data = array();
// iterate over it's td elements
foreach ($tr->getElementsByTagName("td") as $td) {
// retrieve the content as text
$data[] = $td->textContent;
}
// return the array of <td> contents
return $data;
}
echo '<pre>';
print_r(get_data_from_table($url));
echo '</pre>';
}
The result, besides some warnings, are the first 4 elements in that table, the "titles" that are written in the table in HTML and wrapped with label tags.
Is there any issue with jQuery adding this that PHP doesn't recognize them as part of the DOM?
EDIT: I've tried this answer, using the Simple HTML Dom library. And the result is:
Array
{
}