简体   繁体   English

将数据从textarea插入数据库的解决方案

[英]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)

从可编辑文件中插入文本格式<div>或 textarea 进入数据库</div><div id="text_translate"><p>我目前正在为我自己的网站开发一个个人项目,我正在尝试添加将格式化文本存储到数据库中的功能。 到目前为止,我所做的是能够将字体从斜体更改为粗体作为示例,但我完全不知道如何将其传递到数据库。</p><pre> &lt;style&gt; #fake_textarea { width: 100%; height: 200px; border: 1px solid red; } #jBold { font-weigth: bold; } #jItalic{ font-style:italic; } &lt;/style&gt; &lt;script src="/scripts/snippet-javascript-console.min.js?v=1"&gt;&lt;/script&gt; &lt;body&gt; &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt; &lt;button id="jBold"&gt;&lt;b&gt;B&lt;/b&gt;&lt;/button&gt;&lt;button id="jItalic"&gt;&lt;i&gt;I&lt;/i&gt;&lt;/button&gt; &lt;div id='fake_textarea' contenteditable&gt; Select some text and click the button to make it bold... &lt;br&gt;Or write your own text &lt;/div&gt; &lt;script type="text/javascript"&gt; $(document).ready(function() { $('#jBold').click(function() { document.execCommand('bold'); }); }); &lt;/script&gt; &lt;script type="text/javascript"&gt; $(document).ready(function() { $('#jItalic').click(function() { document.execCommand('italic'); }); }); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt;</pre><p> 示例工作: <a href="https://codepen.io/goldenowl/pen/KKdZQxY" rel="nofollow noreferrer">codepen</a></p></div> - Insert text format from an editable <div> or textarea into the database

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从变量中插入文本区域排序数据? - How to insert in textarea sorted data from variable? ckeditor-将textarea插入数据库 - ckeditor - insert textarea into the database 在textarea或div中从数据库中插入/显示可点击链接 - Insert/show clickable links in/from database from textarea or div 将数据从textarea保存到HTML中的数据库 - Saving data from textarea to database in html 从带有换行符的文本区域到数据库的数据 - Data from textarea with line breaks to database 从可编辑文件中插入文本格式<div>或 textarea 进入数据库</div><div id="text_translate"><p>我目前正在为我自己的网站开发一个个人项目,我正在尝试添加将格式化文本存储到数据库中的功能。 到目前为止,我所做的是能够将字体从斜体更改为粗体作为示例,但我完全不知道如何将其传递到数据库。</p><pre> &lt;style&gt; #fake_textarea { width: 100%; height: 200px; border: 1px solid red; } #jBold { font-weigth: bold; } #jItalic{ font-style:italic; } &lt;/style&gt; &lt;script src="/scripts/snippet-javascript-console.min.js?v=1"&gt;&lt;/script&gt; &lt;body&gt; &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt; &lt;button id="jBold"&gt;&lt;b&gt;B&lt;/b&gt;&lt;/button&gt;&lt;button id="jItalic"&gt;&lt;i&gt;I&lt;/i&gt;&lt;/button&gt; &lt;div id='fake_textarea' contenteditable&gt; Select some text and click the button to make it bold... &lt;br&gt;Or write your own text &lt;/div&gt; &lt;script type="text/javascript"&gt; $(document).ready(function() { $('#jBold').click(function() { document.execCommand('bold'); }); }); &lt;/script&gt; &lt;script type="text/javascript"&gt; $(document).ready(function() { $('#jItalic').click(function() { document.execCommand('italic'); }); }); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt;</pre><p> 示例工作: <a href="https://codepen.io/goldenowl/pen/KKdZQxY" rel="nofollow noreferrer">codepen</a></p></div> - Insert text format from an editable <div> or textarea into the database 从 TextArea 中删除文本并使用 javascript 将新数据插入其中 - Remove text from TextArea and insert new data into it using javascript 将数据从模式插入数据库 - Insert data from modal to database 将数据插入输入字段而不是文本区域 - Insert data into inputfield instead of textarea 将文本从弹出窗口插入文本区域 - Insert text from popup into textarea
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM