PHP use session to select dropdown option

我们两清 提交于 2021-02-04 19:55:38

问题


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

回答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'; }


来源:https://stackoverflow.com/questions/38108719/php-use-session-to-select-dropdown-option

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!