如何在html前运行php,how to load php before html is completely loaded.
2016-11-27 10:08
375 查看
今天遇到一个很奇怪现象,如果将一份php文件分三部分:
1. 选择数据库内容(php)
2. 将1中所选内容,展示在html textarea上(html)
3. html from设置POST 接住textarea上再次编辑后的内容,并更新到数据库中(php)
理想效果:每次点击submit,textarea都能看到数据库里更新后的内容。
实际效果:每次点击submit,第三部分echo post值,接住的是最后一次更新后的内容;第一部分echo数据库内容(放在textarea中),接住的是上一次更新后的内容。
结论:尽管第一部分中的php写在了html前面,html页面的加载总是优先于php,php对已经展示的html内容没有控制权。所以所展示数据库内容的更新总是滞后的。
解决方向:以ajax调用php,强行让php的加载优先于html。
附上参考:
后记:
如果想在ajax同时call两个php方法
1. 选择数据库内容(php)
2. 将1中所选内容,展示在html textarea上(html)
3. html from设置POST 接住textarea上再次编辑后的内容,并更新到数据库中(php)
理想效果:每次点击submit,textarea都能看到数据库里更新后的内容。
实际效果:每次点击submit,第三部分echo post值,接住的是最后一次更新后的内容;第一部分echo数据库内容(放在textarea中),接住的是上一次更新后的内容。
结论:尽管第一部分中的php写在了html前面,html页面的加载总是优先于php,php对已经展示的html内容没有控制权。所以所展示数据库内容的更新总是滞后的。
解决方向:以ajax调用php,强行让php的加载优先于html。
/* editnotes.php */ <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script> $(document).ready(function(){ $.ajax({ url: 'shownotes.php', success: function(response){ $('#notes_data').text(response); //shownotes.php中的echo值以text形式在html中id=notes_data区域显示 } }) }); </script> <html> <form action="notes.php" method="POST" enctype="multipart/form-data"> notes: <br><textarea rows="4" cols="50" name="notes" id="notes_data"></textarea> <input type="submit" name="submit" value="save"> </form> </html> <?php //在这里接住$_POST['notes'],并更新到数据库中 ?> /* shownotes.php */ <?php //这里select数据库所需内容,并echo $shownotes; ?>
附上参考:
后记:
如果想在ajax同时call两个php方法
<script> $(document).ready(function(){ // use ajax to call PHP $.ajax({ url: 'shownotes.php', success: function(response){ $('#notes_data').text(response); } }) $.ajax({ url: 'showtbd.php', success: function(response){ $('#tbd_data').text(response); } }) }); </script>
相关文章推荐
- How to load and execute Thread class when spring configuration loaded (如何在读取spring配置时自启动线程类)
- 如何将换行符、特殊字符从html传到php,how to capture newline from input
- IE8 下 HTML Parsing Error: Unable to modify the parent container element before the child element is closed (KB927917)
- IE 8 HTML Parsing Error: Unable to modify the parent container element before the child element is closed (KB927917)
- IE8 中"HTML Parsing Error:Unable to modify the parent container element before the child element is closed"错误
- IE8 中"HTML Parsing Error:Unable to modify the parent container element before the child element is c
- JavaScript: how to load dynamic contents (HTML String, JSON) to iframe
- How to know is the file exist in C++ - 在C++中如何判断文件是否存在
- How to load the specified mscordacwks.dll for managed debugging when multiple .NET runtime are loaded in one process
- 如何调整Dreamhost主机PHP上传尺寸的限制/How to change the maximal size of uploading in your Dreamhost
- A HOWTO on Optimizing PHP(如何优化PHP的一篇文章)
- XP下的IE8 错误:HTML Parsing Error: Unable to modify the parent container element before the child element is closed
- How to put a space character before option text in a HTML select element?
- 【报错】IE 8 HTML Parsing Error:Unable to modify the parent container element before the child element is closed
- how to know wether an element is exist in a html page.
- Failed to load viewstate.The control tree into which viewstate is being loaded must match the control tree that......
- How To Get Application Path,And How To Get The Path's Directory,File Name,And Type Info(如何得到运行程序的路径,以及如何得到路径的文件夹,文件名,以及类型等等信息)
- IE 8 HTML Parsing Error: Unable to modify the parent container element before the child element is closed (KB927917)
- IE 8 HTML Parsing Error: Unable to modify the parent container element before the child element is c
- IE 8 HTML Parsing Error:Unable to modify the parent container element before the child element is cl