2016-11-29 49 views
0

我该如何防止仅向数据库提交脚本,但是应该允许使用我正在使用的以下脚本允许所有HTML标记。请帮忙。如何防止从textarea输入提交脚本

<form name="exthtmlForm" style="height: 100%;"> 
    <fieldset> 
    <legend>Source Editor</legend> 
    <div id="editor" name="editor" style="height: 100%;"> 
     <textarea id="iExthtml" style="max-width: 100%; width: 100%; height: 50px; box-sizing: border-box;"> 
     <?php if($exthtml_content!=""){ echo htmlentities($exthtml_content, ENT_QUOTES, 'UTF-8'); } ?> 
     </textarea> 
    </div> 
    </fieldset> 
</form> 

$(document).on('click','#abtSubmit',function(){ 
    var data = $('#iExthtml').val().replace(/'/g, "\\'"); 
    dataString=$('form[name=exthtmlForm]').serialize(); 
    $.ajax({ 
    type: 'POST', 
    url: "<?php echo $GLOBALS['base_url'];?>ajax/cpanel/cpanel-ajax.php?mode=UpdateExthtml", 
    cache: false, 
    data: { content : data , dpid : <?php echo $dpid; ?> , menuID : <?php echo $MPage; ?> }, 
    dataType: "json", 
    success: function(data){ 
     if(data.success == "yes"){ 
     if($("#states").length!==1){ 
      $(".error_2525").remove(); 
      $('#abtSubmit').before("<div class='error_2525' id='success_message' style='margin-top: 10px;'>Content updated successfully</div>"); 
      $('#success_message').delay(5000).fadeOut(300, function(){ 
      $('#success_message').remove(); 
      }); 
     } 
     } 
    } 
    }); 
}); 
+1

您好,欢迎光临。你的代码需要在脚本标签剥离时进行编辑。 我强烈建议不要用'JavaScript'(客户端)来做这件事,而用'PHP'来做服务器端。 – Mouser

+1

此外,我不能使这个代码的头或故事。 – Mouser

+0

任何客户端验证纯粹是为了方便用户,并且绝不应该用它来消毒用户输入。您需要删除服务器上的恶意输入。 – WillardSolutions

回答

0

有两个步骤来完成此操作。首先,你需要拉的默认值文本区域,并将其存储在变量

jQuery(document.ready(function(){ 
    window.form_exthtmlForm_default = '<?php echo (($exthtml_content!="") ? htmlentities($exthtml_content, ENT_QUOTES, 'UTF-8') : ""); ?>'; 
})); 

一个javascript然后,你需要使用上提交以确保形式不流通的默认值这个变量(window.form_exthtmlForm_default)。

修订提交功能:

$(document).on('click','#abtSubmit',function(){ 
    var data = $('#iExthtml').val().replace(/'/g, "\\'"); 

    //Here we will return false if the form has the default textarea value.  
    if ($('#iExthtml').val() == window.form_exthtmlForm_default) { 
     //You would want to also provide some sort of frontend 
     //user message to alert the user to populate the text 
     return false; 
    } 

    dataString=$('form[name=exthtmlForm]').serialize(); 
    $.ajax({ 
    type: 'POST', 
    url: "<?php echo $GLOBALS['base_url'];?>ajax/cpanel/cpanel-ajax.php?mode=UpdateExthtml", 
    cache: false, 
    data: { content : data , dpid : <?php echo $dpid; ?> , menuID : <?php echo $MPage; ?> }, 
    dataType: "json", 
    success: function(data){ 
     if(data.success == "yes"){ 
     if($("#states").length!==1){ 
      $(".error_2525").remove(); 
      $('#abtSubmit').before("<div class='error_2525' id='success_message' style='margin-top: 10px;'>Content updated successfully</div>"); 
      $('#success_message').delay(5000).fadeOut(300, function(){ 
      $('#success_message').remove(); 
      }); 
     } 
     } 
    } 
    }); 
});