How to make a form style hidden if a specific session is set? I did it that way
<form action="" method="post" <?php if ((isset($_SESSION['start']) )|| (isset($_SESSION['visitor']) )){ echo 'style="display:none;"'; } ?>>
<input type="radio" name="log" id="viso" value="vis" checked onclick="javascript:yesnoCheck();"> Visitor<br>
<input type="radio" name="log" id="uso" value="use" onclick="javascript:yesnoCheck();" > User<br>
<label name="frm" id="frm" style="display:none" ><b>Password</b></label>
<input type="password" style="display:none" name="pw" id="pw" placeholder="Enter Password" >
<input type="submit" >
</form>
<?php
if ( isset ($_POST['log'])&& $_POST['log']=="vis" )
{
$_SESSION["visitor"]="true";
unset($_SESSION["start"]);
}
else if ( isset ($_POST['log'])&& $_POST['log']=="use" )
{
if ( isset($_POST['pw']) && $_POST['pw']!="1111" )
echo "Wrong password";
else if ( isset ($_POST['pw'])&& $_POST['pw']=="1111" )
{
$_SESSION['start']="Welcome";
unset($_SESSION["visitor"]);
}
}
?>
but it's only hidden if i press on the submit button twice not from the first time
You have a simple logical flaw .You are checking for session before you set them .When user fills form and submits , your php runs this
<form action="" method="post" id="lol" <?php if ((isset($_SESSION['start']) )|| (isset($_SESSION['visitor']) )){ echo 'style="display:none;"'; } ?>>
Note that at this time no session is set so form will not have display:none
and will get displayed **even though user has entered his details correctly still when response come form will be visible.Now to fix the issue move your session check before form display code
<?php
if ( isset ($_POST['log'])&& $_POST['log']=="vis" )
{
$_SESSION["visitor"]="true";
//....more stuff
//...
} ?>
<form action="" method="post" id="lol" <?php if ((isset($_SESSION['start']) )|| (isset($_SESSION['visitor']) )){ echo 'style="display:none;"'; } ?>>