2013-07-14 8 views
0

MySQL database에서 데이터를 가져오고 내 스크립트가 유지 관리 변경 사항이 다른 선택 메뉴를 채우고 변경 내용에 대한 설명이있는 텍스트 영역을로드합니다. 페이지가 처음로드 될 때 모든 것이 작동하지만 사용자가 변경 유형을 동적으로로드하면 변경 설명이 동적으로로드됩니다. BUT은 변경 사항 옵션으로 선택 메뉴를 다시 채우지 않습니다.선택 후 풀다운 메뉴가 채워지지 않습니다.

다음은 내가 원하는 일입니다. 선택 이벤트 후에도 다양한 유형의 변경 사항으로 선택 메뉴가 다시로드되기를 원합니다.

나는 내부 HTML과 이벤트 변경을위한 자바 스크립트로 PHP를 사용하고 있습니다.

내 코드를 수정해야하는 사람에게 조언 해 줄 수 있습니까?

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;"; 
    ... 

나는 당신을 생각 -

데이터베이스 테이블은 쿼리가 다른 블록 내부로 열이, 당신의 선택 옵션은 자바 스크립트 다시로드에 채우기되지 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

GAK -


그래서 여기 한 가지 방법이다. 언젠가는 Java, Spring, Hibernate, JSTL을 사용해보아야합니다.이 끔찍한 껌보다 훨씬 더 명확합니다. –

+0

자바 스크립트 대신 JQuery와 AJAX를 사용하십시오. – Prix

+0

JQuery 나 Ajax에 익숙하지 않아요? 무슨 일이 일어나고 싶은지로 작용할 수있는 무엇인가? –

답변

0

이다 change_type 입니다 else{ 블록을 닫고 싶음 $change_type = False;

나는 또한 reco 할 것이다. 귀하의 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. 
관련 문제