我的代码有问题,你能帮我吗?我首先要做的是询问用户他是否有在家充电的可能性。如果他的回答是肯定的,我想运行一个文件(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';
}
?>
homechargingyes.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.";
?>
如果用户点击“否”按钮,一切正常。但如果他检查“是”,则在按下提交后会有一个通知:未定义的索引:第21行的homechargingyes.php中的typeofsockethome和另一个未定义的索引:第22行的充电家庭中的chargehourshome。如果我忽略这些通知并选择上面的选项之一,然后单击提交有关未定义索引的其他通知。这个时间是关于我的主文件中的变量“chargeathome”。 你能理解什么是错的并帮助我吗?
答案 0 :(得分:1)
只需更改homechargingyes.php
<input type="submit" name="submit1" value="Submit">
并改变这个
if (isset($_POST["submit1"])){
$_SESSION["typeofsockethome"]= $_POST["typeofsockethome"];
$_SESSION["charginghourshome"]= $_POST["charginghourshome"];
}
这将解决您的问题
注意:
因为您已经点击了main.php
的提交
<input type="submit" name="submit" value="Submit"/>
当它包含homechargingyes.php
页面时,会检查$_POST['submit']
已发布在$ _POST数组
答案 1 :(得分:0)
试试这个...我压缩了你的代码并将所有内容放在一页上。
之前的部分问题是在页面上显示多个表单,每个表单都询问是否已单击提交按钮,即:$ _POST [&#39;提交&#39;]
无需单独的页面和包含。
这还包括你问Junaid的results.php问题。
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php if (!isset($_POST["update"])) { // if the form has never been submitted ?>
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="hidden" name="update" value="form1">
<input type="submit" name="submit" value="Submit">
</form>
<?php }
if (isset($_POST["update"]) && ($_POST["update"] == "form1") && $_POST['chargeathome'] == 'Yes'){
$_SESSION["chargeathome"] = "Yes";
?>
<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="hidden" name="update" value="form2">
<input type="submit" name="submit2" value="Submit">
</form>
<?php }
if (isset($_POST["update"]) && $_POST['update'] == 'form2'){
$_SESSION["typeofsockethome"]= $_POST["typeofsockethome"];
$_SESSION["charginghourshome"]= $_POST["charginghourshome"];
?>
You do have charging ability at home<br><br>
Your socket type is: <?php echo $_SESSION['typeofsockethome'] ?><br><br>
Your charging time is: <?php echo $_SESSION['charginghourshome'] ?><br><br>
<?php }
if(isset($_POST["update"]) && $_POST["chargeathome"] == "No" ){
$_SESSION["chargeathome"] = $_POST["chargeathome"];
?>
There is no charging possibility at home
<?php } ?>
</body>
希望这有帮助