简体   繁体   English

将HTML表单数据加载和保存到xml文件的最佳方法?

[英]Best way to load and save HTML form data to an xml file?

I know you can't do this with just HTML, or JavaScript. 我知道您不能仅使用HTML或JavaScript来执行此操作。 I am using PHP, JavaScript, and HTML. 我正在使用PHP,JavaScript和HTML。 What I want to do is have a form that a user can fill out then when submit is pressed all the form data is saved to an xml file. 我想做的是有一个用户可以填写的表单,然后按提交时,所有表单数据都保存到xml文件中。 Then when the web page is brought up again all the data from the xml file is loaded into the form. 然后,当再次打开网页时,来自xml文件的所有数据都将加载到表单中。 I am currently using PHP to save to xml which is working fine. 我目前正在使用PHP保存到xml,它工作正常。 My min problem is how I am loading the xml file, I am using PHP embedded into my HTML which I feel is becoming very convoluted. 我的最小问题是我如何加载xml文件,我正在将PHP嵌入到HTML中,这让我感到非常困惑。 I wanted a way to just load the xml data in one section of my code, rather than scattered about. 我想要一种只在我的代码的一部分中加载xml数据而不是分散的方法。 I was able to do this with saving the form data, but loading seems to be a bit more challenging. 我可以通过保存表单数据来做到这一点,但是加载似乎更具挑战性。

<td><input type="text" name="person_name" value="<?php echo $config->main->calling->general->person_name;?>"></td>  

There are a bunch of these kind of statements... 有很多这样的陈述...

Ah, you are feeling the need to practice the principle of DRY . 啊,您感觉有必要实践DRY的原理。 Also, I think you are feeling the need to separate concerns . 另外,我认为您感到有必要分开关注 An MVC pattern can help here. MVC模式可以在这里提供帮助。

There isn't really a way around that per se. 本身并没有真正的解决方法。 That is how PHP is going to work in general. 这就是PHP通常将如何工作的方式。 I mean you can mitigate it by not using the XML structure directly. 我的意思是您可以通过不直接使用XML结构来缓解这种情况。

Like pull out the specific data you need and make separate variables an array or an object and then just output from those. 就像提取特定数据一样,将单独的变量设置为数组或对象,然后从其中输出即可。 Personally I would probably use an object and treat the XML like a datasource. 就个人而言,我可能会使用一个对象并将XML视为数据源。 That way you could do something like: 这样,您可以执行以下操作:

<td><input type="text" name="person_name" value="<?php echo $config->getSomethingSpecific();?>"></td> 

Of course depending on how deeply your structure is nested and what not this might not buy you much unless you only use a subset of the XML data. 当然,取决于您的结构嵌套的深度,如果您仅使用XML数据的子集,这可能不会为您带来多少好处。 Even then you are likely just trading object access with -> for array access with [] "macros" by using method calls. 即使那样,您也可能只是通过使用方法调用将对象访问权限与->交换为使用[] “宏”进行数组访问。

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

相关问题 有什么办法可以用 JavaScript 创建一个带有 html 表单数据的 XML 文件吗? - Is there any way to create an XML file with an html form data with JavaScript? 将 HTML 表单保存到 PDF 的最佳方法是什么 - 包括输入的值? - What is the best way to save an HTML form to PDF - including the values entered? 是否可以在PC本地存储的xml文件中保存和检索以HTML格式填充的数据? - Is it possible to save & retrieve the data filled in HTML form to/from xml file stored locally on PC? 使用JavaScript将HTML表单数据保存到文件中 - Save HTML Form data into a File using JavaScript 将表单数据保存到文本文件 - HTML &amp; Javascript - Save form data to text file - HTML & Javascript 将html5画布数据另存为对象的最佳方法 - Best way to save html5 canvas data as object 将HTML表单数据下载为xml文件时出现问题 - Issue downloading HTML form data as a xml file 使用javascript从html表单在Google表格中推送数据的最佳方法 - Best way to push data in a Google Sheet from html form with javascript 如何使用Javascript将表单输入数据保存到XML文件? - How to save form input data to a XML file using Javascript? 异步数据加载后提交HTML表单的正确方法是什么? - What is the proper way to submit HTML form after asynchronous data load?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM