always confused by $_POST['UserLogin']

I am confused every time, when people use


if(isset($_POST['UserLogin']))

			{...}

, for example in the following code from simpleLogin( http://www.yiiframework.com/extension/simplelogin/).


	public function actionLogin()

	{

		if (Yii::app()->user->isGuest) {

			$model=new UserLogin;

			// collect user input data

			if(isset($_POST['UserLogin']))

			{

				$model->attributes=$_POST['UserLogin'];

				// validate user input and redirect to previous page if valid

				if($model->validate()) {

					$this->lastViset();

					if (strpos(Yii::app()->user->returnUrl,'/index.php')!==false)

						$this->redirect(Yii::app()->controller->module->returnUrl);

					else

						$this->redirect(Yii::app()->user->returnUrl);

				}

			}

			// display the login form

			$this->render('/user/login',array('model'=>$model));

		} else

			$this->redirect(Yii::app()->controller->module->returnUrl);

	}

From the PHP manual, I can learn $_GET[‘name001’], the name001 is the id = name001 inside a HTML tag, e.g.: <form id = “name001” method = GET>…</form>, but in Yii, it seems totally different. I can’t find any id attribute in any tag in the rendered HTML page.

My question is why it has to be ‘UserLogin’ inside the $_POST[‘UserLogin’], why not ‘MyUser0001’? Where else ‘UserLogin’ is defined, that we have to use it here?

Please let know where I am wrong.

Nettrinity, to give you an idea how it works, open up a new php file called post.php and copy the following bit into it (I don’t care about the html headers, as it’s just a real quick demonstration):


<?php var_dump($_POST); ?>


<form id="test-form" action="post.php" method="post">

    First name: <input type="text" name="User[firstName]" /><br />

    Last name: <input type="text" name="User[lastName]" /><br />

    Favorite meal: <input type="text" name="Bio[favoriteMeal]" /><br />

    <input type="submit" value="Send" /><br />

</form>

If you fill out the forms with first name John, last name Doe and favorite meal Lasagna and then submit the form, you should get the form back with the following var_dump() at the top of the page:


array

  'User' => 

    array

      'firstName' => string 'John' (length=4)

      'lastName' => string 'Doe' (length=3)

  'Bio' => 

    array

      'favoriteMeal' => string 'Lasagna' (length=7)

We created the User and Bio sub-arrays by using a special notation for the form input names, like:


User[firstName]

Yii uses the same principle, but uses the form Model_name[attribute_name]. Make sure to check this page in the Yii guide: http://www.yiiframework.com/doc/guide/1.1/en/form.action

In your example, the model is called UserLogin and therefore you check if $_POST[‘UserLogin’] has been set. You can also see the same thing in the contact form of a fresh Yii application. There the model is called LoginForm and the login action in the controller must check if that model was set in $_POST. If it was, it means that the form was submitted.

It is possible that a page contains two or more forms. For example, a search form and a login form. By using the model-name method, you can find out which form was submitted: search, or login.

jodev,

First, thank you very much for such a detailed explanation!

I did find a LoginForm model, and $_POST is always there, and auto populated after the form is submitted. The name attribute in each input tag is auto populated by Yii with modelName[dbTableColumnName]. After submission, $_POST is a nested array. Then you can work with $_POST[modelName] or $_POST[modelName][dbTableColumnName].

I think I finally got it clear:) Thank you again!