[英]How to POST form data from dynamically created inputs?
我正在使用 foreach 生成幾個動態輸入。 每個輸入從文本文件中的數組中循環獲取其name
和id
。
然后將表單數據發送到另一個 PHP 頁面以使用 POST 執行一些數據庫查詢。
我面臨的問題是每個輸入值都返回 NULL。
我不知道發生了什么,因為當我查看網絡選項卡上的 Web 控制台時,我可以看到正在收集參數。
數組.txt
first_name
last_name
occupation
company_name
industry
city
country
countryCode
phone
email
address
stateProvince
postalZipeCode
表單.php
//get array of names and ids for inputs
$array = explode("\n", file_get_contents('array.txt'));
//loop through the array
echo '<form method="POST" action="insert.php">';
foreach($array as $input) {
echo '<label>'.$input.'</label>'
. '<input type="text" id="'.$input.'" name="'.$input.'" required="required" class="form-control" placeholder="'.$input.'">';
echo '<br>';
}
echo '<input type="submit" value="Submit">';
echo '</form>';
網絡選項卡(Web 控制台)
插入.php
if (!empty($_POST)) {
//get variables
if (isset($_POST['first_name'])) {
$first_name= $_POST['first_name']; //1.
}
if (isset($_POST['last_name'])){
$last_name=$_POST['last_name']; //2.
}
if (isset($_POST['occupation'])) {
$occupation=$_POST['occupation']; //3.
}
if (isset($_POST['company_name'])) {
$company_name=$_POST['company_name']; //4
}
if (isset($_POST['industry'])){
$industry = $_POST['industry']; //5
}
if (isset($_POST['city'])) {
$city = $_POST['city']; //6
}
if(isset($_POST['country'])){
$country=$_POST['country']; //7
}
if (isset($_POST['countryCode'])) {
$countryCode = $_POST['countryCode']; //8
}
if (isset($_POST['phone'])) {
$phone = $_POST['phone']; //9
}
if (isset($_POST['email'])) {
$email = $_POST['email']; //10
}
if (isset($_POST['address'])) {
$address = $_POST['address']; //11
}
if (isset($_POST['stateProvince'])) {
$stateProvince = $_POST['stateProvince']; //12
}
if (isset($_POST['postalZipeCode'])) {
$postalZipeCode = $_POST['postalZipeCode']; //13
}
// insert into table
$insertProspectQuery = $conn->prepare("INSERT INTO users (first_name, last_name, occupation, company,industry,city,country,countryCode,phone,email,address,stateProvince,postalZipeCode) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)");
$insertProspectQuery->bind_param('sssssssssssss',$first_name,$last_name,$occupation,$company_name,$industry,$city,$country,$countryCode,$phone,$email,$address,$stateProvince,$postalZipeCode);
$insertProspectQuery->execute();
$insertProspectQuery->close();
$ok = 1;
} else {
//handle error
}
通過按照 MonkeyZeus 的建議使用 var_dump 輸出變量,我發現問題是在每個名稱變量的末尾使用空白空格創建的。
這是 var_dump 的簡短摘要: array(19) { ["first_name "]=> string(6) "John" ["last_name "]=> string(8) "Doe"[.......]
。
我不知道是什么在名稱變量中創建了空格,可能是因為變量是從文本文件創建的,而文本文件在每行末尾添加了空格。
因此,由於它應該是 ["first_name"] 而不是 ["first_name"], $_POST
insert.php
無法識別在insert.php
上發布的變量。
這永遠不會在這個錯誤的用例中運行:
if(isset($_POST["first_name"])){
$first_name = $_POST["first_name"];
}
因為["first_name"] != ["first_name "]
我通過在 foreach 中使用它們之前修剪名稱變量來解決這個問題。
解決方案快速修剪:
表單.php
//get array of names and ids for inputs
$array = explode("\n", file_get_contents('array.txt'));
//trim post variables
$trimmed_array=array_map('trim',$array);
//loop through the array
echo '<form method="POST" action="insert.php">';
foreach($trimmed_array as $input) {//pass the trimmed version of name variables
echo '<label>'.$input.'</label>'
. '<input type="text" id="'.$input.'" name="'.$input.'" required="required" class="form-control" placeholder="'.$input.'">';
echo '<br>';
}
echo '<input type="submit" value="Submit">';
echo '</form>';
問題是 foreach 循環中的$input
包含一個空格,它可能是從array.txt
文件中獲取的。
為了避免將來出現問題,您應該只修剪$input
:
foreach($array as $input) {
$input = trim($input);
// Proceed as normal
}
您可以選擇嘗試修復array.txt
的數據,但如果將來出現錯誤,那么您的應用程序將再次停止工作,因此最好只對從文件中檢索的每個項目應用trim()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.