好吧...这对我来说似乎很复杂..
不知道是不是这样。
文件名:add_types.js
我有一些数据,我将其创建到 mysql 数据库中(用于后端系统)。 通过 jquery/json.效果很好。
var last_id = data.last_id;
var fck_editor = data.fck_editor;
var new_data = '<div id="input_highlight'+last_id+'"><strong style="font-size:14px;">Overskrift:</strong> <br />';
new_data += '<input type="text" name="overskrift[]" /><br />';
new_data += '<input type="hidden" name="tekst_id[]" value="'+data.last_id+'" />';
new_data += fck_editor;
new_data += '<a href="javascript:;" onclick="remove_rejse('+last_id+');" title="Slet"><img src="/gfx/admin/icons/delete.png" alt="" />Slet</a>';
new_data += '</div><div id="loader'+last_id+'"></div><br />';
$(div_id).append(new_data);
现在我只需要更新它(在输出的同一文件中)
rejser.php 我的数据放在 div 中
<div id="add_here" class="add_here_bg"></div>
当我在 rejser.php 文件中提交另一个表单时,我希望 add_types.js 的输出在数据库中更新。
如果我的问题可以理解,请告诉我。
最佳答案
您可以使用常规 jQuery AJAX 请求,它将 div 的内容提交到服务器端脚本:
var content = $('#add_here').html();
$.get(url_of_script, content);
然后让脚本将其添加到数据库中。您熟悉使用 PHP/MySQL 来执行此操作吗?
如果您想在提交表单时更新这些内容,请尝试将事件监听器附加到表单的 onSubmit 事件。
编辑:
因此,首先将 onSubmit 属性添加到您的表单中:
<form onSubmit="return formSubmit(event);">
现在,您必须在某处定义此函数 - 在哪里执行实际上并不重要,尽管 <head>
推荐您页面的部分。当然,外部文件也是可能的。
function formSubmit(event) {
var content = $('#add_here').html();
// this callback will make sure the form is submitted after having completed the other request
var callback = function() { event.target.submit() };
$.get(url_of_script, content, callback);
// Now, cancel the default event, the callback will take care of the submit afterwards
event.stopPropagation();
}
还没有测试过,但类似的东西应该可以工作。如果您需要更多帮助,请告诉我。
关于php - 将json数据更新到db中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1304918/