Question

In my member.php page I have the following alerts:

if (isset($_GET['success']) == 'no')
        {
            echo"<script type=\"text/javascript\">".
                "alert('You can't remove any more services! Please contact admin if you would like your account disabled');".
                "</script>";

        }        
        if (isset($_GET['success']) == 'yes') {
            echo"<script type=\"text/javascript\">".
                "alert('Your details have been updated!');".
                "</script>";

        }

In delete.php I use the following code: (relevant code only)

if($numJobs < 2)
                {
                    header('Location: member.php?success=no&username='.$username);
                    exit();
                }

In update.php I use this code:

if(success){
                 header('Location: member.php?success=yes&username='.$username);
                 exit ();
                }

No matter how I edit the alerts, the alert 'Your details have been updated!' is all that is executed. In the URL I can see the success = no so I don't understand why its not working.

Was it helpful?

Solution

if (isset($_GET['success']) == 'no')

will always be false, since isset($_GET['success']) is either true or false and compared to the string 'no' the whole expression will evaluate to false.

Change it to

if (isset($_GET['success']) && $_GET['success'] == 'no')

like akam correctly stated in the first comment to your question.

OTHER TIPS

Your first block has a syntax error; you need to escape the single quote, since the string is single-quoted.

Note the syntax highlighting here:

alert('You can't remove any more services! Please contact admin if you would like your account disabled');

Escaped:

alert('You can\'t remove any more services! Please contact admin if you would like your account disabled');

However, alerts are:

  • Annoying
  • A bad way to convey important information

Just add the messages as <div>s within the page, and stylize those or pop them up with JavaScript afterwards. Accessible and less frustrating!

Also, comparing the result of isset against something is usually wrong, and you did it twice. It seems like that should read, all told:

if (!isset($_GET['success']) || $_GET['success'] === 'no') {
    echo '<script type="text/javascript">',
         'alert("You can\'t remove any more services! Please contact admin if you would like your account disabled");',
         '</script>';
}
else {
    echo '<script type="text/javascript">',
         'alert("Your details have been updated!");',
         '</script>';
}

Note how I swapped the quotes there; otherwise, you'd have to escape things twice.

If you change success to 1 and 0 instead of yes and no, then you can just have

if ($_GET['success'])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top