am trying to show assigned variable for failed submission on a smarty form.

array assigned as

      $arr = array($_POST[number]);
      $smarty->assign(number,$arr);

and on tpl form, i added as below.

      {foreach from=$number item=no}
      <input name="number[]" type="text" value="{$no[{counter start=1}]}" />
      {/foreach}

am starting the counter from 1 because this is for dynamically added from jquery, hence am skipping the {$no[0]} from the above input.

But when form gets failed, the counter stays at 1 and does not increase.

How can i increase the number counter to get the exact input array value.

And I also tried,

<input name="number[]" type="text" value="{foreach $smarty.request.no as $no}{$no}{/foreach}" />

from the above tried code, i get all the values of array such as

12345

thanks in advance.

Hi,

I strongly suggests to do your counting in the business logic side and not on the presentation side.

Another thing that I am not very clear on your question is that: Are you trying to count the failed submission/s? If so, then you don't loop over the number of failed submission, you need the SUM of an array, NOT increment by loop. You can use session too -> maybe??

****option 1: ****
Assign the failed submission in session, and pass this value to the template side. If a previous session is set, then add one to the current one re-assigning or refreshing the session and giving it the value of old + New.

option 2:
PHP has a function called array_sum(), and it can also work pretty well for your purpose. For example, using the integers you have provided above, we can derive the sum by simply using array_sum

$arr = array(
            1, 
            2, 
            3, 
            4);

$smarty->assign('number',array_sum($arr));

Also, it is a common practice in smarty-> Whenever there is a form in the presentation side, **no cache ** must be use. What this means is that the form tag should be wrapped with no cache.

Security Implications:
IN CASE the cache function is turned on... it will be a desaster because the last information inputted in the form are cached until a new one is created as the old one expires. The security concern here is that, those cached information can be username, credit card number, password, and the list goes on and on..

To make my rattling short, the form elements and its entirety should be coded something like this

{nocache}   

<form>
<!-- the rest of your form here -->

</form>
{/nocache} 

I am not trying to count the number of failed input values, if form gets failed then the input value should be retrieved whatever the user has typed into the form for submission and if there are any errors, then the value should be retained back to the form.

if i use your 2nd option, how would i replace the input value, such as

from

<input name="number[]" type="text" value="{$no[{counter start=1}]}" />

to

<input name="number[]" type="text" value="{$no[{$number}]}" />

so based on option 2, should i use the above code?

By the way, post no value is alphanumeric value.

Thanks for your time and appreciate your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.