在php的下拉菜单中保存选定的选项 [英] Save selected option in dropdown menu in php

查看:34
本文介绍了在php的下拉菜单中保存选定的选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将图像插入下拉菜单中的选定类别.不想在 html 中对菜单进行硬编码,因此它可以由 phpmyadmin 动态更新为菜单.目前,所有图像都插入 Greeting_Cards 表中,因为它现在是一个占位符.

I want to insert an image into a selected category in the drop down menu. Didn't want to hard code the menu in html so it can be dynamically updated by phpmyadmin to menu. Currently, all images insert in Greeting_Cards table because it is a place holder for now.

我尝试将其保存为变量:$selected = $_POST['tables']; 并将其作为 $selected 而不是 greeting_cards 传递,但这会引发返回未定义索引的通知,并且根本不添加到任何表中.

I have tried saving it as a variable: $selected = $_POST['tables']; and passing it as $selected rather than greeting_cards, but that throws back a notice of undefined index and doesn't add to any table at all.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>upload</title>

</head>

<body>

<form action="upload.php" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit" name="submit" value="upload" />
</form>


<?php
require_once('/var/db_file.php');

//To do after submit button
if(isset($_POST['submit']))
{
	mysql_connect("localhost","root", $pass);
	mysql_select_db("images");

	$imageName = mysql_real_escape_string($_FILES["image"]["name"]);
	$imageData = mysql_real_escape_string(file_get_contents($_FILES["image"]["tmp_name"]));
	$imageType = mysql_real_escape_string($_FILES["image"]["type"]);

	/* Drop down menu */
	$dbname = "images";
	$sql = "SHOW TABLES FROM $dbname";
	$result = mysql_query($sql);
	$tableNames=array();

	while($row = mysql_fetch_row($result)){
		$tableNames[] = $row[0];
	}

	echo '<select name="tables" id="tables">';
	foreach($tableNames as $name){
		echo '<option value="' . $name . '">' . $name . '</option>';
	}
	echo '</select>';
	/* Drop down menu end */

    $selected = $_POST['tables'];

	echo '<br>';

	if(substr($imageType,0,5) == "image"){
		mysql_query("INSERT INTO `$selected` VALUES('','$imageName','$imageData')");
		echo "Image Uploaded!";
	}
	else{
		echo "Has to be an image!";
	}
}


?>

</body>
</html>

编辑 1:添加了 $selected 变量,而不是 Greeting_Cards修复:移动表单的结束标记以包含 php 代码.感谢您的帮助!

推荐答案

在这个代码片段中,$_POST['tables'] 没有得到赋值,因为表单没有名为表格的选择下拉菜单.

In this code snippet, the $_POST['tables'] is not getting the assigned value, as the form does not have the select dropdown named tables.

尽管下拉列表被回显,但它在

之外,因此不会被提交.

Despite the dropdown is being echoed, it is outside the <form>, thus not being submitted.

这篇关于在php的下拉菜单中保存选定的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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