Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

$_POST array from form to display value when form has errors

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
raghu810
Smarty Rookie


Joined: 24 Apr 2015
Posts: 15

PostPosted: Mon Sep 14, 2015 3:45 pm    Post subject: $_POST array from form to display value when form has errors Reply with quote

I finally resided here after trying with so many things and googling and my problem still exists. I have a big form like this. Please ignore for not mentioning all the values here. Its just an example:

I have 3 array fields and others are just a singular field.

template file

Code:
<form method="post" action"">
<input type="text" name="id[]" value="{$id}"/>
<input type="text" name="name[]" value="{$name}"/>
<input type="text" name="lastname[]" value="{$lastname}"/>

<input type="text" name="normaltext" value="{$normaltext}" />
<input type="text" name="normaltext1" value="{$normaltext1}" />
</form>


in my php file

after validation and if validation fails, I have assigned like this,

Code:
$smarty->assign($_POST);


this only works for

Quote:
$normaltext, $normaltext1


and not for array fields such as

Quote:
id, name and lastname


please help
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Mon Sep 14, 2015 6:49 pm    Post subject: Reply with quote

http://www.smarty.net/docs/en/api.assign.tpl
Back to top
View user's profile Send private message
raghu810
Smarty Rookie


Joined: 24 Apr 2015
Posts: 15

PostPosted: Mon Sep 14, 2015 7:25 pm    Post subject: Reply with quote

I do know about the assign function, but how can i use like it simple field as I posted earlier as
Code:
$smarty->assign($_POST);
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Mon Sep 14, 2015 8:20 pm    Post subject: Reply with quote

See http://www.php.net/manual/en/faq.html.php#faq.html.arrays

Why did you use arrays for 'id', 'name' and 'lastname'?
It does only make sense if you use several <input> tags for the same field name.

Example
Code:
<input type="text" name="foo[]">
<input type="text" name="foo[]">
<input type="text" name="foo[]">


Note
Code:
$smarty->assign($_POST);

does assign array fields as array

Example
Code:
<input type="text" name="foo[]" value="{$foo[0]}">
<input type="text" name="foo[]" value="{$foo[1]}">
<input type="text" name="foo[]" value="{$foo[2]}">


Or in your case
Code:
<form method="post" action"">
<input type="text" name="id[]" value="{$id[0]}"/>
<input type="text" name="name[]" value="{$name[0]}"/>
<input type="text" name="lastname[]" value="{$lastname[0]}"/>

<input type="text" name="normaltext" value="{$normaltext}" />
<input type="text" name="normaltext1" value="{$normaltext1}" />
</form>
Back to top
View user's profile Send private message
raghu810
Smarty Rookie


Joined: 24 Apr 2015
Posts: 15

PostPosted: Mon Sep 14, 2015 9:02 pm    Post subject: Reply with quote

Sorry, it is array, as I have it like this only
Quote:

<input type="text" name="id[]">
<input type="text" name="id[]">
<input type="text" name="id[]">

<input type="text" name="name[]">
<input type="text" name="name[]">
<input type="text" name="name[]">

<input type="text" name="lastname[]">
<input type="text" name="lastname[]">
<input type="text" name="lastname[]">


I had already tried before posting out here with the following code

Quote:
<input type="text" name="id[]" value="{$id[0]}"/>
<input type="text" name="id[]" value="{$id[1]}"/>
<input type="text" name="id[]" value="{$id[2]}"/>

<input type="text" name="name[]" value="{$name[0]}"/>
<input type="text" name="name[]" value="{$name[1]}"/>
<input type="text" name="name[]" value="{$name[2}"/>

<input type="text" name="lastname[]" value="{$lastname[0]}"/>
<input type="text" name="lastname[]" value="{$lastname[1]}"/>
<input type="text" name="lastname[]" value="{$lastname[2]}"/>



But no luck with it, hence I requested support over here.
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Mon Sep 14, 2015 10:04 pm    Post subject: Reply with quote

Set
Code:
$smarty->debugging = true;


To see in the Smarty Debugging console which values got assigned.
Back to top
View user's profile Send private message
raghu810
Smarty Rookie


Joined: 24 Apr 2015
Posts: 15

PostPosted: Mon Sep 14, 2015 10:34 pm    Post subject: Reply with quote

Thanks debugging helped me to find that I had wrapped up an if statement and echoing that value within if statement, hence it was not displaying.

Thanks so much.
Back to top
View user's profile Send private message
raghu810
Smarty Rookie


Joined: 24 Apr 2015
Posts: 15

PostPosted: Mon Sep 14, 2015 11:25 pm    Post subject: Reply with quote

Sorry to be back, how can I use foreach loop for all three with in the table row.

Following did not help

Code:
<table>
{if isset($thrownerror) && $thrownerror ne ""}
{foreach from=$id item=nid}
{foreach from=$name item=nname}
{foreach from=$lastname item=nlastname}
<tr>
<td><input type="text" name="id[]" value="{$nid}"/> </td>
<td><input type="text" name="name[]" value="{$nname}"/> </td>
<td><input type="text" name="lastname[]" value="{$nlastname}"/> </td>
</tr>
{/foreach}{/foreach}{/foreach}
{else}
<tr>
<td><input type="text" name="id[]"></td>
<td><input type="text" name="name[]"></td>
<td><input type="text" name="lastname[]"></td>
</tr>
<tr>
<td><input type="text" name="id[]"></td>
<td><input type="text" name="name[]"></td>
<td><input type="text" name="lastname[]"></td>
</tr>
<tr>
<td><input type="text" name="id[]"></td>
<td><input type="text" name="name[]"></td>
<td><input type="text" name="lastname[]"></td>

</tr>
{/if}
</table>
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Mon Sep 14, 2015 11:45 pm    Post subject: Reply with quote

You have nested the {foreach} loops which is not correct.

Code:
{if isset($thrownerror) && $thrownerror ne ""}
{foreach $id as $key =>  $nid}
<tr>
<td><input type="text" name="id[]" value="{$nid}"/> </td>
<td><input type="text" name="name[]" value="{$name[$key]}"/> </td>
<td><input type="text" name="lastname[]" value="{$lastname[$key]}"/> </td>
</tr>
{/foreach}
{else}
Back to top
View user's profile Send private message
raghu810
Smarty Rookie


Joined: 24 Apr 2015
Posts: 15

PostPosted: Mon Sep 14, 2015 11:54 pm    Post subject: Reply with quote

Awesome, worked a treat. Thanks again so much.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP