1

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>

2 Answers 2

1
<?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.

Sign up to request clarification or add additional context in comments.

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.