2

I know variations of this question have been asked, but this is a different question than what I have been able to find. I am building a dynamic list of subscriptions on a single page with information from the database and each subscription has a "cancel" button that uses some form data to make that cancellation happen.

Here is an example of the cancel button on each table row:

<form method="post" action="">
  <?php $token = $_SESSION['token'] = md5( session_id() . time(). rand() ); ?>
  <input type="hidden" name="token" value="<?php echo $token; ?>" />
  <input type="hidden" name="sg_subscriptionID" value="<?php echo $subscriptionID; ?>" />
  <input type="hidden" name="sg_postID" value="<?php echo $post->ID; ?>" />
  <input type="submit" class="button" name="submit" value="Cancel" />
</form>

To process the form, at the top of the file I am using <?php if(isset($_POST['submit']) {} ?>

This is not working when there are multiple forms on the page, I suspect because it can't differentiate between form data based on the input name "submit".

There isn't a definitive number of forms to be created, so I can't simply just have "submit1", "submit2". I can add a number dynamically using $count, but how would I check for an array of dynamic numbers in my form handling script?

I appreciate any suggestions for a possible better approach. I am trying to steer clear of ajax if at all possible.

2
  • You don't need this line <input type="hidden" name="token" value="<?php echo $token; ?>" /> since you already have $_SESSION['token'] you can pick up the value any where else on your site except your posting the form to a different server. Commented Sep 28, 2018 at 4:30
  • Also as much as the accepted answer is correct i would suggest using radio buttons or check buttons to tick on a selected subscriptions then one cancel and accept button a good example of this is yahoo mail box system you select all the mails you wish to delete then hit the delete button. Commented Sep 28, 2018 at 4:40

2 Answers 2

3

You can check for multiple forms in PHP.

for ($i = 0; $i < $count; $i++){
    if (isset($_POST['submit'.$i])){ // check for every forms
        // insert your own logic
    }
}

It doesn't matter how many forms you have, it can be identified using this code above.

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

2 Comments

That is exactly what I needed!
Just to point out for anyone else using this, you will need to define $count. I defined it as $count = 100;
1

You can use html input arrays for multiple forms.

Example:- In you code:-

<form method="post" action="">
  <?php $token = $_SESSION['token'] = md5( session_id() . time(). rand() ); ?>
  <input type="hidden" name="token[<?php echo 'form_'.$i ?>]" value="<?php echo $token; ?>" />
  <input type="hidden" name="sg_subscriptionID[<?php echo 'form_'.$i ?>]" value="<?php echo $subscriptionID; ?>" />
  <input type="hidden" name="sg_postID[<?php echo 'form_'.$i ?>]" value="<?php echo $post->ID; ?>" />
  <input type="submit" class="button" name="submit[<?php echo 'form_'.$i ?>]" value="Cancel" />
</form>
<!-- $i is the key for loop -->

And in your php code:-

$submits = $_POST['submit'];
// $submits loks like ['form_1']
// Now take the first key
$key = array_keys($submits)[0];
// This is the token for the given submit button
$token = $_POST['token'][$key];

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.