gpt4 book ai didi

php - php中的多个输入

转载 作者:行者123 更新时间:2023-11-30 07:18:36 25 4
gpt4 key购买 nike

我正在尝试做的事情:

  1. 我们在页面上有一个输入
  2. 我们按下“添加”按钮,然后再添加一个输入
  3. 在提交时我们获取它们的所有值并将它们添加到一个数组中。

如何在页面上添加额外的输入(使用 jquery),然后在 php 中获取它们的内容?

最佳答案

您不需要显式地将输入添加到数组中。如果您正确命名输入字段,PHP 会自动将它们解析为一个数组。

HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>jQuery dynamic input sample</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script type="text/javascript">

// Input adding function
function addInput() {
$('#inputs').append('<p><input type="text" name="input[]"></p>');
}

// Event handler and the first input
$(document).ready(function () {
$('#adder').click(addInput);
addInput();
});

</script>

</head>
<body>
<form id="form" method="post" action="jquery.php">
<a id="adder" href="#">Add Input</a>
<div id="inputs">
</div>
<input type="submit" />
</form>
</body>
</html>

PHP

foreach ($_POST['input'] as $key => $value) {
echo "$key $value";
}

关于php - php中的多个输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3694848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com