-5

I am getting an undefined constant error with the following code, for "success".

    if(success)
    {
        echo "<script type=\"text/javascript\">".
            "alert('Form submitted successfully.');".
            "</script>";
    }

Can anyone help me with resolving this issue?

Álvaro González
  • 135,557
  • 38
  • 250
  • 339

5 Answers5

2

Please read doc http://php.net/manual/en/language.constants.php

Your code must be like this (if you want use constants)

define('success', true);
if(success)
{
    echo "<script type=\"text/javascript\">".
        "alert('Form submitted successfully.');".
        "</script>";
}

Or use variables http://php.net/manual/en/language.variables.basics.php

Artem Ilchenko
  • 990
  • 8
  • 20
1

Please try below code.

    $success = "test"; // set your value that you can get.
    if(isset($success) && $success != "")
        {
            echo "<script type=\"text/javascript\">".
                "alert('Form submitted successfully.');".
                "</script>";
        }
Jalpa
  • 687
  • 3
  • 13
0

change success to $success

if($success)
{
    echo "<script type=\"text/javascript\">".
    "alert('Form submitted successfully.');".
    "</script>";
}
Manoj Sharma
  • 1,457
  • 2
  • 13
  • 20
Shanu k k
  • 1,215
  • 1
  • 16
  • 37
0

"success" without $ is a constant, not a variable. And this constant is not initialized with a value to be checked in the if.

0

your variable has to be set befor you go to that if-clause. in your case you want to check if the variable is true and if so it should alert() with the content Form submitted successfully.

there are 2 ways to set a variable.

  1. define a constant with the define("success", true)- function. Then you can let your code as it is.
  2. or define a variable lik $success = true;

then you have to change your code to this:

<?php 

$success = true; // define the variable and set it to true

if ($success) {

    echo "<script type=\"text/javascript\">" .
        "alert('Form submitted successfully.');" .
        "</script>";

} 

?>

or

<?php

$success = true; // define the variable and set it to true

if (isset($success) && !empty($success) && $success != false) {

    echo "<script type=\"text/javascript\">" .
        "alert('Form submitted successfully.');" .
        "</script>";

}

?>
Blueblazer172
  • 551
  • 2
  • 14
  • 43