-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.

SenTisso
  • 579
  • 7
  • 18
  • 2
    How is your form submitted if it has no elements? – Adder Apr 19 '18 at 14:36
  • In form name attribute is post_name and in php u checking only name user if (isset($_POST['post_name'])) {} – Null Pointer Apr 19 '18 at 14:39
  • 1
    `$_POST['name']` is a reference to the input (and other) form elements, not teh form itself – xander Apr 19 '18 at 14:40
  • @xander I know. I wrote it there, but I want to know how to define name of form itself. – SenTisso Apr 19 '18 at 14:42
  • So to make this clearer, are you trying to identify which form submitted the form element? – Darren Crabb Apr 19 '18 at 14:43
  • There is no way in php to get form name directly. you can get with adding hidden field to every form. `` OR `The name or value of the submit button.` – Noman Apr 19 '18 at 14:44
  • I don't think the name of the form is submitted at all, so you cannot read the name of it, so you need to use other variables to identify if you have multiple forms.. – xander Apr 19 '18 at 14:44
  • I'm trying to identify which post is being submited, input it would be like this `if (isset($_POST['input_name']) {}`, but I don't want that. – SenTisso Apr 19 '18 at 14:46
  • @Adder in this time I don't care, to be honest I was lazy to write some inputs as examples – SenTisso Apr 19 '18 at 14:51

2 Answers2

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>
    
  • 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. – SenTisso Apr 19 '18 at 14:55
  • how does your answer is different from this ? https://stackoverflow.com/questions/846020/how-to-access-the-forms-name-variable-from-php?answertab=oldest#tab-top – Noman Apr 19 '18 at 14:57
  • I was "asking" if I get this right, but that's my bad, sorry. – SenTisso Apr 19 '18 at 14:59
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.