dongren4099 2012-04-13 15:43
浏览 21
已采纳

html数组索引名称为变量

I have a form such as the below:

<form name="basketSelectionForm" action="processBasket.php" method="POST">
            <div id="tabs-1">
                <table cellpadding="10" cellspacing="10" width="inherit">
                    <tr>
                        <td><img alt="itemNameb" src="images/itemName.jpg" width="70px" height="70px"/></td>
                        <td>Qty. <input value="0" id="itemName" name="basket[itemName]" type="text" style="width:40px;"/>&nbsp; &nbsp; &nbsp;</td>

                        <td><img alt="itemName" src="images/itemName.jpg" width="70px" height="70px"/></td>
                        <td>Qty. <input value="0" id="itemName" name="basket[itemName]" type="text" style="width:40px;"/></td>

Now when I go to second page to look at the entries of the array, I do this:

<?php
    $itemsBasket = array( );
    $itemsBasket = $_POST['basket'];
    echo "<h1>The Items Are...</h1><br>";
    //print_r($itemsBasket);


    foreach ($itemsBasket as $value) 
    {
        if($value > 0){
            echo $value . "<br>";
        }
    }

?>

This will print the value at the indexes of the array...but I need to store the name of the index so lets say the item is chocolate and value of 12. I want to extract that index name from array to store it in variable and then assign value to that variable...

Any way I can do that? right now I get only the value while iterating...

Thanks for help and sorry if question isn't clear I will help explain better if so...

UPDATE: this is the unexpected output....

whitethoab: Array woolthoab: 22 shemag: 22 undershirt: 1 serwalthoab: 22 socks: 12

and this is the definition of the element showing as two dimensional array...

<td><img alt="White Thoab" src="images/whitethoub.jpg" width="70px" height="70px"/></td>
                        <td>Qty. <input value="0" id="whitethoab" name="basket[whitethoab]" type="text" style="width:40px;"/>&nbsp; &nbsp; &nbsp;</td>
  • 写回答

2条回答 默认 最新

  • dsa5211314 2012-04-13 15:46
    关注

    Something like:

    <form name="basketSelectionForm" action="processBasket.php" method="POST">
                <div id="tabs-1">
                    <table cellpadding="10" cellspacing="10" width="inherit">
                        <tr>
                            <td><img alt="itemNameb" src="images/itemName.jpg" width="70px" height="70px"/></td>
                            <td>Qty. <input value="12" id="itemName" name="basket[chocolate]" type="text" style="width:40px;"/>&nbsp; &nbsp; &nbsp;</td>
    
                            <td><img alt="itemName" src="images/itemName.jpg" width="70px" height="70px"/></td>
                            <td>Qty. <input value="9" id="itemName" name="basket[onions]" type="text" style="width:40px;"/></td>
    

    ...and...

    <?php
    
        echo "<h1>The Items Are...</h1><br>";
    
        foreach ($_POST['basket'] as $name => $value) 
        {
            if($value > 0){
                echo $name . ": " . $value . "<br>";
            }
        }
    
    /* Output:
     chocolate: 12
     onions: 9
    */
    
    ?>
    

    ?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?