imgrooot Posted February 19, 2020 Share Posted February 19, 2020 I am creating a user inbox system. I am retrieving all the unread messages. Each message row contains a "reply" form. So say I have 10 messages showing on a single page. That's 10 forms. What I would like to know is how can I submit any one of the 10 forms and not have it affect the remaining 9 forms? Here is the basic code. if(isset($_POST['submit'])) { $post_message = trim($_POST['message']); $errors = array(); $db->beginTransaction(); if(empty($post_message)) { $errors[] = 'The message field can not be empty!'; } if(empty($errors)) { $db->commit(); echo 'success'; } else { $db->rollBack(); } } <form action="" method="post"> <fieldset> <textarea name="message" maxlength="10000" placeholder="What would you like to say?"></textarea> </fieldset> <fieldset> <input type="submit" name="submit" value="Submit" /> </fieldset> </form> Quote Link to comment https://forums.phpfreaks.com/topic/310075-how-do-i-submit-a-single-form-from-multiple-forms-on-the-same-page/ Share on other sites More sharing options...
kicken Posted February 19, 2020 Share Posted February 19, 2020 Just make sure each form has it's own set of <form> tags and inputs. Add hidden inputs that you can use to differentiate the forms if necessary. Quote Link to comment https://forums.phpfreaks.com/topic/310075-how-do-i-submit-a-single-form-from-multiple-forms-on-the-same-page/#findComment-1574573 Share on other sites More sharing options...
ginerjm Posted February 19, 2020 Share Posted February 19, 2020 As Kicken says..... Just remember that any modifications you make to fields in form A will be lost if the user submits form B. Those inputs (from the A form) will not be sent to the server as part of the submit normally. Nor should they be. Of course - each of your multiple forms should have the very same field 'names' so that your logic will handle each form properly. Quote Link to comment https://forums.phpfreaks.com/topic/310075-how-do-i-submit-a-single-form-from-multiple-forms-on-the-same-page/#findComment-1574581 Share on other sites More sharing options...
imgrooot Posted February 19, 2020 Author Share Posted February 19, 2020 Got it. Here is the updated form. It seems to work fine. // Message ID being an ID of individual message rows retrived from the DB. $message_id if(isset($_POST['submit']) AND $_POST['message-id'] == $message_id) { $post_message = trim($_POST['message']); $errors = array(); $db->beginTransaction(); if(empty($post_message)) { $errors[] = 'The message field can not be empty!'; } else { // ADD CODE HERE } if(empty($errors)) { $db->commit(); echo 'success'; } else { $db->rollBack(); } } <form action="" method="post"> <fieldset> <textarea name="message" maxlength="10000" placeholder="What would you like to say?"></textarea> </fieldset> <fieldset> <input type="hidden" name="message-id" value="<?php echo $message_id; ?>" /> <input type="submit" name="submit" value="Submit" /> </fieldset> </form> Quote Link to comment https://forums.phpfreaks.com/topic/310075-how-do-i-submit-a-single-form-from-multiple-forms-on-the-same-page/#findComment-1574594 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.