j.smith1981 Posted October 11, 2011 Share Posted October 11, 2011 I am having problems with the following code: <?php ini_set('display_errors', true); require_once '/usr/share/smarty/Smarty-3.1.2/libs/Smarty.class.php'; define('APPLICATION_DIR', getcwd()); $new_form = new Smarty(); $new_form->compile_check = true; $new_form->force_compile = true; $new_form->debugging = false; $new_form->use_sub_dirs = false; $new_form->cache_dir = APPLICATION_DIR.'/cache'; $new_form->template_dir = APPLICATION_DIR.'/views'; $new_form->compile_dir = APPLICATION_DIR.'/templates_c'; $new_form->config_dir = APPLICATION_DIR.'/config'; // $action_script $new_form->assign('action_script', $_SERVER['PHP_SELF']); $new_form->assign('user_input', ''); // how would I remove this, but when i do it complains of the following: /* ( ! ) Notice: Undefined index: user_input in /www/jeremysmith.me.uk/html/projects-work/php/smarty/form_examples/templates_c/1c78301fa58f94b80e54149e55e6b498be33f294.file.index.tpl.php on line 41 ( ! ) Notice: Trying to get property of non-object in /www/jeremysmith.me.uk/html/projects-work/php/smarty/form_examples/templates_c/1c78301fa58f94b80e54149e55e6b498be33f294.file.index.tpl.php on line 41 */ $new_form->assign('message', 'Please input something below:'); // how would I remove this, but when i do it complains of the following: if(array_key_exists('submit', $_POST)) { $new_form->assign('user_input', trim($_POST['user_input'])); } $new_form->display('index.tpl'); This is a Smarty v3.1.2 coding with the below template: {* Just a sample form! *} <html> <head> <title>Smarty Simple Form</title> </head> <body> <P>{$message} <form method="POST" action="{$action_script}"> <input type="text" id="user_input" name="user_input" value="" maxlength="55" /> <input type="submit" id="submit" name="submit" value="Send" /> </form> {if $user_input gt "0"} <p>{$user_input}</p> {/if} </body> </html> But this keeps bringing up: Notice: Undefined index: user_input in /mydomain.co.uk/html/templates_c/1c78301fa58f94b80e54149e55e6b498be33f294.file.index.tpl.php on line 41 Notice: Trying to get property of non-object in /mydomain.co.uk/html/templates_c/1c78301fa58f94b80e54149e55e6b498be33f294.file.index.tpl.php on line 41 I keep looking over it but can't seem to find a solution to this, any help's much appreciated Quote Link to comment https://forums.phpfreaks.com/topic/248875-php-smarty-undefined-index-and-trying-to-get-property-of-non-object/ Share on other sites More sharing options...
j.smith1981 Posted October 11, 2011 Author Share Posted October 11, 2011 Is there such a function in smarty as a is set? Quote Link to comment https://forums.phpfreaks.com/topic/248875-php-smarty-undefined-index-and-trying-to-get-property-of-non-object/#findComment-1278084 Share on other sites More sharing options...
trq Posted October 11, 2011 Share Posted October 11, 2011 {if $user_input && $user_input gt "0"} Quote Link to comment https://forums.phpfreaks.com/topic/248875-php-smarty-undefined-index-and-trying-to-get-property-of-non-object/#findComment-1278097 Share on other sites More sharing options...
j.smith1981 Posted October 11, 2011 Author Share Posted October 11, 2011 Sorry I will explain allot better, when I omit $user_input say completely from the PHP Controller logic it still brings up a notice. That's what the problem is, I think I accidentally didn't put that in, is there any other way of detecting if a variable actually exists in Smarty HTML? Quote Link to comment https://forums.phpfreaks.com/topic/248875-php-smarty-undefined-index-and-trying-to-get-property-of-non-object/#findComment-1278110 Share on other sites More sharing options...
trq Posted October 11, 2011 Share Posted October 11, 2011 The code I posted should work. Quote Link to comment https://forums.phpfreaks.com/topic/248875-php-smarty-undefined-index-and-trying-to-get-property-of-non-object/#findComment-1278112 Share on other sites More sharing options...
j.smith1981 Posted October 11, 2011 Author Share Posted October 11, 2011 The notice warning keeps appearing though. It's becuase of course if I omit the first $user_input in my PHP logic, there's no variable to display, hence the error but even with that what you gave me there's still the notice error. Quote Link to comment https://forums.phpfreaks.com/topic/248875-php-smarty-undefined-index-and-trying-to-get-property-of-non-object/#findComment-1278142 Share on other sites More sharing options...
trq Posted October 11, 2011 Share Posted October 11, 2011 You could use isset but really you shouldn't need it. {if isset($user_input) && $user_input gt "0"} I would be inclined to forget smarty if it's up to you to do so. It really is a terrible idea. Quote Link to comment https://forums.phpfreaks.com/topic/248875-php-smarty-undefined-index-and-trying-to-get-property-of-non-object/#findComment-1278144 Share on other sites More sharing options...
j.smith1981 Posted October 11, 2011 Author Share Posted October 11, 2011 I had a feeling someone was going to say that. I mean been digging around on the smarty forum trying to find anyway around this but no ones got anything (could be an idea for a new plugin) but really I could just turn the error off maybe, I'd have to have a look at that later. Thanks any how. Quote Link to comment https://forums.phpfreaks.com/topic/248875-php-smarty-undefined-index-and-trying-to-get-property-of-non-object/#findComment-1278149 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.