首页 > 解决方案 > 知道在 ajax 中提交了哪个表单

问题描述

我需要知道发送的是哪种表格。我在一个循环中创建它们,因此它们有几个同名。我已经通过将索引 ($ i, $ j) 与表单名称连接起来解决了这个问题,但我想知道是否有更简单的方法来解决它。

菜单.php

<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="estilos.css">
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="ajax.js"></script>
</head>
<body>
    <?php
    $restaurante=$_GET['restaurante'];
    chdir(getcwd()."/".$restaurante);
    $id_fichero= @fopen("menus.txt","r") or die("<B>El fichero no se pudo abrir");
    ?>
    <?php
        while (!feof($id_fichero))
        {            
        if(($linea=trim(fgets($id_fichero,256),"\n\r"))!=false) 
        {
            echo("<button type='button' class='accordion'>".$linea."</button>");
            echo("<div class='panel'>");
            $id_fichero2= @fopen($linea.".txt","r") or die("<B>El fichero no se pudo abrir");
            while (!feof($id_fichero2))
            {
                echo("<form method='post' id='platos'>");//I need to know which of these forms was sent
                if(($linea2=trim(fgets($id_fichero2,256),"\n\r"))!=false)
                {
                    echo("<button type='button' id='plato' name='plato' class='plato'>".$linea2."</button><br>");
                    echo("<input type='hidden' id='nombrePlato' name='nombrePlato' value='".$linea2."'>");
                }
                echo("</form>");
            }
            echo ("</div>");
        }
        }            
    ?>
    <div id="carrito">
        
    </div>
<script src="collapsible.js"></script>
</body>
</html>

ajax.js

$(document).on('ready',function(){    
    $('.plato').click(function(){
    var url = "anadirCarrito.php";
    $.ajax({                        
       type: "POST",                 
       url: url,                     
       data: $('#platos').serialize(), 
       success: function(data)             
       { 
         $('#carrito').html(data);  
       }
       });
    });
});

标签: ajaxforms

解决方案


推荐阅读