2

我有这个 html 表单:

    <form method="post">
        <input type="radio" name="news" id="new" value="nuova" onchange="newstype(this.id);">Nuova News
        <input type="radio" name="news" id="mod" value="modifica" onchange="newstype(this.id);">Modifica News

        <select name="news" id="modifica" style="display:none" onchange="shownews(this.value)">
            <?php
                include "../flock/sql.php";
                $connection = new mysqli($host, $user, $pw, $db) or die("Impossibile connettersi"); 
                $querylistanews = "SELECT * FROM NEWS ORDER BY id DESC";
                $listanews = $connection->query($querylistanews);                       
                    print '<option>Seleziona una news...</option>';                     
                while ($newsdamodificare = $listanews->fetch_object()) {
                    print '<option value="'.$newsdamodificare->id.'">'.$newsdamodificare->data." - ".$newsdamodificare->title.'</option>';
                }
                $listanews->close();
                $connection->close;      
            ?>    
        </select>
    </form>

这个javascript:

function newstype(param) {
    if(param == 'new') {
        document.getElementById('crea').style.display = 'inline';
        document.getElementById('modifica').style.display = 'none';
        document.getElementById('newsdamodificare').style.display = 'none';
    } else {
        if(param == 'mod') {
        document.getElementById('crea').style.display = 'none';
        document.getElementById('modifica').style.display = 'inline';
        document.getElementById('newsdamodificare').style.display = 'inline';
        }
    }
}

function shownews(str) {

    if (str=="") {
      document.getElementById("newsdamodificare").innerHTML="";
      return;
    } 

    if (window.XMLHttpRequest) {        // codice per IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();     
    } else {                            // codice per IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange=function() {
      if (xmlhttp.readyState==4 && xmlhttp.status==200) {
        document.getElementById("newsdamodificare").innerHTML=xmlhttp.responseText;
      }
    }

xmlhttp.open("GET","modifica.php?news="+str,true);
xmlhttp.send();
}

这个php:

<head>
<script type="text/javascript" src="../editor/ckeditor.js"></script>
<script type="text/javascript" src="/js/charscounter.js"></script>
</head>

<?php
$news=$_GET["news"];
include "../flock/sql.php";

$connection = new mysqli($host, $user, $pw, $db) or
  die('Impossibile connettersi al database: ' . mysql_error());

$newsdaldatabase="SELECT * FROM NEWS WHERE id = '".$news."'";

$result = $connection->query($newsdaldatabase);

$count = mysqli_num_rows($result);

if($count==1){
    while($dati = mysqli_fetch_array($result)) {
      $id = $dati['id'];
      $data = $dati['data'];
      $title = $dati['title'];
      $body = $dati['body'];
    }
} else {
    die('Errore del sistema. Più di una news selezionate: ' . mysql_error());
}
mysqli_close($connection);
?>

<div class="normal" id="modifica">
<table style="width:100%;height:100%;">
<tr>
<td colspan="3" border="0">
    <strong class="confirm">Modifica news</strong>
</td>
</tr>

<tr>
<td width="107" align="right">
    <strong>Data</strong>
</td>
<td colspan="2">
<form name="modificanews" method="post" action="italiano.php?modifica=yes">
    <input name="idmodificanews" type="text" style="display:none" value="<?php echo $id ?>">
    <input name="datanewsmodificata" type="text" maxlength="10" size="8" value="<?php echo $data ?>"> gg.mm.aaaa
</td>
 </tr>

<tr>
<td align="right">
    <strong>Titolo</strong>
</td>
<td width="360">
    <input name="modificatitolo" type="text" maxlength="50" size="50" value="<?php echo $title ?>" onKeyPress="return taLimit(this)" onKeyUp="return taCount(this,'myCounter')">
</td>
<td width="522">
          <b><span id=myCounter>50</span></b> caratteri rimanenti per il titolo</font>
</td>
</tr>

<tr>
<td colspan="3">
    <textarea name="modificatesto"><?php echo $body ?></textarea>
        <script>
            CKEDITOR.replace('modificatesto');
        </script>
</td>
</tr>

一切都很好,除了一件事:当我选择要编辑的新闻时,来自服务器的内容被正确地发布在 php 上,但在 HTML 上,php 中的脚本(CKEDITOR 和 Char Cunter)不活动。

所以,在我的 HTML 上,如果用户将编辑新闻,我只得到一个简单的文本区域而不是 CKEDITOR 等。

有没有办法解决这个问题?你能以某种方式帮助我吗?

谢谢!

编辑:我自己解决了这个问题。将解决方案视为答案。

4

2 回答 2

1

对于那些有同样问题的人:

我通过以下方式解决了我的问题:在javascript上添加一个新行:

document.getElementById("newsdamodificare").action=CKEDITOR.replace('modificatesto');

在这 2 行之间:

document.getElementById("newsdamodificare").innerHTML=xmlhttp.responseText;
NEW LINE TO INSERT HERE
}

并从 php 中删除这些行:

<script>
    CKEDITOR.replace('modificatesto');
</script>

希望这可以帮助!

于 2013-01-28T02:43:29.623 回答
0

您正在尝试通过name选择您的 textarea ,但CKEDITOR.replace('modificatesto');正在寻找一个ID

尝试

<textarea id="modificatesto"><?php echo $body ?></textarea>
    <script>
        CKEDITOR.replace('modificatesto');
    </script>
于 2013-01-27T07:23:56.513 回答