开发者

how to access array form position dynamically in html/php form?

I am doing a little learning on php and and html using smarty to aid it. I am trying to build a very simple shop-alike list with a cart image that when it is clicked, it is supposed to return via form the info I want from that item, but I am failing to get the index of the items right....

This is the code relative to the loop inside the tlp (it is a HTML internally used by smarty):

{section name=i loop=$homeList}
     <div class="homemenu" align="right">
        <table class="menhome" border="0">
            <tr>
            <td width="102"><img src="{$homeList[i].img}"/></td>
            <td width="150">{$homeList[i].Nome} </td>
            <td width="350">{$homeList[i].Descricao}</td>
            <td width="80">{$homeList[i].Preco} €</td>
            <td width="80">{$homeList[开发者_如何学Pythoni].Disponiblidade}</td>
            <td><form action="updatecart.php"  method="POST">
                <input type="hidden" name="usr[]" value="abobora">
                <input type="hidden" name="iditem[]" value="{$homeList[i].IDItem}">
                <input type="hidden" name="qta" value="1">
                <input type="hidden" name="index" value="1">
                <input type="image" name="submit" src="../images/carro.png" width="52" height="52"> 
            </td>
            </tr>
        </table>
    </div>
{/section}

and this is the code relative to the "experimental" php update:

require_once("functions.php");
var_dump($_POST);

and this is a sample var_dump output:

array
'usr' => 
    array
      0 => string 'abobora' (length=7)
      1 => string 'abobora' (length=7)
      2 => string 'abobora' (length=7)
      3 => string 'abobora' (length=7)
      4 => string 'abobora' (length=7)
  'iditem' => 
    array
      0 => string 'it10' (length=4)
      1 => string 'it13' (length=4)
      2 => string 'it2' (length=3)
      3 => string 'it20' (length=4)
      4 => string 'it21' (length=4)
  'qta' => string '1' (length=1)
  'index' => string '1' (length=1)
  'submit_x' => string '22' (length=2)
  'submit_y' => string '27' (length=2)

The thing is: how do I tell the form that a single button has a singular index to the array and gain access to the correct info inside the array?

PS: the index="1" here is just an experimental value, what I wanted there is the info relative to the index

PSS: sorry for the bad output format, but there are times that I simply don't get how code tag works....


I´m not sure I understand the question correctly, but are you looking for something like:

<input type="hidden" name="usr[{index_in_array}]" value="abobora">
<input type="hidden" name="iditem[{index_in_array}]" value="{$homeList[i].IDItem}">

Edit: You are not closing the <form> correctly and that´s why you can´t rely on the index values that get sent in:

        <td><form action="updatecart.php"  method="POST">
            <input type="hidden" name="usr[]" value="abobora">
            <input type="hidden" name="iditem[]" value="{$homeList[i].IDItem}">
            <input type="hidden" name="qta" value="1">
            <input type="hidden" name="index" value="1">
            <input type="image" name="submit" src="../images/carro.png" width="52" height="52">
          </form>            // added
        </td>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜