0

I've a table being populated with courses. Each row represents a different course and each row has a register button which when clicked will register the user to that course.

My problem is this...How can I detect which button is pressed in the table and perform the relevant actions using PHP?

This is my code:

<form action="#" method="POST">
            <table>
            <tr>
                <th>Date</th>
                <th>Venue</th>
                <th></th>
            </tr>
            <? while(have_rows('location')): the_row(); ?>
                <tr>
                <td> <?= date("d-m-Y", strtotime(get_sub_field('date'))) ?></td>
                <td><? the_sub_field('venue') ?></td>
                <td><input type='submit' value="Register" class="register"</td>
                </tr>
            <? endwhile ?>
            </table>
            </form>

At the moment I assume when I click the register button it will send all of the rows of the table to the script. I would like to send one depending on which register button was clicked.

Javacadabra
  • 5,578
  • 15
  • 84
  • 152
  • 2
    I think you should use ajax/jquery for this – Khushboo Jun 16 '14 at 11:46
  • Maybe helpful [how-can-i-tell-which-button-was-clicked-in-a-php-form-submit](http://stackoverflow.com/questions/2680160/how-can-i-tell-which-button-was-clicked-in-a-php-form-submit) – Debflav Jun 16 '14 at 11:48
  • I suggest you to add a different name to your submit button and then, in your PHP, the only parameter posted will have the name of the button you clicked. But I agree with @Khushboo, ajax/jQuery can helps. – niconoe Jun 16 '14 at 11:49
  • @Debflav This isn't really a good idea in this case.. – Naruto Jun 16 '14 at 11:50
  • @Naruto I don't understand ? You must had unique name to each button to know which one was clicked. Another way / advice ? – Debflav Jun 16 '14 at 11:57
  • @Debflav How do I explain this? The thing you linked works, but then you have to write an isset for each button he creates.. If he creates alot of buttons, that isn't the ideal working method, and he should better use ajax, considdering courses will be added dynamically.. – Naruto Jun 16 '14 at 12:00
  • why does it need to be a POST submit?? Using buttons which call a URL specific for each row / course would be much easier.. – RaggaMuffin-420 Jun 16 '14 at 12:03
  • @Naruto Ajax or not, he must use an identifier store in an hidden input or submit input and then check on the PHP side if data are correct. – Debflav Jun 16 '14 at 12:06
  • @Debflav Yes, but then he needs a dynamic piece of code and not x-zillion isset($_POST) of buttons.. – Naruto Jun 16 '14 at 12:38
  • @Naruto You can do it without x-zillion isset() but only one foreach. – Debflav Jun 16 '14 at 12:55
  • @Debflav My point is use the foreach.. – Naruto Jun 16 '14 at 13:21

2 Answers2

0
<form action="#" method="POST">
            <table>
            <tr>
                <th>Date</th>
                <th>Venue</th>
                <th></th>
            </tr>
            <?php $i=0; while(have_rows('location')): the_row();?>
                <tr>
                <td> <?= date("d-m-Y", strtotime(get_sub_field('date'))) ?></td>
                <td><? the_sub_field('venue') ?></td>
                <td><input type='submit' value="Register" class="register" id="<?=$i;?>"/></td>
                </tr>
            <?php $i++; 
              endwhile ?>
            </table>
            </form>

put a variable in while loop and increment it in each loop,then you can easily identify each loop..which is explained above..

Or else,if you need to add any specific id like student id as the unique value,you can put that also in the id attribute. you can identify each of the rows through java script or jquery

Vaisakh Pc
  • 714
  • 8
  • 21
  • 1
    That is a good solution, but the problem I see here, on the front end it will generate the relevant `id` for each button, but on the back end my `PHP` will not know if there is 1 `button` or 100 `buttons`, this way I would have to write a condition for each button generated? – Javacadabra Jun 16 '14 at 11:53
  • 1
    Do you really need to know HOW MANY buttons are there ? Or is the fact only which one was pressed ? – Kei Jun 16 '14 at 11:54
  • Only which one was pressed, but there could potentially be 100 items in the table – Javacadabra Jun 16 '14 at 11:54
  • but it is possible that you can assign some student id to each of those entries right? – Vaisakh Pc Jun 16 '14 at 11:55
  • you can set a button value in one common hidden fied in onclick event. by this way, you can get that pressed button value in php. – Asik Jun 16 '14 at 11:57
0

The answer to your question can be achieved in many ways.

The first one is to have ONE form for EACH course, and inside EACH one you put a

<input type="hidden" value="<?php $i; ?>">

The second one would be to name the submit button, and check with PHP through a loop between 1 and count($rows)

<input type="submit" name="<?php $i; ?>" value = "register">

The second solution is quite tricky, whilst the first one would be ideal.

Kei
  • 771
  • 6
  • 17