Skip to content
Advertisement

Add HTML table data to $_post

I wanted to add a Dictionary to PHP $_POST, Dictionary will be containing the data of the html table such that the columns heading are the keys and values(of respective key) are the list containing the table data(row wise) of their respective column.

HTML example

<form action="/page.php" method="post">
    <table id="tbl">
        <tr>
            <th>Item Code</th>
            <th>Item</th>
            <th>Rate</th>
        </tr>
        <tr>
            <td>5642</td>
            <td>Paracetamol</td>
            <td>10.00</td>
        </tr>
        <tr>
            <td>7849</td>
            <td>Crocin</td>
            <td>45.00</td>
        </tr>
        <tr>
            <td>6418</td>
            <td>Condom</td>
            <td>12.00</td>
        </tr>
        <tr>
            <td>4572</td>
            <td>Vitamin D</td>
            <td>5.00</td>
        </tr>
    </table>
</form>

I wanted to add this data to a dict(internal Structure of dictunary defined above) and this dictionary to $_POST.

Advertisement

Answer

you can use the hidden fields for each row and items and then post it

<form action="/page.php" method="post">
    <table id="tbl">
        <tr>
            <th>Item Code</th>
            <th>Item</th>
            <th>Rate</th>
        </tr>
        <tr>
            <td>5642 <input type="hidden" name="item_code[]" value="5642"/></td>
            <td>Paracetamol <input type="hidden" name="item[]" value="Paracetamol"/></td>
            <td>10.00 <input type="hidden" name="rate[]" value="10.00"/></td>
        </tr>
        <tr>
            <td>7849 <input type="hidden" name="item_code[]" value="7849"/></td>
            <td>Crocin <input type="hidden" name="item[]" value="Crocin"/></td>
            <td>45.00<input type="hidden" name="rate[]" value="45:00"/></td>
        </tr>
        <tr>
            <td>6418 <input type="hidden" name="item_code[]" value="6418"/></td>
            <td>Condom <input type="hidden" name="item[]" value="Condom"/></td>
            <td>12.00 <input type="hidden" name="rate[]" value="12:00"/></td>
        </tr>
        <tr>
            <td>4572</td>
            <td>Vitamin D <input type="hidden" name="item[]" value="Vitamin D"/></td>
            <td>5.00 <input type="hidden" name="rate[]" value="5:00"/></td>
        </tr>
    </table>

<button type="submit">Submit</button>
</form>
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement