Home >Backend Development >PHP Tutorial >在PHP中如何插入HTML代码块

在PHP中如何插入HTML代码块

WBOY
WBOYOriginal
2016-06-23 13:58:232002browse

我用PHP从mysql中查询记录然后想把不同字段显示在表单中的不同的text中,并且可以更新数据。
想插入一个比较复杂的表单,用echo太容易出错,各位大神,有没有其他办法可以实现啊,跪求!


回复讨论(解决方案)

封装好一个一个小函数, 面对接口编程就简单了.

表单代码静态写在php文件中,PHP代码将查询到的数据输出为一个JS对象,再DOM操作对表单元素进行赋值,举个例子:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>无标题文档</title><script type="text/javascript"><?php$str = 'var data = {demo: 111, test: "test"};';echo $str;?>var data = {demo: 111, test: "test"};window.onload = function() {	document.getElementsByName('demo')[0].value = data.demo;	document.getElementsByName('test')[0].value = data.test;}</script></head><body><form>	<input type="text" name="demo" /><br />    <input type="text" name="test" /></form></body></html>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>无标题文档</title><script type="text/javascript"><?php$str = 'var data = {demo: 111, test: "test"};';echo $str;?>window.onload = function() {	document.getElementsByName('demo')[0].value = data.demo;	document.getElementsByName('test')[0].value = data.test;}</script></head><body><form>	<input type="text" name="demo" /><br />    <input type="text" name="test" /></form></body></html>

晕。。我又把简单问题复杂化了。。输出大段html代码可以这样做:

$ar = array('demo' => '111', 'test' => '222');$html = <<<HTML	<form>		<input type="text" value="{$ar['demo']}" /><br />		<input type="text" value="{$ar['test']}" /><br />	</form>HTML;echo $html;

很好 ,谢谢了哥们 我也正需要这个问题的答案呢嘿嘿

可以考虑模板,如smarty

晕。。我又把简单问题复杂化了。。输出大段html代码可以这样做:
PHP code
$ar = array('demo' => '111', 'test' => '222');
$html =      


        

        
谢谢大神!

晕。。我又把简单问题复杂化了。。输出大段html代码可以这样做:
PHP code
$ar = array('demo' => '111', 'test' => '222');
$html =      


        

        
谢谢大神!

很有用的帖子,有用有用

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn