【发布时间】:2015-09-02 11:29:50
【问题描述】:
我的代码有问题,您能帮帮我吗?我首先要做的是询问用户他是否有在家充电的可能性。如果他的回答是肯定的,我想运行一个包含他拥有的收费选项的文件 (homechargingyes.php),并且我想将他的答案保存在会话变量中。如果他的回答是否定的,将运行另一个文件 (homechargingno.php),并且只会显示一条消息。 这是我的代码文件:
主文件:
Do you have any charging possibility at household?</br>
<form method="post" action="">
<input type="radio" name="chargeathome" value="Yes">Yes
<input type="radio" name="chargeathome" value="No">No
<input type="submit" name="submit" value="Submit">
</form>
<?php
if (isset($_POST["submit"]) and $_POST["chargeathome"]== "Yes"){
include 'homechargingyes.php';
}elseif (isset($_POST["submit"]) and $_POST["chargeathome"]== "No" ){
include 'homechargingno.php';
}
?>
homechargesyes.php:
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<form action="" method="post">
<b>Type of socket:</b><input type="radio" name="typeofsockethome" value="Domestic socket 120V">Domestic socket 120V</br>
<input name="typeofsockethome" type="radio" value="Domestic socket 240V">Domestic socket 240V</br>
<input type="radio" name="typeofsockethome" value="CEE Blue">CEE Blue</br>
<input type="radio" name="typeofsockethome" value="CEE Red">CEE Red</br>
<input type="radio" name="typeofsockethome" value="Wallbox">Wallbox</br>
<b>Hours of continuous battery charge:</b> <input name="charginghourshome" type="number" min="0" max="24" value=""></br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
$_SESSION["chargeathome"]= $_POST["chargeathome"];
if (isset($_POST["submit"])){
$_SESSION["typeofsockethome"]= $_POST["typeofsockethome"];
$_SESSION["charginghourshome"]= $_POST["charginghourshome"];
}
?>
</body>
</html>
还有 homechargingno.php :
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
$_SESSION["chargeathome"]= $_POST["chargeathome"];
echo "There is no charging possibility at home.";
?>
如果用户单击“否”按钮,一切正常。但是,如果他选中“是”,则在按下提交后立即有一个通知: Undefined index: typeofsockethome in homechargingyes.php on line 21 和另一个 Undefined index:charginghourshome in homechargingyes.php on line 22. 如果我忽略这些通知并选择出现上述选项之一并单击提交有关未定义索引的其他通知。这次是关于我的主文件中的变量“chargeathome”。 你能明白什么是错的并帮助我吗?
【问题讨论】:
-
` $_SESSION["typeofsockethome"]= $_POST["typeofsockethome"]; $_SESSION["charginghourshome"]= $_POST["charginghourshome"];`你在这两个来自的页面上只发布了一个值
chargeathome -
K
typeofsockethome这没有定义如何访问它 -
错误消息(通知)非常清楚,它包含不存在的索引名称,以及您尝试访问它们的行。您还需要什么信息?
-
顺便说一句,您永远不应该期望,来自用户的任何东西(例如
GET、POST等)都会一直存在。因此,每当您从那里阅读时,请先检查它是否存在。