Question

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?

Was it helpful?

Solution

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';

OTHER TIPS

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
}
$action = isset($_GET["action"]) ? $_GET['action'] : 'form';
$action = isset($action) && in_array($action, array('form', 'submit')) ? $action : 'form';
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top