2016-07-01 11 views
1

TinyMCEからフォームデータを送信しようとしましたが、スクリプトは新しい値ではなく古い値を送信します。 フォームコード:GetElementByID with textarea

<form> 
    <textarea id="tml" name="template_content" class="editor_large">{$template_content|escape}</textarea> 
</form> 

<input class="button_green button_save" type="button" name="save" value="Save" /> 

JSコード:

$(function() { 
    // Saving AJAX code 
    function save() 
    { 
     $('#tml').css('background-color','#e0ffe0'); 
content=document.getElementById("tml").innerHTML   
     $.ajax({ 
      type: 'POST', 
      url: 'ajax/save_template.php', 
      data: {'content': content, 'theme':'{/literal}{$theme}{literal}', 'template': '{/literal}{$template_file}{literal}', 'session_id': '{/literal}{$smarty.session.id}{literal}'}, 
      success: function(data){ 

       $('#tml').animate({'background-color': '#000000'}); 
      }, 
      dataType: 'json' 
     }); 
    } 

    // Press Save 
    $('input[name="save"]').click(function() { 
     save(); 
    }); 

    // ctrl+s 
    var isCtrl = false; 
    var isCmd = false; 
    $(document).keyup(function (e) { 
     if(e.which == 17) isCtrl=false; 
     if(e.which == 91) isCmd=false; 
    }).keydown(function (e) { 
     if(e.which == 17) isCtrl=true; 
     if(e.which == 91) isCmd=true; 
     if(e.which == 83 && (isCtrl || isCmd)) { 
      save(); 
      e.preventDefault(); 
     } 
    }); 
}); 

私が考えるほど、私のスクリプトはまた、CSSを経由して色を変更する必要がありますが、それはまたdidntの。 Save_template.phpファイル:私の意見では

<?php 

session_start(); 

chdir('../..'); 
require_once('api/Simpla.php'); 

$simpla = new Simpla(); 

if(!$simpla->managers->access('design')) 
    return false; 

// Проверка сессии для защиты от xss 
if(!$simpla->request->check_session()) 
{ 
    trigger_error('Session expired', E_USER_WARNING); 
    exit(); 
} 
$content = $simpla->request->post('content'); 
$template = $simpla->request->post('template'); 
$theme = $simpla->request->post('theme', 'string'); 

if(pathinfo($template, PATHINFO_EXTENSION) != 'tpl') 
    exit(); 

$file = $simpla->config->root_dir.'design/'.$theme.'/html/'.$template; 
if(is_file($file) && is_writable($file) && !is_file($simpla->config->root_dir.'design/'.$theme.'/locked')) 
    file_put_contents($file, $content); 

$result= true; 
header("Content-type: application/json; charset=UTF-8"); 
header("Cache-Control: must-revalidate"); 
header("Pragma: no-cache"); 
header("Expires: -1");  
$json = json_encode($result); 
print $json; 

、問題はgetElementByIdを

+0

要素の値を取得するためのjQuery: '$("#tml ")val()' - 既にライブラリを使用している場合、ブラウザAPIを利用する理由はありません。 – Pointy

+0

には、varとセミコロンの両方がありません。 –

答えて

6

に隠しそれはあなたがテキストエリアに入力したときinnerHTMLプロパティが変更されないため、value

var content = document.getElementById("tml").value; 
を行います

またはjQueryで

var content = $("#tml").val();