PHP使用会话选择下拉选项 [英] PHP use session to select dropdown option

查看:56
本文介绍了PHP使用会话选择下拉选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此上传表单,并希望保留会话中下拉菜单中的所选选项,以便在提交后显示最后的选择,例如在下拉菜单中仍然选择了提交颜色后,我选择了颜色"选项.我回显$ _SESSION ['testname'](就在第一个单选按钮之前),它给我返回颜色",但是在选项标签中,如果颜色"是最后一个选择,我想回显选择",它什么也不返回!我想念什么?

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

推荐答案

不得使用isset()函数.此函数返回True或False.

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

您只需将$ _POST ['kategorie']或$ _SESSION ['testname'](如我所见)的值与您的文本(颜色"或黑色")进行比较,就像这样:

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

这篇关于PHP使用会话选择下拉选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆