Php 从循环中提取正确的值
下面你可以看到我的代码。不需要显示其他部分或数据库表 代码Php 从循环中提取正确的值,php,mysqli,Php,Mysqli,下面你可以看到我的代码。不需要显示其他部分或数据库表 代码 我需要从正确的输入中获取值。此代码始终从循环中获取最后一个输入。我已经试着研究了一天了。如何修复此问题?因为输入的名称重复。尝试将输入的名称更改为数组。 例如: <select name='children[]'></select> 请按照以下代码操作: $result = mysqli_query($connection, $query); ?> <form method="POST" acti
我需要从正确的输入中获取值。此代码始终从循环中获取最后一个输入。我已经试着研究了一天了。如何修复此问题?因为输入的名称重复。尝试将输入的名称更改为数组。 例如:
<select name='children[]'></select>
请按照以下代码操作:
$result = mysqli_query($connection, $query);
?>
<form method="POST" action="custInfo.php">
<center><input type="submit" id="submit_button" name="room_submit" value="Submit"></center>
<?php
while ($row = mysqli_fetch_assoc($result)) {
?>
<div class='chooseRoom'>
<div class='indent'>
<h4><?php echo $row['room_type']; ?></h4>
<h5><?php echo $row['room_rate']; ?></h5>
<img src='../images/<?php echo $row['room_type']; ?>.jpg' width="250" height="160">
<h5>Number of Adult
<select name='adult[]'> <!-- NOTICE HERE... -->
<?php
$x = 0;
while($x <= $row['max_cap']){
?>
<option><?php echo $x; ?></option>
<?php
$x++;
}
?>
</select>
Number of Children
<select name='children[]'> <!-- NOTICE HERE... -->
<option>0</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</h5>
<table class='table'>
<thead>
<th>Inclusive</th>
<th>Description</th>
</thead>
<tbody>
<td><?php echo $row['inclusive']; ?></td>
<td><?php echo $row['description']; ?></td>
</tbody>
<tfoot>
<td colspan='2'><?php echo $row['room_status']; ?></td>
</tfoot>
</table>
</div>
</div>
<?php
}
?>
</form>
在上面的代码中,您将看到我们正在循环一些DB行。因此,将有多个成人和儿童选择。因此,在这种情况下,我们将使它们成为末尾带有[]的数组。这将在数组中保留所有同名输入
/**
* After form submittion look at $_POST variable... eg.
*
* echo "<pre>";
* print_r($_POST);
* echo "</pre>";
*/
/**
*
* The $_POST['adult'] and $_POST['children'] will be an array.
* And then you can have whichever value you want by calling the right Index.
*
* Otherwise you can loop through them:
* foreach ($_POST['adult'] as $key => $value) {
* echo $value;
* }
* And ---- for children.
* foreach ($_POST['children'] as $key => $value) {
* echo $value;
* }
*
*/
您计划如何获取价值?通过Jquery或简单的表单提交,我希望一切都围绕着一个表单?是的,它在一个表单中。抱歉,我将编辑此项。我需要从正确的输入中获取值。你想得到什么价值?什么不起作用?
/**
* After form submittion look at $_POST variable... eg.
*
* echo "<pre>";
* print_r($_POST);
* echo "</pre>";
*/
/**
*
* The $_POST['adult'] and $_POST['children'] will be an array.
* And then you can have whichever value you want by calling the right Index.
*
* Otherwise you can loop through them:
* foreach ($_POST['adult'] as $key => $value) {
* echo $value;
* }
* And ---- for children.
* foreach ($_POST['children'] as $key => $value) {
* echo $value;
* }
*
*/