选择后,下拉菜单不会填充

时间:2013-07-14 06:17:05

标签: php mysql html5 textarea onchange

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

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

我在其中使用带有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>

1 个答案:

答案 0 :(得分:0)

您的选择选项不会在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;";
    ...

我想您想在else{

之后立即关闭$change_type = False;

我还建议您将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.
相关问题