2013-08-16 55 views
0

我使用的是一个个人项目的CONTENTEDITABLE功能更新SQL数据库,但是当我更新的内容将其添加HTML标签到数据库即PHP - 删除HTML标签

<div id="lipsum" style="font-size: 11px; font-family: Arial, Helvetica, sans; 
text-align:  justify; font-style: normal; font-variant: normal; line-height: normal;"> 
<p style="font-size: 11px; line-height: 14px; margin-bottom: 14px;"> 
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin tincidunt tincidunt tellus, 
ac tincidunt magna imperdiet volutpat. Pellentesque pharetra lorem vitae velit gravida, 
eget gravida tellus volutpat. Praesent viverra nulla at arcu fringilla, quis semper ligula 

我有什么解决方案在剥离这些标签方面呢?我可以使用jquery或php吗?任何人都可以向我展示一些实例吗?

这是我使用来更新我的数据库

save.php

<?php 
include("db.php"); 
$content = $_POST['content']; 
$firstname = $_POST['firstname'];//get posted data 
$content = mysql_real_escape_string($content); 
    $firstname = mysql_real_escape_string($firstname);//escape string 

$sql = "UPDATE datadump SET firstname = '$firstname', content = '$content' WHERE id = '1'"; 
if (mysql_query($sql)) 
{ 
    echo 1; 
} 
?> 

JS/js.js

$(document).ready(function() { 

    $("#save").click(function (e) {   
     var content = $('#content').html(); 
     var firstname = $('#firstname').html();  
     $.ajax({ 
      url: 'save.php', 
      type: 'POST', 
      data: {content: content, firstname: firstname},    
      success:function (data) { 

       if (data == '1') 
       { 
        $("#status") 
        .addClass("success") 
        .html("Data saved successfully") 
        .fadeIn('fast') 
        .delay(3000) 
        .fadeOut('slow'); 
       } 

       if (data == '1') 
       { 
        $("#status") 
        .addClass("success") 
        .html("Data saved successfully") 
        .fadeIn('fast') 
        .delay(3000) 
        .fadeOut('slow'); 
       } 
       else 
       { 
        $("#status") 
        .addClass("error") 
        .html("An error occured, the data could not be saved") 
        .fadeIn('fast') 
        .delay(3000) 
        .fadeOut('slow'); 
       } 
      } 
     }); 

    }); 

    $("#maincontent").click(function (e) { 
     $("#save").show(); 
     e.stopPropagation(); 
    }); 

    $(document).click(function() { 
     $("#save").hide(); 
    }); 

}); 
+2

你正在寻找[用strip_tags](http://php.net/manual/en/function.strip-tags.php) – Rob

+0

@rob做我想用strip_tags而不是mysql_real_escape_string – 001221

+0

你可能想都。你应该真的停止使用mysql_ *函数,并开始使用pdo或mysqli。 – Rob

回答

2

使用strip_tags()函数的代码。

改变这一点;

$content = mysql_real_escape_string($content); 

对此;

$content = mysql_real_escape_string(strip_tags($content));