我有一个动态的表单,用户可以在其中向表单添加其他行。最终,我想获取这些数据并将其提交到我的数据库中,其中包含插入、更新或删除的条件,但现在我只想检索数组值。每当我点击提交时,它只会进入一个空白的白页。
这是表格:
<form id="" class="form" name="parts" method="post" action="http://website.com/home/wp-content/themes/Avada-child/update-list.php">
<table class="table table-bordered">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>County</th>
<th>Street</th>
<th>City</th>
<th>Ward</th>
<th>Precinct</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr>
<td style="display:none;">
<input type="hidden" class="form-control" name="username[]" id="username" value="user">
</td>
<td>
<input onkeyup="valid(this)" onblur="valid(this)" type="text" class="form-control" name="first_name[]" id="first_name">
</td>
<td>
<input onkeyup="valid(this)" onblur="valid(this)" type="text" class="form-control" name="last_name[]" id="last_name">
</td>
<td>
<input onkeyup="valid(this)" onblur="valid(this)" type="text" class="form-control" name="county[]" id="county">
</td>
<td>
<input onkeyup="valid(this)" onblur="valid(this)" type="text" class="form-control" name="street[]" id="street">
</td>
<td>
<input onkeyup="valid(this)" onblur="valid(this)" type="text" class="form-control" name="city[]" id="city">
</td>
<td>
<input onkeyup="valid(this)" onblur="valid(this)" type="text" class="form-control" name="ward[]" id="ward">
</td>
<td>
<input onkeyup="valid(this)" onblur="valid(this)" type="text" class="form-control" name="precinct[]" id="precinct">
</td>
<td style="display:none;">
<input type="hidden" class="form-control" name="id[]" id="id">
</td>
<td style="display:none;">
<input type="hidden" class="form-control active" name="is_active[]" id="is_active" value="yes">
</td>
<td>
<a class="delete" title="" data-toggle="tooltip" data-original-title="Delete"><i class="material-icons"></i></a>
</td>
</tr>
</tbody>
</table>
<div class="col-lg-12" style="text-align:right; padding-right:0;">
<button type="button" class="btn btn-info add-new"><i class="fa fa-plus"></i> Add New</button>
</div>
<div style="float:left; width:100%; text-align:right; padding-bottom:24px; padding-top:24px;">
<input type="submit" id="savey" name="submit" value="submit" border="0">
</div>
</form>
这是我的表单操作文件:
<?php
require_once '../../../wp-load.php';
global $wpdb;
$newdb = new wpdb( 'user' , 'pass' , 'table' , 'localhost' );
if(isset($_POST['submit'])) {
$username = $_POST['username'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$county = $_POST['county'];
$street = $_POST['street'];
$city = $_POST['city'];
$ward = $_POST['ward'];
$precinct = $_POST['precinct'];
$id = $_POST['id'];
$is_active = $_POST['is_active'];
if(is_array($_POST['submit'])) {
foreach($username as $key ) {
echo $key . "<br>";
}
}
}
?>
如果我没有名称作为 name[],我可以让表单行回显,以便我知道表单正在发布数据。但是一旦我将名称转换为数组,name[]我似乎无法获取数据。我做错了什么?
您需要通过索引获取所有字段:
<?php
require_once '../../../wp-load.php';
global $wpdb;
$newdb = new wpdb( 'user' , 'pass' , 'table' , 'localhost' );
if(isset($_POST['submit'])) {
$username = $_POST['username'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$county = $_POST['county'];
$street = $_POST['street'];
$city = $_POST['city'];
$ward = $_POST['ward'];
$precinct = $_POST['precinct'];
$id = $_POST['id'];
$is_active = $_POST['is_active'];
if(is_array($_POST['username'])) {
foreach($username as $index => $value) {
// You can get all other items by array[$index];
// Example: $street[$index]
// If you include array items inside a string
// enclose it between curly braces
echo "Username: $value, First name: {$first_name[$index]}<br>";
}
}
}
?>
它必须是:
$first_name = $_POST['first_name'][];