Reputation: 788
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
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
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
Reputation: 639
$action = isset($_GET["action"]) ? $_GET['action'] : 'form';
$action = isset($action) && in_array($action, array('form', 'submit')) ? $action : 'form';
Upvotes: 2