简体   繁体   English

使用HTML标记和innerHTML,setAttribute()等传递数据。编辑和更新存储在数据库中的新闻文章

[英]Passing data using HTML tags and innerHTML, setAttribute(), etc. Editing and updating news articles stored in a database

I have a simple news update interface where I read news headlines/titles from a database and print them on page as a list. 我有一个简单的新闻更新界面,可以从数据库中读取新闻标题/标题并将其作为列表打印在页面上。 The goal is, that when I click one of the headlines it brings out an area with a form where the news article could be modified and then updated back into database. 目的是,当我单击标题之一时,它会带出一个区域,其中的表单可以修改新闻文章,然后再更新回数据库。

Now in order to do that I need to somehow pass the information (an id, or the headline itself) on to the form, so I can there read the proper article and other info from the database. 现在,为了做到这一点,我需要以某种方式将信息(ID或标题本身)传递给表单,以便在那里可以从数据库中读取正确的文章和其他信息。 It seems like a very simple task, but I've come to notice it seems like an impossible task. 这似乎是一个非常简单的任务,但是我已经注意到这似乎是不可能的任务。 I could of course put the whole form inside a javascript function and then pass on the headline as a parameter using this.innerHTML to the function and then read it from a variable, but then I'd have to generate all the html using javascript, or php, but I think there must be a simpler way to do this. 我当然可以将整个表单放在javascript函数中,然后使用this.innerHTML将该标题作为参数传递给该函数,然后从变量中读取它,但随后必须使用javascript生成所有html,或php,但我认为必须有更简单的方法来做到这一点。

I've tried using a hidden input tag and others like span and div elements and then pass the headline there as the input elements value using onclick="document.getElementByID("elementsname").value=this.innerHTML;" 我已经尝试过使用隐藏的输入标记以及诸如span和div元素之类的其他标记,然后使用onclick =“ document.getElementByID(” elementsname“)。value = this.innerHTML;”将标题作为输入元素值传递到此处。 and onclick="document.getElementsByName("elementname")[0].setAttribute("value/name", this.innerHTML);". 和onclick =“ document.getElementsByName(” elementname“)[0] .setAttribute(” value / name“,this.innerHTML);”。 Something like this would be so simple, but for some reason unknown to me these methods don't work. 这样的事情很简单,但是由于某种我不知道的原因,这些方法不起作用。 Either the info doesn't get there, or it's not further readable from the elements by javascript. 信息无法到达那里,或者无法通过javascript从元素中进一步读取。

For example I can pass on the info using input element when setting it to type="text" and it shows up on the page as a text field, but it's not readable from there for some reason and it doesn't even appear on the code when checking the source code from the browser. 例如,当将输入元素设置为type =“ text”时,我可以使用输入元素来传递信息,它以文本字段的形式显示在页面上,但是由于某种原因它无法从那里读取,甚至没有出现在从浏览器检查源代码时的代码。 Why is this? 为什么是这样? For that reason I can't get the info passed to my SQL query, so I could read the corresponding news article in to a textarea from database. 因此,我无法将信息传递给SQL查询,因此可以从数据库中的textarea中读取相应的新闻文章。

So, is there a way to somehow make that idea to work, or should I forget it and use other methods? 因此,是否有办法以某种方式使该想法生效,还是我应该忘记它并使用其他方法? How is this usually done anyway by web developers when they want to open something for editing/updating from database? 通常,Web开发人员要打开一些内容以从数据库进行编辑/更新时,通常如何做?

This approach is usually going to be accomplished using ajax and a separate template view for the form which is populated dynamically server side. 通常将使用ajax和动态填充在服务器端的表单的单独模板视图来完成此方法。

You will have your page of news articles. 您将拥有新闻文章页面。 When one is clicked, it will make an ajax request to your server with an identifier for the article. 单击一个后,它将向您的服务器发出ajax请求,并带有该文章的标识符。 The server will compose a page containing a WYSIWYG editor pre-filled with the information from the article inside of a set of input elements along with a save or cancel action. 服务器将组成一个页面,其中包含一个WYSIWYG编辑器,该编辑器预先填充了一组输入元素中来自文章的信息以及保存或取消操作。

The response from the server with this template page will come in the success callback in your page of news articles which will then popup a modal type of element containing the server side produced view. 服务器对此模板页面的响应将出现在新闻页面中的成功回调中,然后将弹出包含服务器端生成视图的元素的模式类型。

Upon completing changes if the view is saved an update is called to the relevant news article on the page, as well as posting the changes to the server. 完成更改后,如果保存了视图,则会调用页面上相关新闻文章的更新,并将更改发布到服务器。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM