簡體   English   中英

將HTML轉換為php數組

[英]Convert HTML to php array

我目前有一個帶有表格的表格。 我想將這些值傳遞給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。 有關此內容的更多信息, 。 )。

還要注意,您的提交按鈕的id屬性應該是id="btnSubmit" ,而不是id="#btnSubmit"

不需要jQuery以及所有這些。只需使用HTML <form>標記的默認功能

為此,首先要改變

<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頁面中,將var_dump($_POST)添加到php標簽內的開頭。

單擊提交按鈕后, var_dump()將打印一個數組,其中包含所有傳遞的表單數據。

要檢索您的php頁面中的值,您可以像

echo $_POST['txtName'];

其中txtName是您輸入的名稱。

對於其他輸入類似。

使用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'];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM