2

I develop a simple blog site. There is a list of posts with "view post" button and "remove post" check-box below each post on my page. Also there is a "delete checked posts" at the bottom of the page. I'd like to send to a server a particular id when the "view post" is pressed and to send a list of ids when the "delete checked posts" button pressed. The problem is that I need nested forms for this, but they are not allowed.

How to overcome this?

I'm thinking of having a form for each post and on "delete checked posts" submit all checked forms via javascript.

Is it a good approach? Thanks in advance)

1
  • To be clear: there is no such thing as nested forms in HTML. However, it sounds like what you want to do is have different actions be taken, i.e. a form with different submit buttons. Just give each submit button the same name and a different value, then check the posted value when handling the form as Quentin says. The rest of the data in the form will be submitted as well. Commented Feb 27, 2015 at 23:29

1 Answer 1

2

Put appropriate data in the submit buttons:

<button type="submit"
        name="view_post"
        value="1234">
        View Post
</button>

<button type="submit"
        name="delete_checked_posts"
        value="delete_checked_posts">
        Delete checked posts
</button>

Test to see which of the two submit button names appears in the submitted data. If it is view_post then ignore the checkbox data and just view the post. Otherwise, loop over the checkbox values and delete away.

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

2 Comments

Thanks for reply) But is it a good solution? What if I'd like to pass one more parameter except id on "view_post". I guess I won't be able to put 2 parameters in the value attribute
Yes, it is a good solution. What other parameter would you want to pass? Isn't it something you can infer from the identifier for the post?

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.