表单代码静态写在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;
?>
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>
<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>
$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;