简体   繁体   English

尝试将内容可编辑div的内容保存到MySQL数据库

[英]Trying to save the contents of a contenteditable div to MySQL database

Alright, so I want to be able to submit the contents of a contenteditable div into a MySQL database. 好吧,所以我希望能够将contenteditable div的内容提交到MySQL数据库中。 I've got a (contenteditable='true') div populated with HTML code from a MySQL database. 我有一个(contenteditable ='true')div,其中填充了来自MySQL数据库的HTML代码。 It's something like this: 就像这样:

$page = $_GET['page'];

$getpage = mysql_query("SELECT * FROM pages WHERE title = '$page'") or die(mysql_query());
$row = mysql_fetch_assoc($getpage);
$pagecode = $row['pagecode'];

<div class="editable" contenteditable="true">
<?php echo $pagecode; ?>
</div>

And I want to control this with a regular link: 我想通过常规链接来控制它:

<a href="#" onclick="somefunction()">Save</a>

Maybe this could work with a hidden form that's controlled with Javascript, but I really have no clue. 也许这可以用Javascript控制的隐藏形式工作,但我真的不知道。

The goal is to have something like an HTML editor, which displays the HTML code from the database inside the editable div and allows the user to edit it and save it back in the database. 我们的目标是拥有类似HTML编辑器的功能,它可以在可编辑div中显示数据库中的HTML代码,并允许用户对其进行编辑并将其保存回数据库中。 Any help is greatly appreciated. 任何帮助是极大的赞赏。

Thanks, 谢谢,

Gill. 鳃。

In your AJAX code, make a new AJAX request to a processor. 在您的AJAX代码中,向处理器提出一个新的AJAX请求。 Assign an id='id_name_here to your div and pass the id_name_here.innerHTML to the url. 为您的div分配一个id='id_name_here ,然后将id_name_here.innerHTML传递给该网址。 Then you can do something with it from your processor. 然后,您可以从处理器中对其进行处理。

// JS
function somefunction() {
    var updated_div_info = document.getElementById('id_name_here').innerHTML;
    // POST TO AJAX ...
}


// PHP
<?php

$updated_div_info = $_POST['updated_div_info'];

// INSERT THIS INTO THE DB

try this 尝试这个

function somefunction(){
    var html = encodeURIComponent($(".editable").html());
    $.ajax({
       url: "save.php",
       data: "content="+html
    })
}

保存内容<div contenteditable="“true”">通过 Rails 中的表单到我的数据库</div><div id="text_translate"><p>我正在尝试通过表单将&lt;div contenteditable="true"&gt;的内容保存到我的 Rails 应用程序中的数据库中,以便用户可以保存和更新它。</p><p> 这是设置:</p><p> 用户获得一个视图(即 /posts/1),其中包含一个&lt;div&gt;和contenteditable= "true" 。 我添加了 javacript 给我一个“保存”按钮和一个隐藏字段,它将捕获 div 的内容以进行保存,如<a href="https://stackoverflow.com/questions/7355583/can-div-with-contenteditable-true-be-passed-through-form" rel="nofollow">Can div with contenteditable=true be pass through form 中所建议的那样?</a> . 然后将其更改为遵循同一问题中的建议。</p><pre> &lt;%= form_for @post do |form| %&gt; &lt;%= form.hidden_field:content %&gt; &lt;%= form.submit "Save" %&gt; &lt;% end %&gt; &lt;div id="content" contenteditable="true"&gt; &lt;p&gt;Hello&lt;/p&gt; &lt;/div&gt; &lt;script&gt; document.getElementById('content').addEventListener('input', function(ev){ document.getElementById('post_content').value = ev.target.innerHTML; }) &lt;/script&gt;</pre><p> 我正在寻找的行为是这种形式,一旦我按下“保存”,将&lt;div id="content"&gt;的所有内容保存到@post.content</p><p> 我在post_params中的posts_controller.rb中添加了:content</p><pre> def post_params params.require(:post).permit(:user_id, :content) end</pre><p> 但它不起作用。 @post.content保持nil 。</p><p> 服务器日志显示 param:content 正在发送,但为空!</p><pre> Started PATCH "/posts/3" for::1 at 2019-10-01 19:36:24 +0200 Processing by PostsController#update as HTML Parameters: {"authenticity_token"=&gt;"SRzcfcrBWOCdaWeJkp6rCRw94Ex2XLuVvnc3Bt1dFP1JFz36NCNTcxcuOVqzymlIGq9sveaE/+OkDnaHv3kJNQ==", "post"=&gt;{"content"=&gt;""}, "commit"=&gt;"Save", "id"=&gt;"3"}</pre><p> 任何猜测我做错了什么? 提前致谢</p></div> - Save content of a <div contenteditable=“true”> to my db through a form in Rails

暂无
暂无

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

相关问题 在contenteditable div中的空内容 - Empty contents in contenteditable div JS / jQuery:修改div contenteditable的内容并重新调整焦点 - JS/jQuery : Amend contents of div contenteditable and refocus 在ContentEditable Div中使用Rangy保存/还原选择 - Save/Restore Selection with Rangy in ContentEditable Div 在 state 中保存 Div 的内容 - Save contents of Div in the state 限制contenteditable div中要存储在数据库中的字符数 - Limiting the number of characters in a contenteditable div to store in a database 如何将div的内容保存为图像? - How to save the contents of a div as a image? 如何将DIV ID的值保存到数据库PHP和MySQL? - How do I save the value of a DIV ID to database PHP & MySQL? 保存内容<div contenteditable="“true”">通过 Rails 中的表单到我的数据库</div><div id="text_translate"><p>我正在尝试通过表单将&lt;div contenteditable="true"&gt;的内容保存到我的 Rails 应用程序中的数据库中,以便用户可以保存和更新它。</p><p> 这是设置:</p><p> 用户获得一个视图(即 /posts/1),其中包含一个&lt;div&gt;和contenteditable= "true" 。 我添加了 javacript 给我一个“保存”按钮和一个隐藏字段,它将捕获 div 的内容以进行保存,如<a href="https://stackoverflow.com/questions/7355583/can-div-with-contenteditable-true-be-passed-through-form" rel="nofollow">Can div with contenteditable=true be pass through form 中所建议的那样?</a> . 然后将其更改为遵循同一问题中的建议。</p><pre> &lt;%= form_for @post do |form| %&gt; &lt;%= form.hidden_field:content %&gt; &lt;%= form.submit "Save" %&gt; &lt;% end %&gt; &lt;div id="content" contenteditable="true"&gt; &lt;p&gt;Hello&lt;/p&gt; &lt;/div&gt; &lt;script&gt; document.getElementById('content').addEventListener('input', function(ev){ document.getElementById('post_content').value = ev.target.innerHTML; }) &lt;/script&gt;</pre><p> 我正在寻找的行为是这种形式,一旦我按下“保存”,将&lt;div id="content"&gt;的所有内容保存到@post.content</p><p> 我在post_params中的posts_controller.rb中添加了:content</p><pre> def post_params params.require(:post).permit(:user_id, :content) end</pre><p> 但它不起作用。 @post.content保持nil 。</p><p> 服务器日志显示 param:content 正在发送,但为空!</p><pre> Started PATCH "/posts/3" for::1 at 2019-10-01 19:36:24 +0200 Processing by PostsController#update as HTML Parameters: {"authenticity_token"=&gt;"SRzcfcrBWOCdaWeJkp6rCRw94Ex2XLuVvnc3Bt1dFP1JFz36NCNTcxcuOVqzymlIGq9sveaE/+OkDnaHv3kJNQ==", "post"=&gt;{"content"=&gt;""}, "commit"=&gt;"Save", "id"=&gt;"3"}</pre><p> 任何猜测我做错了什么? 提前致谢</p></div> - Save content of a <div contenteditable=“true”> to my db through a form in Rails 保存本地文本文件,以检测contenteditable div的新行和空白 - Save local text file detecting new lines and whitespaces of contenteditable div 如何使用$ _POST来保存div是contentEditable =“true”? - How to use $_POST to save a Div which is contentEditable=“true”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM