我遇到以下代码问题。变量$ houseid在第二个if语句中显示为空,但可以在第一个中调用。我需要在第二个if语句中达到$ houseid变量。我已经在它们之外和第一个中进行了测试,它似乎在两者中都能正常工作。
$landlordid = $_SESSION['landlordid'];
$con = mysqli_connect("mysql1616int.cp.blacknight.com","***","***","***"); // Connect to the MySQL server
$mysql = mysqli_query($con, "SELECT * FROM houseInfo WHERE landlordID = '{$landlordid}'");
echo '<form method="POST" action=""><select name="pickhouseid">';
while($row = mysqli_fetch_array($mysql)){
echo '<option value="' . $row['houseID'] . '">'. $row['housename'] . '</option>';
}
echo '</select>
<input type="submit" name="houseselect" value="Select">
</form>';
echo "<br>";
$houseid = $_POST['pickhouseid']; ///where house is set.
if(isset($_POST['houseselect'])){
echo "<form method='POST' action=''><select name='services'><option value='gas1'>Gas Reading</option>
<option value='water1'>Water Reading</option><option value='elec1'>Electricity Reading</option>
</select><input type='submit' value='Select'></form>";
echo "<br>";
echo $houseid; /// returns correct value
}
if (($_POST['services']) == 'gas1') {
echo "something";
echo $houseid; ///doesn't work, appears as empty.
}
任何人都可以帮忙解决这个问题吗?谢谢。
答案 0 :(得分:3)
表单只提交此表单中的内容,而不是页面上的其他表单。
$houseid
(在你的最后一个if中)现在应该是NULL
所以回应它应该不输出任何内容。
因此,请将有关$houseid
的信息添加到其他表单中。 $_POST
数据不会通过多个请求传递。
echo "<form method='POST' action=''><select name='services'><option value='gas1'>Gas Reading</option>
<option value='water1'>Water Reading</option><option value='elec1'>Electricity Reading</option>
</select><input type='submit' value='Select' />
<input type='hidden' value='$houseid' name='houseid' /> <!-- here was added -->
</form>";
答案 1 :(得分:0)