-2

In PHP you can define input's name just like this: if (isset($_POST['name'])) {}, but my question is how can I define exactly this post (<form method='post' action='action.php'> with this name (name='post_name') exists.

So the whole HTML would be like this:

<form method='post' name='post_name' action='action.php'><input name='name' /></form>

I'm trying to achieve this, because I have more forms on my page and I want to work with exactly this one. I can theoretically change action='', but I want to have it in same file or do this: if (isset($_post_name)) {}, but it doesn't work.

9
  • 2
    How is your form submitted if it has no elements? Commented Apr 19, 2018 at 14:36
  • In form name attribute is post_name and in php u checking only name user if (isset($_POST['post_name'])) {} Commented Apr 19, 2018 at 14:39
  • 1
    $_POST['name'] is a reference to the input (and other) form elements, not teh form itself Commented Apr 19, 2018 at 14:40
  • @xander I know. I wrote it there, but I want to know how to define name of form itself. Commented Apr 19, 2018 at 14:42
  • So to make this clearer, are you trying to identify which form submitted the form element? Commented Apr 19, 2018 at 14:43

2 Answers 2

2

To identify the each submitted form, you can use:

  • As hidden input field.
  • Set a name or value of the submit button.

    <form name="myform" method="post" action="" enctype="multipart/form-data">
    <input type="hidden" name="frmname" value=""/>
    </form>
    
Sign up to request clarification or add additional context in comments.

3 Comments

So I can bypass it by creating random input in that form and in PHP I would define if that hidden input exists and if it would, that would mean form exists too.
how does your answer is different from this ? stackoverflow.com/questions/846020/…
I was "asking" if I get this right, but that's my bad, sorry.
1

You can use the same action file by using a GET parameter. Instead of naming the form element itself you can change the action this way:

<form method='post' action='action.php?formname=form1'>...</form>
<form method='post' action='action.php?formname=form2'>...</form>

Then in php:

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if ($_GET['formname'] == 'form1') {
        //stuff
    } 
    else if($_GET['formname'] == 'form2' {
        //other stuff
    }
}

Or you can simply add an hidden input element to define which form is posted.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.