定义$ message =“”变量

时间:2012-07-20 11:11:32

标签: php

这个代码一切正常,除了我得到一个未定义的变量警告'消息',我无法找到一种方法来定义它,因为它不是一个帖子或会话变量或任何东西。谢谢

<?php

if (isset($_POST['submit']))
{
  $newemail = $_POST['newemail'];
  $repeatnewemail = $_POST['repeatnewemail'];
  $email= ($_SESSION['email']);
  $message="";

 //open database

  if (condition)
  {
    $message="first message";
  }
}

?>

<p td class='td2'><?php echo $message;?></td>

5 个答案:

答案 0 :(得分:6)

$message="";置于条件之外。

$message="";
if (isset($_POST['submit'])){
    $newemail = $_POST['newemail'];
    $repeatnewemail = $_POST['repeatnewemail'];
    $email= ($_SESSION['email']);


 //open database


    if (condition){
        $message="first message";
    }
}

?>
<p td class='td2'><?php echo $message;?></td>

答案 1 :(得分:2)

在上面声明$ message: -

$message = ""
if (isset($_POST['submit']))
{
 $newemail = $_POST['newemail'];
 $repeatnewemail = $_POST['repeatnewemail'];
 $email= ($_SESSION['email']);

 //open database


 if (condition)
 {
  $message="first message";
 }
}

?>
 <p td class='td2'><?php if(isset($message)) echo $message;?></td>

答案 2 :(得分:1)

$message=null;
if (isset($_POST['submit']))
{
 $newemail = $_POST['newemail'];
 $repeatnewemail = $_POST['repeatnewemail'];
 $email= ($_SESSION['email']);
 $message="";

 //open database


 if (condition)
 {
  $message="first message";
 }
}

?>
 <p td class='td2'><?php if(isset($message)) echo $message;?></td>

答案 3 :(得分:1)

您收到的错误是因为您尚未定义$ message。

$message = ""
if (isset($_POST['submit']))
{
 $newemail = $_POST['newemail'];
 $repeatnewemail = $_POST['repeatnewemail'];
 $email= ($_SESSION['email']);

 //open database


 if (condition)
 {
  $message="first message";
 }
}

?>
 <p td class='td2'><?php if(isset($message)) echo $message;?></td>

答案 4 :(得分:0)

$message=""放在if (isset($_POST['submit'])) { }阻止

之前