1

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?

<?php
session_start();
if(isset($_POST['kategorie'])) {
$_SESSION['testname']=$_POST['kategorie']; }
?>

<?php

$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));

if(isset($_POST['submit'])){

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

	if(move_uploaded_file($tmp_name,$location.$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;
		};
		renameImg();
		
	} 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>                   
        </select>
        
        	<br>
            <?php 
				echo $_SESSION['testname'];
			 ?>
        	
        <input type="radio" name="size" value="full" id="regularRadio" checked="checked">
        <label for="regularRadio">Full size</label>
        <br>        	
        <input type="radio" name="size" value="thumb" id="thumbRadio">
        <label for="thumbRadio">Thumbnail</label>
        <br>
        
        <input type="submit" name="submit">
    </form>
</div>

<?php
$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;'>";
		
}
?>

4
  • When I run the code here I see that there is a > before the value in the optionbox. So >black and white. You could try and add a space between the end php tag and the end option >. <option value="colour" <?php if(isset($_SESSION['kategorie']) == "colour") { echo selected';} ?> >. If I remember correct, I had a similar issue with radiobuttons Commented Jun 29, 2016 at 19:53
  • thx, unfortunately it didn't do the job Commented Jun 29, 2016 at 19:58
  • Sorry did not notice the isset as papang answered with, try it without isset and with the space Commented Jun 29, 2016 at 20:02
  • Also, what happens if you do like: <option value="colour" <?php if("colour" == "colour") { echo ' selected';} ?> >Colour</option> not sure that actually works, but what I mean is not using the session and hard coding it Commented Jun 29, 2016 at 20:03

1 Answer 1

1

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'; }
Sign up to request clarification or add additional context in comments.

2 Comments

thx for your reply, i tried it like you suggested before, but i get that undefined index notice, that's why i tried with "isset"...
never mind, i'm sorry, it actually did work!! thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.