javascript - 将 HTML 转换为 php 数组

标签 javascript php jquery html json

我目前有一个表格,里面有一个表格。我想将这些值传递给 php 脚本。我最好的方法是什么?我搜索过的所有内容均不适用。

这就是我格式化表单的方式:

<form id="pageform" action="phpscript.php" method="post">
  <table>
    <tbody>
      <tr>
        <td><input type="text" class="nestedInput"name="txtName" value="John"></td><td><input type="text" class="nestedInput" name="txtLocation" value="North St"></td><td><input type="text" class="nestedInput" name="txtAge" value="42"></td>
      </tr>
      <tr>
        <td><input type="text" class="nestedInput"name="txtName" value="John"></td><td><input type="text" class="nestedInput" name="txtLocation" value="North St"></td><td><input type="text" class="nestedInput" name="txtAge" value="42"></td>
      </tr>
      <tr>
        <td><input type="text" class="nestedInput"name="txtName" value="John"></td><td><input type="text" class="nestedInput" name="txtLocation" value="North St"></td><td><input type="text" class="nestedInput" name="txtAge" value="42"></td>
      </tr>
    </tbody>
  </table>
  <input type="button" name="btnSubmit" id="btnSubmit" value="Submit">
</form>

jQuery:

$("#btnSubmit").click(function(){

  var array = [];

  $(".nestedInput").each(function(n){
    array[n] = $(this).val();
  });

  document.forms["pageform"].submit();

});

我的 PHP:

<?php
  $array=json_decode($_POST['json']);
  print_r($array);    
?>

我想做的是使用每个 tr 中每个输入的值运行 mysqli 插入。关于如何做到这一点有什么想法吗?

最佳答案

在 phpscript.php 中,您可以像这样访问这些变量:

$name = $_GET['txtName']
$location = $_GET['txtLocation']
$age = $_GET['txtAge']

通过表单提交的变量将存储在全局数组 $_GET 或 $_POST 中(取决于您的表单使用 GET 还是 POST。您的表单没有定义该变量的 method 属性,所以它默认为 GET。More on this here. )。

还需要注意的是,您的提交按钮的 id 属性应该是 id="btnSubmit",而不是 id="#btnSubmit"

关于javascript - 将 HTML 转换为 php 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31884671/

相关文章:

javascript - 使用 SetInterval 进行无限循环

javascript - 使用 Node 、mysql、express 和纯 js 从数据库中删除一行

javascript - 使用 ref 输入 React 组件? typescript

javascript - 当主复选框被选中时,如何使复选框可见?

php - 使用 %03.3f 填充不起作用

javascript - 参数数据为空 Ajax 到 MVC Controller

jquery - animate.css 使用 jquery 失败

jquery - 如何从 SignalR 调用多个集线器方法

javascript - 控制台日志不适用于 Netbeans(Android 模拟器和 Cordova)

php - 在 php 中解析非常大的 XML 文件