Jump to content

Archived

This topic is now archived and is closed to further replies.

j.smith1981

PHP Smarty 'Undefined index' and 'Trying to get property of non-object'

Recommended Posts

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

Share this post


Link to post
Share on other sites
{if $user_input && $user_input gt "0"}

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

The code I posted should work.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.