AkiEru
AkiEru

Reputation: 788

Check on $_GET variable fails

I am creating a PHP page, in one page I would like to show the main HTML based on which $_GET["action"]. However whatever which if ($action == "xxx") condition

// Action Judgement
$action = isset($_GET["action"]) ? $action : 'form';
$action = isset($action) && in_array($action, array('form', 'submit')) ? $action : 'form';

if($action == "form") { ?>
    // only html <form>...</form> codes here
<?php } elseif($action == "submit") { ?>
    // only php codes for processing here
<?php } ?>

If I put the URL like this abc.php?action=submit, the HTML codes are showing in the page. Is my coding wrong?

Upvotes: 0

Views: 567

Answers (3)

dialogik
dialogik

Reputation: 9552

Your first line is wrong. Fix it by replacing it by

$action = isset($_GET["action"]) ? $_GET["action"] : 'form';

or you could even improve it by taking

$action = ( isset($_GET["action"]) && !empty($_GET["action"]) ) ? $_GET["action"] : 'form';

or even better (as Ben D suggested)

$action = !empty($_GET["action"]) ? $_GET["action"] : 'form';

Upvotes: 6

user3599730
user3599730

Reputation: 136

You're confusing variables and forms.

if($action == "form") {
    echo "only html <form>...</form> codes here";
} else if($action == "submit") {
    only php codes for processing here
}

Upvotes: 3

Timur
Timur

Reputation: 639

$action = isset($_GET["action"]) ? $_GET['action'] : 'form';
$action = isset($action) && in_array($action, array('form', 'submit')) ? $action : 'form';

Upvotes: 2

Related Questions