ajax - 知道在 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);
}
});
});
});
解决方案
推荐阅读
- c++ - 声明数组而不创建新的 int [size] 时出现 SIGSEGV 错误
- bash - 在 bash shell 脚本中找到最小的素数
- java - 如何在 Windows 10 上设置 jaudiotagger
- swift - 具有可互换属性的可散列结构?
- excel - VBA 代码循环遍历 .csv 文件的文件夹,将数据粘贴到 xlsx 模板中并另存为 .xlsx
- time - SAS时间信息
- javascript - Onclick 找不到要执行的函数
- php - 在 Laravel 表单验证后插入记录
- python - 如何修复 Unix 域套接字“/var/run/postgresql/.s.PGSQL.5432”上的连接?运行 bitbucket 管道时出错
- vba - 如何在 PowerPoint 应用程序中获得字母之间的延迟?