[英]Solution to insert data from textarea to database
I am programming a new website which add topics to database. 我正在编程一个新的网站,它将主题添加到数据库。
I made input for title and textarea for content and I use wysiwyg editor with the textarea to add some effects to my articles. 我为标题和文本区域输入了内容,并使用wysiwyg编辑器和文本区域为文章添加了一些效果。
The problem is that when I try to insert data to db it fails to insert the content of textarea and when I remove the wysiwyg it works perfectly. 问题是,当我尝试将数据插入db时,它无法插入textarea的内容,而当我删除所见即所得时,它可以正常工作。
I am using is PHP and i use jQuery to insert data without refreshing the page. 我使用的是PHP,我使用jQuery插入数据而不刷新页面。
This is the code i am using===> 这是我正在使用的代码===>
ajax.js ajax.js
$('#share').click(function(){
var title=$("#tilearticle").val();
if(title== ''){$('#tiart').show('slow');}
else{
var datastring= $("#form1").serialize();
var url1='action.php?action=article';
$.post(url1,datastring,function(info){$("#res").html(info);});
}
});
action.php action.php
if($_GET['action']=='article'){
$get=$_GET['action'];
$title=$_POST['title']; // get data from title input
$content=$_POST['ckeditor1']; // get data from textarea
$image=$_POST['image'];
$date=date("Y/m/d");
$newtitle=string_limit_words($title, 6);
$urltitle=preg_replace('/[^a-z0-9]/i',' ', $newtitle);
$newurltitle=str_replace(" ","-",$newtitle);
$url='/'.$newurltitle.'.html';
$sql=mysql_query("INSERT INTO article (id,title,img,content,url,time ) VALUES
('','".$title."','".$image."','".$content."','".$url."','".$date."')") or die(mysql_error());
if($sql){
echo 'work';}else{echo 'nooo';}
}
add.php // add article page add.php //添加文章页面
<form class="form-horizontal" action='action.php?action=article' method='post' id='form1' role="form">
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">title</label>
<div class="col-sm-10">
<input type="text" class="form-control" name='title' id="tilearticle" placeholder="title">
<span id='tiart'style='color:red;display: none;'><i class='glyphicon glyphicon-info-sign'> </i> pleas fill in the field</span>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<div class="checkbox">
<textarea class="form-control ckeditor" name='ckeditor1' id='textarea' rows="4"> </textarea>
</div>
</div>
</div>
<div class="form-group">
<label for="exampleInputFile">File input</label>
<input type="file" name="image"id="filearticle">
<p class="help-block">Example block-level help text here.</p>
</div> ';
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="button" name='submit' id='share' class="btn btn-default">Post</button>
</div>
</div>
</form>
建议在插入数据库之前,应始终使用
addslashes($value)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.