2013-07-14 95 views
0

我從MySQL database中提取數據,我的腳本填充具有不同維護更改的選擇菜單,並加載textarea字段和更改描述。當頁面首次載入所有內容時,但在用戶選擇更改類型後,它會動態載入更改的描述,但不會再次使用更改選項填充選擇菜單。下拉菜單不會在選擇後填充

這就是我想要發生的事情:即使在選擇事件之後,我也希望選擇菜單可以重新加載不同類型的更改。

我正在使用PHP的HTML內部它和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> 
+0

珏。有一天你應該研究使用Java,Spring,Hibernate,JSTL--它比這個可怕的gumf更加清晰。 –

+0

僅使用JQuery和AJAX代替javascript。 – Prix

+0

我不熟悉JQuery或Ajax你能舉個例子嗎?一些能夠與我想要發生的事情一起工作的東西? –

回答

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. 
相關問題