-1

How can i check if a post is set? isset($_POST['formname'])

I can't seem to find out how to do it in smarty?

{if isset($_POST['login'])}
Login is set!
{/if}
user3626795
  • 21
  • 1
  • 4
  • [Follow the rabbit](http://stackoverflow.com/a/5952906/3444240) – potashin May 17 '14 at 23:03
  • Did you `$smarty->assign()` values from `$_POST` to make them available to smarty, and [did you see the Smarty docs on how to access POST vars](http://www.smarty.net/docsv2/en/language.variables.smarty.tpl) (which don't actually need to be assigned) – Michael Berkowski May 17 '14 at 23:07
  • By the way, please [don't re-post questions](http://stackoverflow.com/questions/23712426/smarty-if-isset-post). If they aren't getting enough attention, edit them to improve the amount of information in them and that will send them to the front page due to the editing activity. – Michael Berkowski May 17 '14 at 23:08
  • @Michael Berkowski I have no idea about smarty so i havent – user3626795 May 17 '14 at 23:16

1 Answers1

4

You don't have to assign any data to Smarty in this case because request variables are simple visible in Smarty. So you can simple do that in your tpl file:

{if isset($smarty.post.username)}
  Login is set!
{/if}

From comment I understand you want to do something else. Look at this code in template file:

{if isset($smarty.post.username)}
  Username is set!

{if $smarty.post.username eq ''}
  Empty username
{/if}  
{/if}



<form name="form" method="post"> 
<input type="text" class="form-control" style="width:500px;" name="username" placeholder="Username or Email"><br>
 <input type="password" class="form-control" style="width:500px;" name="password" placeholder="Password"><br> 
<input type="submit" class="btn btn-info" style="width:500px;" name="login" value="Sign In"> </form>

When you first open site (and don't send form) no message is displayed because form has not been set and there is no $_POST data sent. But when you send form (without filling in anything) you will have message "Login is set" because form has been set and $_POST data are set although value of $_POST['login'] is empty string.

So in inner if you check if it's empty or not.

You can as alternative also use

{if isset($smarty.post.username) and ($smarty.post.username eq '')}
  Username is set but it's empty!
{/if}
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291