Sinisterly
Handling multiple automatically generated buttons? - Printable Version

+- Sinisterly (https://sinister.li)
+-- Forum: Coding (https://sinister.li/Forum-Coding)
+--- Forum: PHP (https://sinister.li/Forum-PHP)
+--- Thread: Handling multiple automatically generated buttons? (/Thread-Handling-multiple-automatically-generated-buttons)



Handling multiple automatically generated buttons? - Harvey - 10-04-2013

Sup folks?

I've got a script that generates lots of buttons in a loop. We currently look like this.

[Image: mAjKJQ2.png]

The buttons are generated using the following code (everything's being read from a database).

Simplified version:

PHP Code:
<?php while($rows = mysql_fetch_array($query)): assign variables and output other shit here echo "<td>" . "<center><input type='submit' value='Edit' name='edit_post_" . $post_id . "'></center>" . "</td>"; endwhile; ?>

I left some of the formatting shit out but you get the point. Each button generated is called (for example) 'edit_post_7' or 'edit_post_8'.

The problem is, I want to make ALL these buttons do a very similar thing (e.g. run a function). But I don't want to have to do this for each of them.

PHP Code:
if(isset($_POST['edit_post_7'])){ do my shit } if(isset($_POST['edit_post_8'])){ do essentially the same shit }

I can't do this because there could be thousands of buttons.

What's the best way to handle these events?


RE: Handling multiple automatically generated buttons? - PolarDesigns - 10-04-2013

Code:
<?php while ( $rows = mysql_fetch_array ( $query ) ) { echo "<td><center><a href='index.php?edit&id={$post_id}' title='Edit post'>Edit</a></center></td>" } ?>

Then to process it:
Code:
<?php if ( isset ( $_REQUEST['edit'] ) ) { if ( isset ( $_REQUEST['id'] ) && is_numeric ( $_REQUEST['id'] ) ) { // do shit here with $_REQUEST['id'] instead of $_POST['edit_post_#'] } } ?>

This is a very basic and insecure way of doing it, but it should get your job done... Hope this is what you are looking for..


RE: Handling multiple automatically generated buttons? - Harvey - 10-04-2013

Brilliant. Thank you!


RE: Handling multiple automatically generated buttons? - Fhoto - 10-04-2013

I was about to answer this for you, anyway Polar answered.