【问题标题】:pull-down menu does not get populated after selection选择后不填充下拉菜单
【发布时间】:2013-07-14 06:17:05
【问题描述】:

我正在从MySQL database 中提取数据,我的脚本会使用不同的维护更改填充选择菜单,并在 textarea 字段中加载更改说明。当页面第一次加载一切正常,但在用户选择更改类型后,它会动态加载更改的描述,BUT 不会再次使用更改选项填充选择菜单。

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

我正在使用带有 html 的 php 和用于事件更改的 javascript。

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

数据库表是change_type 列是changeID changeType description

<!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>

【问题讨论】:

  • 嘎克。有一天,你应该考虑使用 Java、Spring、Hibernate、JSTL——它比这种可怕的口香糖要清楚得多。
  • 使用 JQuery 和 AJAX 而不是单独使用 javascript。
  • 我对JQuery或Ajax不熟悉,你能举个例子吗?可以满足我想要发生的事情?
  • @BryceTurng 这里有很多示例搜索这两个术语,您会找到使用 ajax 和 jquery 的下拉示例。

标签: php mysql html textarea onchange


【解决方案1】:

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

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{

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

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


所以这是一种方法-

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.

【讨论】:

    猜你喜欢
    • 2012-07-24
    • 2014-12-11
    • 1970-01-01
    • 2022-11-17
    • 2012-02-23
    • 1970-01-01
    • 2015-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多