Using Multiple Submit buttons to trigger customised php functions

I may be going about this completely the wrong way but bear with me… I have a long guestlist that is automatically populated using a foreach table.

I would like to put a ‘No Show’ button on this guestlist so that without being admin the host of the event can click (client side) “No Show” and update the ticket (custom post) meta to State: ‘noshow’

Read More

A lot of the examples I have found mean making a new .php page for the submit button to link to and run the code [obviously because it is server side language] – the first problem is then, once they have clicked the submit button, it goes to a blank page (having no real HTML output) rather than returning to their event… This isn’t such a big problem – I can put a redirect in I suppose but seems a bit clunky.

The second problem is also that many of these examples don’t take into account A) Multiple Submit Buttons that have to be automatically produced B) Different PHP code to be triggered for each one…

Here is the jist of what I need doing below:

        <form onsubmit="return confirm('Do you want to mark <?php echo $FullName ?> as a 'No Show' - this cannot be undone unless you are an admin');" method="post" action="<?php HELP ?>">
        <input name="NoShow<?php echo $IDuser?>" type="submit" value="No Show" class="button" /> //$IDuser is their user ID to give the values something different on the page. Could also be ticket [post] numbers?
        </form>

Now on the server side I need something like this to run:

    $tixID = $ticket['id']; //This gets the ticket [custom post] ID number
            $StateKey = state; //This is the meta_key that needs changing
            $NoShowing = noshow; //This is what it needs to change to.
                  update_post_meta($tixID , $StateKey , $NoShowing ); #This is the code to change the State: to No Show
            ?>

So I gather I need to use something along the lines of

if(isset($_POST['SUBMIT-BUTTON'])) { 
some_function_called_here(); 
} 

But this is fixed and is looking out for a certain name – It would be physically impossible for me to type out the different submit button names as it would be changing individual tickets – of which about 6,000 have already been sold on the site… Also the function called would have to be unique to the button that called it.

Any shining light would be great as I feel like I’m going round in circles with some of these blog posts.

EDIT:
Sorry – for clarification.

What I need to happen is…

  1. Trigger – User presses one button in a list of buttons client side. It is generated in a table using php and a foreach that lists tickets bought for that event.
    <input name="noshow<?php echo $ticketID ?>" type="submit" value="No Show" class="button" />

  2. Action – Each particular button corresponds to a particular ticket, once the button is clicked for that ticket it runs the code to edit that ticket post meta data, server side.
    update_post_meta($ticketID , 'state' , 'noshow' ); This updates the key State: to noshow

  3. Result – Page is reloaded and you remain on the same event page

Here’s a visual example of the screenshot

Screenshot Example of scenario

Related posts

Leave a Reply

1 comment

  1. Instead of redirecting to a new page, you can use the one you’re already on with basic logic similar to this:

    if ( !empty( $_POST['ticket_action'] && ( $_POST['ticket_action'] == 'noshow' ) ) {
        $ticket_post_id = $_POST['ticket_post_id'];
        ... you probably want to check if the user is logged in or check for a valid nonce here ...
        // do whatever it is with the ticket post that you wanted to do here
        ?>
        <p>Success!! Or Error, or some other message dependent on what happened in form processing</p>
        <?php
    }
     .... later on ...
        ?>
        <form method="post" action="">
            <input type="hidden" name="ticket_post_id" value="<?php the_ID();?>" />
            ... you should probably put a nonce here too, search this site for examples and explanations on what they are ...
            <input type="hidden" name="ticket_action" value="noshow" />
            <input type="submit" value="no show" />
        </form>
        <?php
    

    Perhaps you will have a form for each button, or a single form, that part is a generic PHP/HTML question beyond the scope of this site ( stack overflow would be the appropriate place )