Reputation: 1074
So here is my question. Can i somehow put this Onclick event that gives Javascript function variable $i from Php without using javascript,as i want to get the number of clicked button. I know it has something to do with button name, but with that button name i only know how to see if button is clicked with isset (i don't know how to pass value if its clicked to function), i would like to do this entirely in php, but i am stuck here as i don't know what to do, any help would be appreciate.
<?PHP
$i=0;
foreach($test2 as $test)
{
$delete ="<INPUT TYPE=BUTTON OnClick='submit_btn(" . $i. ")' NAME='delete".$i."'VALUE='delete'>";
$i++;
}
?>
<script type="text/javascript">
function submit_btn(btnClick) // < -- How to do this in PhP
{
alert(btnClick); // echo would go here
}
</script>
Upvotes: 1
Views: 4570
Reputation: 15220
<?php
if(isset($_POST['delete']) && is_array($_POST['delete'])) {
echo 'Button ' . $_POST['delete'][0] . ' was clicked.';
}
$test2 = array(1, 2, 3, 4, 5, 6);
?>
<!doctype html>
...
<html>
<form method="post">
<?php
$i=0;
foreach($test2 as $test) {
echo "<button type='submit' name='delete[]' value='button_{$i}'>
Delete-Button #{$i}
</button>";
$i++;
}
?>
</form>
</html>
Here's a working example: http://codepad.viper-7.com/LQpHvp
Note that (unlike with JavaScript and Ajax techniques) this will always require a page refresh.
Upvotes: 1
Reputation: 249
Try this (put it in a test.php file to test it)
<form method="post"><?php
$i=0;
$test2 = array(1,2,3,4,5,6,7,8);
foreach($test2 as $test)
{
echo '<input type="submit" name="delete" value="'.$i.'" />';
$i++;
}
?></form>
<p><?php
// get result
if (isset($_POST['delete']))
{
echo $_POST['delete'];
}
?></p>
Upvotes: 0