Jump to content

Form tag and name attribute


LittleJoe

Recommended Posts

I'm wondering whether the name attribute has any purpose in the form tag because when I see PHP scripts working out which form was submitted to a page, in cases where a page has multiple forms and they all submit to the same page, they always just look for the submit button using the name attribute to realise which form it was that submitted to the page. So when would you want to look for the name on the form tag itself? I ran some tests in a PHP script and I was always able to tell which form submitted to my page looking for the submit button but the name attribute on the form tag itself was always empty. Another related question; if you have multiple forms and some of the input fields in the various forms have the same name, how can you filter input fields or other form fields by a specific form? Can you do in PHP for example: "give me the field where name is "email" in the form where name is "loginform"? Thanks.

Link to comment
Share on other sites

The name attribute makes a big difference. How else can you easily keep track of all the inputs that can be potentially submitted? http://www.w3schools.com/tags/att_input_name.asp

Edited by niche
Link to comment
Share on other sites

The name attribute makes a big difference. How else can you easily keep track of all the inputs that can be potentially submitted? http://www.w3schools..._input_name.asp
Yes, I know. I'm not talking about the name attribute in general but when used in the form tag: <form name="formname"></form> Another related question; if you have multiple forms and some of the input fields in the various forms have the same name, how can you filter input fields or other form fields by a specific form? Can you do in PHP for example: "give me the field where name is "email" in the form where name is "loginform"? Edited by LittleJoe
Link to comment
Share on other sites

No, there's no name attribute on the <form> element. That's a bad practise that originated in the 90s. It has nothing to do with PHP, it was used in Javascript.

Link to comment
Share on other sites

No, there's no name attribute on the <form> element. That's a bad practise that originated in the 90s. It has nothing to do with PHP, it was used in Javascript.
So how do you go about figuring out which form was submitted when multiple ones have fields with the same name such as in this example:
<form action="index.php" id="login" method="post"><input name="email" type="text"><input name="loginSubmit" type="submit"></form> <form action="index.php" id="register" method="post"><input name="email" type="text"><input name="registerSubmit" type="submit"></form>

Both forms have an input named "email".

Edited by LittleJoe
Link to comment
Share on other sites

<?phpif(isset($_POST['loginSubmit']) && isset($_POST['email']) && !empty($_POST['email']) ){echo "from 'login' form email= ";echo  $_POST['email'];}else if(isset($_POST['registerSubmit']) && isset($_POST['email']) && !empty($_POST['email'])){echo "from 'register' form email =  ";echo  $_POST['email'];}else{echo "no email provided";}?>

Link to comment
Share on other sites

<?phpif(isset($_POST['loginSubmit']) && isset($_POST['email']) && !empty($_POST['email']) ){echo "from 'login' form email= ";echo  $_POST['email'];}else if(isset($_POST['registerSubmit']) && isset($_POST['email']) && !empty($_POST['email'])){echo "from 'register' form email =  ";echo  $_POST['email'];}else{echo "no email provided";} ?>

So basically, you have to go through the submit buttons and see if they're set and then look for the input field named "email".
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...