laos yu
laos yu

Reputation: 1

Button in for loop

I am using for loop to create several buttons, like this

<form method="get" action="index.php">
<table width="90%"  border="0">
        <tr>
            <td>
                brand: <input type="text" name="brand"/>
                <input type="submit"    value="search">
            </td>
        </tr>
</table>
<?php
$brand = isset($_GET['brand'])  ? $_GET['brand'] : '';
for($i=0; $i<count($data); $i++){
//some code
    echo "<br><input type='button' name='Delete' value='Delete' onclick=\"this.form.action='delete.php';this.form.submit();\">";
}
?>

When I click submit, it will create several buttons. I want that when onclick the button, it can pass different $i to delete.php, how can I do this? Thanks!

Upvotes: 0

Views: 145

Answers (1)

Absorbing
Absorbing

Reputation: 95

Within delete.php you can check a GET parameter (from the URL - e.g. delete.php?id=1) and handle that based on the data passed

for($i=0; $i<count($data); $i++){
//some code
    echo "<br><input type='button' name='Delete' value='Delete' onclick=\"this.form.action='delete.php?id=$i';this.form.submit();\">";
}

The point of note here is 'delete.php?id=$i', again assuming your file handles it by id.

Within delete.php it could be handled with something like the following;

if(isset($_GET['id') && $_GET['id'] != '') {
    // do something here with $_GET['id']
}

Upvotes: 1

Related Questions