gpt4 book ai didi

php - 选择后下拉菜单不会填充

转载 作者:行者123 更新时间:2023-11-30 01:29:27 26 4
gpt4 key购买 nike

我正在从 MySQL 数据库 中提取数据,我的脚本会使用不同的维护更改填充选择菜单,并加载包含更改描述的文本区域字段。当页面首次加载时,一切正常,但在用户选择更改类型后,它会动态加载更改的描述,但是不会再次使用更改选项填充选择菜单。

这就是我想要发生的事情:即使在选择事件之后,我也希望选择菜单能够重新加载不同类型的更改。

我使用 php,其中包含 html 和 javascript 来进行事件更改。

有人可以建议我需要什么来纠正我的代码吗?

数据库表是change_type这些列是 changeID changeType 描述

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="keywords" content="" />
<meta name="description" content="" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Change Type</title>
<!--[if IE 6]>
<link href="default_ie6.css" rel="stylesheet" type="text/css" />
<![endif]-->
<script language="JavaScript" type="text/javascript">
function getData(combobox){
var value = combobox.options[combobox.selectedIndex].value;
// TODO: check whether the textarea content has been modified.
// if so, warn the user that continuing will lose those changes and
// reload a new page, and abort function if so instructed.
document.location.href = '?change_type='+value;
}
</script>
</head>


<?php

$conn = mysql_connect("localhost", "username", "password");


if (!$conn) {
echo "Unable to connect to DB: " . mysql_error();
exit;
}

if (!mysql_select_db("change")) {
echo "Unable to select mydbname: " . mysql_error();
exit;
}

error_reporting(E_ALL);

if (!mysql_ping()) {
die ("The MySQL connection is not active.");
}

mysql_set_charset('utf8');

// $_REQUEST is both _GET and _POST
if (isset($_REQUEST['change_type'])) {
$change_type = mysql_real_escape_string($_REQUEST['change_type']);
} else {
$change_type = False;
$query = "SELECT changeID, changeType FROM change_type;";
$exec = mysql_query($query); // You need to be already connected to a DB

if (!$exec) {
trigger_error("Cannot fetch data from change_type table: " . mysql_error(), E_USER_ERROR);
}

if (0 == mysql_num_rows($exec)) {
trigger_error("There are no changes in the 'change_type' table. Cannot continue: it would not work. Insert some changeids and retry.", E_USER_ERROR);
}

$options = '';

while($row = mysql_fetch_array($exec))
{
// if the current pageid matches the one requested, we set SELECTED
if ($row['changeID'] === $change_type)
$sel = 'selected="selected"';
else
{
// If there is no selection, we use the first combo value as default
if (False === $change_type)
$change_type = $row['changeID'];
$sel = '';
}
$options .= "<option value=\"{$row['changeID']}\" $sel>{$row['changeType']}</option>";
}
mysql_free_result($exec);
}
if (isset($_POST['change_data']))
{
$change_data = mysql_real_escape_string($_POST['change_data']);
$query = "INSERT INTO change_type ( changeID, description ) VALUE '{$change_type}', '{$change_data}' ) ON DUPLICATE KEY UPDATE description=VALUES(description);";
if (!mysql_query($query))
trigger_error("An error occurred: " . mysql_error(), E_USER_ERROR);
}
// Anyway, recover its desciption (maybe updated)
$query = "SELECT description FROM change_type WHERE changeID='{$change_type}';";
$exec = mysql_query($query);
// who says we found anything? Maybe this id does not even exist.
if (mysql_num_rows($exec) > 0)
{
// if it does, we're inside a textarea and we directly output the text
$row = mysql_fetch_array($exec);
$textarea = $row['description'];
}
else
$textarea = '';
mysql_free_result($exec);
?>


<body>
<div id="page-wrapper">
<div id="change_type">
<div id="description2">
<h2>Edit Your Description Here</h2>
<script type="text/javascript" src="../ckeditor/ckeditor.js"></script>
<form name="editpage" method="POST" action="email_form.php">
<table border="1" width="100%">
<tr>
<td>Change Type:</td>
<td>
<select name="change_type" onChange="getData(this)"><?php print $options; ?></select>
</td>
</tr>
<tr>
<td><textarea name="description" cols="80" row="8" id="description"><?php print $textarea; ?></textarea></td>
</tr>
<tr>
<td><input type="Submit" value="Save the page"/></td>
</tr>
</table>
</form>
</div>
</div>
</div>
</body>
</html>

最佳答案

您的选择选项不会在 JavaScript 重新加载时填充,因为您的查询位于 else block 内 -

if (isset($_REQUEST['change_type'])) {
$change_type = mysql_real_escape_string($_REQUEST['change_type']);
} else {
$change_type = False;
$query = "SELECT changeID, changeType FROM change_type;";
...

我认为您想在 $change_type = False; 之后关闭 else{ block

我还建议将您的 INSERT 查询移至 SELECT 之前,以便您始终获得最新数据。

此外,您有 if (isset($_POST['change_data'])),但没有 change_data 表单元素,所以我假设您的意思是 描述

<小时/>

所以这是一种方法 -

if (isset($_REQUEST['change_type'])) {
$change_type = mysql_real_escape_string($_REQUEST['change_type']);
}
else {
$change_type = False;
}

if (isset($_POST['description']))
{
$change_data = mysql_real_escape_string($_POST['description']);
$query = "INSERT INTO change_type ( changeID, description ) VALUE '{$change_type}', '{$change_data}' ) ON DUPLICATE KEY UPDATE description=VALUES(description);";
if (!mysql_query($query))
trigger_error("An error occurred: " . mysql_error(), E_USER_ERROR);
}

$query = "SELECT changeID, changeType FROM change_type;";
$exec = mysql_query($query); // You need to be already connected to a DB

... // abbreviated unchanged code.

$query = "SELECT description FROM change_type WHERE changeID='{$change_type}';";
$exec = mysql_query($query);
... // abbreviated unchanged code.

关于php - 选择后下拉菜单不会填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17637034/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com