douduoquan2824 2019-05-12 06:27
浏览 148
已采纳

使用表单在数据库中保存/更新设置,并使用模板中的这些设置构建HTML

My limited experience in web development as a self-taught led me to hit a wall while trying to figure out how to deal with this problem.

I need a form (map_settings.php) where the user should enter some inputs. Those inputs must be saved in a database table (MAPS) and then used to create the final HTML file (e.g. map1.html) for that specific user/inputs.

I know how to deal with using forms and saving submitted data to a database.

What is completely obscure to me is how can I use those inputs to automatically generate the final HTML.

My idea is to have a template HTML (template.html) and each time a user saves new settings via the form, I copy the template and replace some variables inside it with the actual data the user has input in the form.

If this might matter, the variables I need to replace in the template are also JavaScript variables within a <script> tag.

Can anybody help me suggesting one viable way to do this? I am mostly using JavaScript and PHP, without frameworks. I've also red about JavaScript templating engines, but I sincerely did not get if those are useful to me in my case.

Anyway, here is an illustration of what I would need to do, to hopefully clarify better my point.

enter image description here

  • 写回答

2条回答 默认 最新

  • dsqve08622 2019-05-12 12:35
    关注

    Creating a static HTML file per user is not the way to go. Instead just have a PHP script like mapdisplay.php or similar.

    Make the script so that if you type mapdisplay.php?map=1 in the browser then it will read the map ID, get the relevant settings from the database for the map in question and then generate some HTML to display them - of course you can have most of the HTML ready made like a template, and just use PHP to fill in the details from the database. This idea of getting data on the fly when requested, and plugging it into some HTML is how most web applications work.

    If you create a static HTML for each user it quickly becomes unmanageable with a large number of users, plus it's hard to introduce changes or improvements to the template because instead of just updating one script file, you have to back and re-do every existing page. There are other disadvantages to your approach too, but I won't continue here - you get the idea I hope.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部