我有一个表单,在开始有2个输入字段和一个按钮来添加新字段,我想保存所有用户传递到数组甚至更好的直接到CSV,因为我将通过和电子邮件发送CSV文件在结束。我已经尝试了很多方法,但没有一个对我有效。
下面是我的代码:index . php<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- STYLES -->
<link href="style.css" rel="stylesheet">
<!-- SCRIPTS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="add_new_fields.js"></script>
<title>Shopping form</title>
</head>
<body>
<div class="container1">
<form id="form-info" method="POST">
<input type="email" name='email' placeholder="Email" required>
<input type="text" name="mytext[]" required id="text1">
</div>
<button class="add_form_field">Add New Field
<span>+</span>
</button>
<input type="submit" class="submit-btn" value="Submit">
</form>
<p id="para"></p>
</body>
</html>
add_new_fields.js
console.log("add_new_field start");
$(document).ready(function () {
var max_fields = 14;
var wrapper = $(".container1");
var add_button = $(".add_form_field");
var x = 0;
$(add_button).click(function (event) {
event.preventDefault();
if (x < max_fields) {
x++;
$(wrapper).append('<div><input type="text" name="mytext[] id="text1"/><a href="#" class="delete">Delete</a></div>'); //add input box
} else {
alert('You Reached the limits. 15 fields are maximum.')
}
});
$(wrapper).on("click", ".delete", function (event) {
event.preventDefault();
$(this).parent('div').remove();
x--;
})
});
一种进入php的方法将表单操作设置为PHP文件我创建一个php文件form。php因此,在代码中首先,我设置了表单动作
<form action="form.php" id="form-info" method="POST">
<input type="email" name='email' placeholder="Email" required>
<input type="text" name="mytext[]" required id="text1">
</div>
<button class="add_form_field">Add New Field
<span>+</span>
</button>
<input type="submit" class="submit-btn" value="Submit">
</form>
用于获取PHP文件中的值form.php文件的代码应该像这样
<?php
//save the email in a variable email
$email= $POST_["email"]; //here email inside [] is the name of input field
//then save text in variable text
$text = POST_["mytext[]"];
//then save it in a array named input
$input= array($email,$text);
?>