2013-07-18 31 views
0

用戶檢查區域類型並選擇更改類型;這個腳本會將數據庫中的變更描述細節加載到html中的textarea字段中。將複選框選項附加到從數據庫加載的textarea php

那我接下來要happend是什麼:

的描述文本獲取與不同區域的變化將採取HTML描述文本區域內進行追加。我不希望區域被添加到我的數據庫中的描述數據中。問題是不同區域的附加說明字段不起作用..

我正在使用javascript追加文本。有人能告訴我爲什麼我需要更正我的代碼嗎?

<html> 

<head> 
<script language="JavaScript" type="text/javascript"> 
    // who says we found anything? Maybe this id does not even exist. 
    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; 

      data_center = [], 
      data_centers = ""; 
      inputs = document.getElementsByTagName('input'); 

      for (var x = 0, len = inputs.length; x < len; x++) { 
       { 
        if (inputs[x].type == "checkbox" && inputs[x].name == 'data_center[]') { 
         if (inputs[x].checked === true) { 
          data_center.push(inputs[x].value); 
         } 
        } 
       } 
       data_centers = (data_center.length > 0 ? ': ' + data_center.join(', ') : '') + '.'; 

       document.getElementById('description').value += data_centers; 
       document.getElementById('impact').value += data_centers; 

      } 
    } 
    </script> 

    </head> 


    <?php 

    require_once("db_handler.php"); 

    // $_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` , `changeName` FROM `change`;"; 
$exec = mysql_query($query); // You need to be already connected to a DB 

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

     if (0 == mysql_num_rows($exec)) { 
        trigger_error("There are no changes in the 'change' 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) 
          // who says we found anything? Maybe this id does not even exist. 
            $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['changeName']}</option>"; 
                } 
    mysql_free_result($exec); 
    if (isset($_POST['description'])) 
{ 
$change_data = mysql_real_escape_string($_POST['description']); 
    $query = "INSERT INTO change (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` , `changeName` FROM `change`;"; 
    $exec = mysql_query($query); // You need to be already connected to a DB 

    // abbreviated unchanged code. 

$query = "SELECT `description`, `impact` FROM `change` WHERE `changeID`='{$change_type}';"; 
$exec = mysql_query($query); 

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); 
     $textareaDescription = $row['description']; 
     $textareaImpact = $row['impact']; 
    } else { 
    $textareaDescription = ''; 
    $textareaImpact = ''; 
    } 
    mysql_free_result($exec); 
?> 
    <body bgcolor="white"> 

<form name="changeform" method="post" action="email_form.php"> 
<table> 

<tr valign="top"> 
<td><b>Data Center</b></td> 
<td><input name="data_center[]" type="checkbox" value="[Zone10]"/>[Zone10] 
    <input name="data_center[]" type="checkbox" value="[Zone11]"/>[Zone11] 
</td> 
</tr> 
<tr valign="top"> 
<td><b>Change Type</b></td> 
<td><select id="change_type" name="change_type" onChange="getData(this)""><?php print $options; ?></select></td> 
</tr> 
<tr valign="top"> 
<td><b>Description</b></td> 
<td><textarea name="description" id="description" cols="50" rows="10"><?php print $textareaDescription; ?></textarea></td> 
</tr> 
<tr valign="top"> 
<td><b>Service Impact</b></td> 
<td><textarea name="impact" id="impact" cols="50" rows="10"><?php print $textareaImpact; ?></textarea></td> 
</tr> 
<tr valign="top"> 
<td align="center" colspan="2"><input type="submit" value="submit change" /></td> 

</tr> 
</table> 
</form> 
</body> 

</html> 

回答

0

下面的線條看起來像有可能是一些錯誤:

for (var x = 0, len = inputs.length; x < len; x++) 
    { 
     { 

除非是語法,我只是不熟悉。

有沒有什麼方法可以告訴我們實際的代碼,運行?看這樣的事情有點難以消化,不能看到它在行動。

相關問題