【问题标题】:Convert HTML to php array将 HTML 转换为 php 数组
【发布时间】:2015-10-31 07:23:50
【问题描述】:

我目前有一个表格,里面有一个表格。我想将这些值传递给 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 插入。关于如何做到这一点的任何想法?

【问题讨论】:

    标签: javascript php jquery html json


    【解决方案1】:

    在 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"

    【讨论】:

    • 抱歉方法是post,在我的php页面中我有array=json_decode($_POST['json']);我想对数组中的每个项目运行一个 for each 循环。有没有办法让我在不明确设置每个变量的情况下做到这一点?
    • 是的,$_POST 是一个数组,所以你可以这样做:foreach ($_POST as $key =&gt; $value) { echo "$key : $value"; }
    • 现在数据正在输出,但它只是从最后一个 TR 元素中抛出数据。有没有办法从每个地方获取数据?我想要类似 $_POST[0][1]
    • 啊,自从我写了这个答案后,你帖子中的 HTML 已经改变了。如果您有多个同名的表单元素,PHP 就不能很好地处理它; here's some info on ways you can deal with that。基本上,如果您想拥有这样的二维数组,您可能需要将[] 添加到每个“名称”的末尾。
    • 这让我更接近我想要的位置,但它正在捕获输入数组。我试图将表单的内容放在一个数组中。我已将您标记为最佳答案,因为您比以前更接近我!谢谢。
    【解决方案2】:

    使用jQuery ajax 方法;

    function send_form(this) {
       jQuery.ajax({
          type: 'POST',
          url: $(this).attr('action'),
          data: $(this).serialize(),
          error:function(){ console.log('error'); },
          success: function(data) { $console.log(data);}
       });
       return false;
    }
    

    您的表格;

    <form id="pageform" onsubmit="return send_form(this);" action="phpscript.php">
       <table>
          <tbody>
            <tr>
              <td><input type="text" class="nestedInput"name="txtName" value="John"></td>
            </tr>
            <tr>
              <td><input type="text" class="nestedInput" name="txtLocation" value="North St"></td>
           </tr>
           <tr>
             <td><input type="text" class="nestedInput" name="txtAge" value="42"></td>
          </tr>
       </tbody>
    </table>
    

    在phpscript.php中;

    $name = $_POST['txtName'];
    $txtLocation= $_POST['txtLocation'];
    $txtAge= $_POST['txtAge'];
    

    【讨论】:

    • 抱歉,我的示例不准确,我已对其进行了更新,以更准确地显示我正在尝试做的事情。
    【解决方案3】:

    不需要 jQuery 和所有这些......只需使用 HTML &lt;form&gt; 标签的默认功能

    为此,首先,改变

    <input type="button" name="btnSubmit" id="#btnSubmit" value="Submit">
    

    <input type="submit" name="btnSubmit" id="#btnSubmit" value="Submit">
    

    然后为您的表单标签添加一个方法,例如,

    <form id="pageform" action="phpscript.php" method="post">
    

    现在,在您的 phpscript.php 页面中,在 php 标记内的开头本身添加 var_dump($_POST)..

    var_dump() 将在单击提交按钮后打印一个包含所有传递的表单数据的数组。

    要检索 php 页面中的值,您可以这样做

    echo $_POST['txtName'];
    

    txtName 是您输入的名称。

    其他输入也是如此..

    【讨论】:

      猜你喜欢
      • 2019-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-28
      • 2015-08-29
      • 1970-01-01
      • 2016-05-06
      • 2012-06-22
      相关资源
      最近更新 更多