【问题标题】:if (isset($_POST["submit"]) and $_POST["chargeathome"]== "Yes")if (isset($_POST["submit"]) 和 $_POST["chargeathome"]== "Yes")
【发布时间】: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 这没有定义如何访问它
  • 错误消息(通知)非常清楚,它包含不存在的索引名称,以及您尝试访问它们的行。您还需要什么信息?
  • 顺便说一句,您永远不应该期望,来自用户的任何东西(例如GETPOST 等)都会一直存在。因此,每当您从那里阅读时,请先检查它是否存在。

标签: php session post


【解决方案1】:

只需将提交的名称从homechargingyes.php更改为

<input type="submit" name="submit1" value="Submit">

然后修改这个

if (isset($_POST["submit1"])){
    $_SESSION["typeofsockethome"]= $_POST["typeofsockethome"];
    $_SESSION["charginghourshome"]= $_POST["charginghourshome"];
}

这将解决您的问题

注意:

因为您已经点击了main.php 的提交,即 &lt;input type="submit" name="submit" value="Submit"/&gt; 当它包含 homechargingyes.php 页面时,它会检查 $_POST['submit'] 是否已经发布在 $_POST 数组中

【讨论】:

  • 谢谢朱奈德!我能问你点别的吗?如果我想在与主文件链接的另一个子页面(result.php)中使用 $_SESSION["typeofsockethome"],我该怎么办?我认为将其保存为会话变量就可以了,但我无法从 result.php 访问它
  • 如果将此值存储为会话,则可以从 result.php 访问它,但我提醒您必须在页面顶部使用session_start()
  • 我的 result.php 文件是这样的: if ($_SESSION["chargeathome"]== "Yes"){ echo "Type of socket at home:".$_SESSION["typeofsockethome"] ."
    "; echo "在家连续充电的小时数:".$_SESSION["charginghourshome"]."
    "; }else{ 回声“否”; }
  • 我使用过 session_start();在页面顶部。更具体一点,它会打印“是”或“否”的答案和家里的插座类型:和家里连续电池充电的时间:但它们旁边没有值。
  • print_r($_SESSION);这是什么结果
【解决方案2】:

试试这个...我压缩了你的代码并将所有内容都放在一个页面上。

之前的部分问题是页面上显示了多个表单,每个表单都询问是否已单击提交按钮,即:$_POST['submit']

不需要单独的页面和包含。

这还包括您向 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>

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-04
    • 2015-03-05
    • 1970-01-01
    • 2016-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多