Jump to content

How do I submit a single form from multiple forms on the same page.


imgrooot

Recommended Posts

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>

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.