get value of input element with “id” or “name”

2020-03-02 06:55发布

I am trying to learn php and I have seen some videos and tutorials, when creating session variables which values are going to be pulled out from values entered in input elements, the "id" of the input element is the one that matters while when checking if a submit button was clicked is the "name" of the input element what matters?

<?php

if (isset($_POST['Register'])) {  //Register is the "name" 
   session_start();
   $Fname = $_POST['first_name'];  //first_name is the "id"
    ...
}
?>
<!doctype html>
...
<form action="" method="post" name="registerform" id="registerform">
<input name="firstname" type="text" id="first_name">

...
<input name="Register" type="submit" id="register" value="submit">

3条回答
Lonely孤独者°
2楼-- · 2020-03-02 07:09

Id and Classes are mainly for CSS or JavaScript purpose.Use name for getting the post values $_POST['firstname'].

<input name="firstname" type="text" id="first_name">
查看更多
劫难
3楼-- · 2020-03-02 07:09

Following are my guidelines:

1) Whenever we post a form, only name will be considered for getting posted value. e.g.

<input type="text" name="fname" id="first_name"/>

Here we get $_POST['fname'] Not $_POST['first_name'];

2) Id and classes are there for CSS/JS purposes. Thus if you add whatever class/id attributes to the element,

Only name gets posted.

3) In the array $_POST, name is the key and value in it is a value.

查看更多
劳资没心,怎么记你
4楼-- · 2020-03-02 07:15

$_POST['firstname'] <> $_POST['first_name']

It takse name atribute, not the ID one. You have to use

$fname = $_POST['firstname'];
查看更多
登录 后发表回答