简体   繁体   中英

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. I am using PHP, JavaScript, and 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. Then when the web page is brought up again all the data from the xml file is loaded into the form. I am currently using PHP to save to xml which is working fine. 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. I wanted a way to just load the xml data in one section of my code, rather than scattered about. 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 . Also, I think you are feeling the need to separate concerns . An MVC pattern can help here.

There isn't really a way around that per se. That is how PHP is going to work in general. I mean you can mitigate it by not using the XML structure directly.

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. 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. Even then you are likely just trading object access with -> for array access with [] "macros" by using method calls.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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