0

我正在从 a 中提取数据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>
4

1 回答 1

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.
于 2013-07-14T06:40:56.303 回答