无法使用PHP和jQuery读取所有表元素

时间:2015-09-09 01:16:03

标签: php jquery html

我有一个名为 crear_pedido_cliente.php 的文件,当几乎在底部有一个 id =“tabla”的表格时。用户添加select,ammount和price的文章,每行给出一个总值;当用户点击按钮“confirmar_linea”时,使用jQuery将这4个字段附加到表中。这是代码:

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

我的问题是,在文件的开头,我想用PHP读取所有那些用jQuery插入的行插入数据库中。我在同一个文件中尝试了以下内容:

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>';

}

除了一些警告之外,结果是该表中的前4个元素,“HTML”中的“标题”用HTML标记并用标签标签包装。

jQuery是否存在任何问题,即PHP不会将它们识别为DOM的一部分?

编辑:我尝试使用Simple HTML Dom库this answer。结果是:

Array
{
}

2 个答案:

答案 0 :(得分:1)

<table id="table1" border="1">
   <tr>
      <th>EmployeeID</th>
      <th>Name</th>
      <th>Designation</th>
      <th>Process1</th>
      <th>Process2</th>
   </tr>
   <tr>
      <td>
         <input type="hidden" id="a1" class="firstName" value="1">
         0000011
      </td>
      <td>
         chidambaram
      </td>
      <td>
         Tech Lead
      </td>
      <td>
         <input type="hidden" id="a1" class="firstName" value="11">
         <input type="text" id="a1" class="firstName" value="text1">
      </td>
      <td>
         <input type="hidden" id="a1" class="firstName" value="22">
         <input type="text" id="a1" class="firstName" value="text2">
      </td>
   </tr>
   <tr>
      <td>
         <input type="hidden" id="a1" class="firstName" value="2">
         0000002
      </td>
      <td>
         Suresh
      </td>
      <td>
         Android Developer
      </td>
      <td>
         <input type="hidden" id="a1" class="firstName" value="33">
         <input type="text" id="a1" class="firstName" value="text3">
      </td>
      <td>
         <input type="hidden" id="a1" class="firstName" value="44">
         <input type="text" id="a1" class="firstName" value="text4">
      </td>
   </tr>
</table>

<button id="target">Click me</button>

Jquery

$("#target").click(function() {

    //storage        
    var data = {
        firstName: [],
        lastName: []
    }

    //loop through the td's
    $('table tr').each(function() {

        var tdlen = $(this).children('td').length
        var i = 0;

        $('td', this).each(function() {


            if (i == 0) {
                alert('Emp. ID is : ' + $(this).find("input[type='hidden']").val());
            }
            if (i > 2) {
                alert('ID is : ' +$(this).find("input[type='hidden']").val());
                alert('text value is : ' +$(this).find("input[type='text']").val());
            }
            i++;
        });

    });



});

https://jsfiddle.net/chidamca/ty30j2ju/用于演示

答案 1 :(得分:0)

好的,所以这里的答案是创建一系列隐藏的输入,@ Rasclatt在某种程度上提出的建议。然后很容易通过PHP迭代$ _POST名称。

感谢大家的时间