view raw
Melvin Melvin - 10 months ago 57
PHP Question

PHP use session to select dropdown option

I've got this upload form and would like to keep the selected option from the dropdown in the session in order to show the last selection after submitting, e.g. i choose the option 'colour' und after submitting colour is still selected in the dropdown.
I echo $_SESSION['testname'] (just before the first radio button) and it gives me back "colour", but in the option-tag where i'd like to echo 'selected' if "colour" was the last selection, it returns nothing! what am i missing?

if(isset($_POST['kategorie'])) {
$_SESSION['testname']=$_POST['kategorie']; }


$con = mysqli_connect("localhost","Melvin","") or die ("could not connect to server: " . mysqli_connect_error($con));
mysqli_select_db($con, "galerie") or die ("Could not connect to database: " . mysqli_error($con));


$name = $_FILES['file']['name'];
$sub_name = substr($name, 0, -4);
$img_ext = ".jpg";
$tmp_name = $_FILES['file']['tmp_name'];
$location = '_images/_galerie/';
$target = '_images/_galerie/' .$name;


echo "file uploaded";

$nam = $_POST['nam'];
$kategorie = $_POST['kategorie'];
$size = $_POST['size'];

if ($size == 'thumb') {
// add "thumb" between filename and extension
$extension_pos = strrpos($target, '.'); // find position of the last dot, so where the extension starts
$thumb = substr($target, 0, $extension_pos) . '_thumb' . substr($target, $extension_pos);
$query = mysqli_query($con , "INSERT INTO images(img_name,img_title,img_cat,img_size)VALUES('".$thumb."','$nam','$kategorie','$size')");
} else {
$query = mysqli_query($con , "INSERT INTO images(img_name,img_title,img_cat,img_size)VALUES('".$target."','$nam','$kategorie','$size')");

function renameImg() {
$name = $_FILES['file']['name'];
$target = '_images/_galerie/' .$name;
$extension_pos = strrpos($target, '.');
$thumb = substr($target, 0, $extension_pos) . '_thumb' . substr($target, $extension_pos);
rename($target, $thumb);
//echo $name . " replaced with " . $thumb;

} else {

echo "file not uploaded";



<div style="margin:20px 0 40px 0;">
<form action="upload.php" method="POST" enctype="multipart/form-data">
Upload: <input type="file" name="file">
Title: <input type="text" name="nam" value="Tattoo Gallery">
Category: <select name="kategorie" id="selectKat">
<option value="black" <?php if(isset($_SESSION['kategorie']) == "black") { echo ' selected';} ?>>Black and white</option>
<option value="colour" <?php if(isset($_SESSION['kategorie']) == "colour") { echo ' selected';} ?>>Colour</option>

echo $_SESSION['testname'];

<input type="radio" name="size" value="full" id="regularRadio" checked="checked">
<label for="regularRadio">Full size</label>
<input type="radio" name="size" value="thumb" id="thumbRadio">
<label for="thumbRadio">Thumbnail</label>

<input type="submit" name="submit">

$result = mysqli_query($con, "SELECT * FROM images WHERE img_size='thumb'");

while($row = mysqli_fetch_array($result)){
echo "<img src=".$row['img_name'] . " &nbsp; class='thumbnails' style='display:inline;float:left;'>";



You must not use the isset() function. This function return True or False.

You only have to compare the value of $_POST['kategorie'] or $_SESSION['testname'] (as I see) with your text ("color" or "black"), like this :

if ($_SESSION['kategorie'] == "black") { echo ' selected'; }