javascript - 如何使用 Php 中的 Foreach 或任何其他方式来实现此目标,将 jquery textBox 中的数据插入到 MySql 数据库中?

标签 javascript php jquery mysql

我需要发生以下事情:

  1. A form with multiple input boxes that receives user information.

  2. An "Add User" button that allows for additional user input.

  3. A submit button that will INSERT the data from the form into the designated database column, and creates a new row in the database for each new user.

我很新,不知道如何循环 results.php 页面上的 $_REQUEST 数组来执行此操作。我愿意接受任何建议。预先感谢您。

	   var fname, lname, dob, input, inputCount = 0;

	 $(window).load(function() {
	   newUser();
	 })

	 function newUser() {
	   $('#box >div').hide();
	   inputCount++;
	   input = $('<div data-id="' + inputCount + '">Entry ' + inputCount + '<br><br></div>').appendTo('#box');
	   fname = $('<input type="text" name="fname' + inputCount + '" placeholder="First Name ' + inputCount + '"><br><br>').appendTo(input);
	   lname = $('<input type="text" name="lname' + inputCount + '" placeholder="Last Name ' + inputCount + '"><br><br>').appendTo(input);
	   dob = $('<input type="text" name="dob' + inputCount + '" placeholder="Date Of Birth ' + inputCount + '"><br><br>').appendTo(input);
	 } 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<form action="results.php" method="post">

  <body>
    <span id="box"></span>
    <button type="button" onclick="newUser()">Add User</button>
    <br>
    <br>
    <input type="submit" value="Submit">
  </body>
</form>

最佳答案

您的请求已发送至 PHP。在 $_REQUEST 中搜索请求参数错误。您需要在 $_POST 中搜索它们.

您需要定义一个name属性到要传递的表单字段。在你的newUser javascript 函数将新用户的数据添加到 html 表单中。使用 jQuery,您可以执行类似 $("#box").append(<your code>) 的操作.

您的代码应该类似于 input ,其中有 name以及所需的值。然后,在 results.php 中解析具有给定名称的值并插入必要的值。

关于javascript - 如何使用 Php 中的 Foreach 或任何其他方式来实现此目标,将 jquery textBox 中的数据插入到 MySql 数据库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30963791/

相关文章:

javascript - GetElementsByTagName 在特定类中?

php - 如何通过 Laravel 5 中的 SQL 查询在模型中分配动态属性

php - 正则表达式只允许字符串中的整数和逗号

php - 如何将Json插入mysql数据库?

javascript - onclick和onclientclick web方法在onclick之后执行

javascript - AJAX 未成功从 asp.net 服务器返回

javascript - 如何在 VueJS test-utils parentComponent 中模拟 Vuex 商店

javascript - addEventListener 多重使用

javascript - 绑定(bind)这个的正确方法是什么?

javascript - 如果不透明,则为 PNG 文件区域着色