View previous topic :: View next topic |
Author |
Message |
raghu810 Smarty Rookie
Joined: 24 Apr 2015 Posts: 15
|
Posted: Mon Sep 14, 2015 3:45 pm Post subject: $_POST array from form to display value when form has errors |
|
|
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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
|
Back to top |
|
raghu810 Smarty Rookie
Joined: 24 Apr 2015 Posts: 15
|
Posted: Mon Sep 14, 2015 7:25 pm Post subject: |
|
|
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 |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Mon Sep 14, 2015 8:20 pm Post subject: |
|
|
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 |
|
raghu810 Smarty Rookie
Joined: 24 Apr 2015 Posts: 15
|
Posted: Mon Sep 14, 2015 9:02 pm Post subject: |
|
|
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 |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Mon Sep 14, 2015 10:04 pm Post subject: |
|
|
Set
Code: | $smarty->debugging = true; |
To see in the Smarty Debugging console which values got assigned. |
|
Back to top |
|
raghu810 Smarty Rookie
Joined: 24 Apr 2015 Posts: 15
|
Posted: Mon Sep 14, 2015 10:34 pm Post subject: |
|
|
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 |
|
raghu810 Smarty Rookie
Joined: 24 Apr 2015 Posts: 15
|
Posted: Mon Sep 14, 2015 11:25 pm Post subject: |
|
|
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 |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Mon Sep 14, 2015 11:45 pm Post subject: |
|
|
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 |
|
raghu810 Smarty Rookie
Joined: 24 Apr 2015 Posts: 15
|
Posted: Mon Sep 14, 2015 11:54 pm Post subject: |
|
|
Awesome, worked a treat. Thanks again so much. |
|
Back to top |
|
|