user1531158
user1531158

Reputation:

definining $message="" variable

this code all works fine except for I get an undefined variable warning 'message' and I cant figure out a way to define it as it is not a post or session variable or anything that. Thanks

<?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>

Upvotes: 0

Views: 94

Answers (5)

Alexey
Alexey

Reputation: 7247

Put the $message=""; outside the condition.

$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>

Upvotes: 6

Prathamesh mhatre
Prathamesh mhatre

Reputation: 1085

The error you are getting is because you have not defined $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>

Upvotes: 1

Rakesh Shetty
Rakesh Shetty

Reputation: 4578

declare $message on top :-

$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>

Upvotes: 2

J.K.A.
J.K.A.

Reputation: 7404

Put $message="" before if (isset($_POST['submit'])) { } block

Upvotes: 0

Romain
Romain

Reputation: 6420

$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>

Upvotes: 1

Related Questions