模板代码:
{include file ='header.tpl'}
<form name="oform" action="index.php" method="post" onsubmit="">
<table border="1" align="center" width="70%" cellpadding="3" cellspacing="0">
<caption><h3>{$tableName}<h3></caption>
<tr>
<td width="20%">姓名:</td><td><input type="string" name="ceshi" {if $wandoujia} value='{$wandoujia}'{/if} /></td>
</tr>
<tr>
<td>测试:</td><td><input type="string" name="ceshi2" /></td>
</tr>
<tr><td colspan="2"><input type="submit" value="查询" /></td></tr>
</table>
</form>
{include file ='footer.tpl'}index.php调用代码:
<?php
require_once dirname(__FILE__) . "\include\common.inc.php";
require_once SR_TEMPLATE . "\TemplatesFunction.php";
$smarty = $GLOBALS['smarty'];
$smarty -> assign('title','测试标题');
$smarty -> assign('Version',"2011");
$smarty -> assign('HomePageLink','http://localhost/Matance1.0/');
$smarty -> assign('tableName','标题');
if(isset($_POST['ceshi2'])){
$smarty -> assign('wandoujia',$_POST['ceshi2']);
}
$smarty -> display('index.tpl');
?>即在index.tpl模板上作一表单,action="index.php",index.php在加载模板时判断有木有传值,如果有,则给模板中$wandoujia函数传值但运行后,总是提示:Notice: Undefined index: wandoujia in D:\Program Files\wamp\www\Mantance1.0\TemplateFile\Templates\default\templates_c\da1ad7986fcfc741270ca7b7bdceba8d01b88c26.file.index.tpl.php on line 34
Notice: Trying to get property of non-object in D:\Program Files\wamp\www\Mantance1.0\TemplateFile\Templates\default\templates_c\da1ad7986fcfc741270ca7b7bdceba8d01b88c26.file.index.tpl.php on line 34
/>求教
{include file ='header.tpl'}
<form name="oform" action="index.php" method="post" onsubmit="">
<table border="1" align="center" width="70%" cellpadding="3" cellspacing="0">
<caption><h3>{$tableName}<h3></caption>
<tr>
<td width="20%">姓名:</td><td><input type="string" name="ceshi" {if $wandoujia} value='{$wandoujia}'{/if} /></td>
</tr>
<tr>
<td>测试:</td><td><input type="string" name="ceshi2" /></td>
</tr>
<tr><td colspan="2"><input type="submit" value="查询" /></td></tr>
</table>
</form>
{include file ='footer.tpl'}index.php调用代码:
<?php
require_once dirname(__FILE__) . "\include\common.inc.php";
require_once SR_TEMPLATE . "\TemplatesFunction.php";
$smarty = $GLOBALS['smarty'];
$smarty -> assign('title','测试标题');
$smarty -> assign('Version',"2011");
$smarty -> assign('HomePageLink','http://localhost/Matance1.0/');
$smarty -> assign('tableName','标题');
if(isset($_POST['ceshi2'])){
$smarty -> assign('wandoujia',$_POST['ceshi2']);
}
$smarty -> display('index.tpl');
?>即在index.tpl模板上作一表单,action="index.php",index.php在加载模板时判断有木有传值,如果有,则给模板中$wandoujia函数传值但运行后,总是提示:Notice: Undefined index: wandoujia in D:\Program Files\wamp\www\Mantance1.0\TemplateFile\Templates\default\templates_c\da1ad7986fcfc741270ca7b7bdceba8d01b88c26.file.index.tpl.php on line 34
Notice: Trying to get property of non-object in D:\Program Files\wamp\www\Mantance1.0\TemplateFile\Templates\default\templates_c\da1ad7986fcfc741270ca7b7bdceba8d01b88c26.file.index.tpl.php on line 34
/>求教
第二,看看此文件34行即可
D:\Program Files\wamp\www\Mantance1.0\TemplateFile\Templates\default\templates_c\da1ad7986fcfc741270ca7b7bdceba8d01b88c26.file.index.tpl.php
$smarty -> assign('wandoujia',$_POST['ceshi2']);
}没考虑wandoujia没有分配的情况,如果wandoujia没有值自然或报错。
if(isset($_POST['ceshi2'])){
$smarty -> assign('wandoujia',$_POST['ceshi2']);
}
还应该在模板里用SMARTY也判断是否有POST提交。如果有就加入SMARTY变量wandoujia。注意这一段:
<tr>
<td width="20%">姓名:</td><td><input type="string" name="ceshi" {if $wandoujia} value='{$wandoujia}'{/if} /></td>
</tr>
试着改为:
<tr>
<td width="20%">姓名:</td><td><input type="string" name="ceshi" {if $smarty.post.ceshi2!=''} value='{$wandoujia}'{/if} /></td>
</tr>
这里不一定是这样写,你最好直接看看POST提交值的模板的2种输出情况,{$smarty.post.ceshi2}直接插入到一个标签里显示一下。看看有提交值跟没提交值适合{$smarty.post.ceshi2}的输出差别。这样就能判断是否需要加入$wandoujia变量了。
把
if(isset($_POST['ceshi2'])){
$smarty -> assign('wandoujia',$_POST['ceshi2']);
}
改为
$smarty -> assign('wandoujia' ,isset($_POST['ceshi2']) ? $_POST['ceshi2'] : '');
不就结了
我按照falizixun2的方法将代码更为:
{if $smarty.post.ceshi2!=''} value='{$wandoujia}'{/if}
网站仍旧提示$wandoujia未定义
最后干脆将错误级别更改了。
另外xuzuning说的
把
if(isset($_POST['ceshi2'])){
$smarty -> assign('wandoujia',$_POST['ceshi2']);
}
改为
$smarty -> assign('wandoujia' ,isset($_POST['ceshi2']) ? $_POST['ceshi2'] : '');
方法是好的,简化代码,但是还是不能解决smarty模板中提示未定义变量的问题我也是刚开始学着弄smarty,慢慢折腾吧。谢谢!!
我按照falizixun2的方法将代码更为:
{if $smarty.post.ceshi2!=''} value='{$wandoujia}'{/if}
网站仍旧提示$wandoujia未定义
最后干脆将错误级别更改了。
另外xuzuning说的
把
if(isset($_POST['ceshi2'])){
$smarty -> assign('wandoujia',$_POST['ceshi2']);
}
改为
$smarty -> assign('wandoujia' ,isset($_POST['ceshi2']) ? $_POST['ceshi2'] : '');
方法是好的,简化代码,但是还是不能解决smarty模板中提示未定义变量的问题我也是刚开始学着弄smarty,慢慢折腾吧。谢谢!!