Absar A. Khan Absar A. Khan - 6 months ago 11
HTML Question

Disable input fields if a variable is not set

I have a webpage which allows user to upload an image ONLY IF he is logged in , I'm using session_start() to get my variable $_SESSION['username']. How can I set my text image field such that its only enabled/visible when there is a set value for $_SESSION['username'].

My code below:

<form action="uploadtofile.php" method="post" enctype="multipart/form- data">
<input id="bc0" type="file" name="fileToUpload" id="fileToUpload" >
<input id="bc1" type="submit" value="Upload Image" name="submit" >
</form>
session_start();
if(isset{$_SESSION['username']{


.bc01.visibility="hidden}

Answer

Using the code below, the form will only display if the user is logged in.

<?php
session_start();
if(isset($_SESSION['username'])) {
echo'
<form action="uploadtofile.php" method="post" enctype="multipart/form-data">
<input id="bc0" type="file" name="fileToUpload" id="fileToUpload">
<input id="bc1" type="submit" value="Upload Image" name="submit">
</form>
';
}
?>