Newbie question: trying to get the knack of SUBMITTING forms

Hi all,

I hope you’ll be patient with me. I’ve been trying to follow the tutorials about creating a form and submitting the data. I don’t want to store the data in the database yet. All I want to do is Echo something I’ve inputted in a textbox.

The php is doing something. The browser’s address bar shows

"http://127.0.0.1/species/index.php/animals/index/nameholder?NameHolder[genusname]=Panthera&yt0=submit"

after I click the button after typing “Panthera”. But I’m not getting anything from it to Echo.

this is inside the form in "nameholder.php"





<?php echo CHtml::BeginForm($action='',$method='POST'); ?>

<?php


$animalgenus= new NameHolder;





echo CHtml::activeTextField($animalgenus, 'genusname'); 

echo CHtml::submitButton('submit');




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

	{

	$animalgenus->attributes=$_POST['NameHolder'];

	}

	




echo $animalgenus->genusname;

?>


<?php echo CHtml::EndForm() ?>


<?php echo $animalgenus->genusname; ?>






and this is the NameHolder class that was supposed to hold the variable:


<?php


class NameHolder extends CFormModel

{

public $genusname;


public function rules()

	{

	return array(

		array('genusname', 'required')

		);

		

	}


}


?>

What did I do wrong? I just want to Echo the genusname I typed in the textbox. There is something I’m not getting.

First your address bar shows a GET request, not a POST request. No capital b on beginForm.


<?php echo CHtml::beginForm('','post');

// The following line will do the exact same thing

echo CHtml::beginForm(); ?>

$_POST[‘genusname’] doesn’t exist, but $_POST[‘NameHolder’][‘genusname’] does.




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

{

      $animalgenus->attributes=$_POST['NameHolder'];

}



Overall you should separate your view and your controller (where you check your post).

Try print_r($_POST); you will see all the posted data

I forgot to mention that the first snippet of code is inside the Views folder, and the second is inside the Models folder, so I did separate View and Model.

print_r($_POST) didn’t output anything… :(

Read again my post…

print_r($_POST) WON’T PRINT anything because your url tells your form generated a GET request and not a POST one.

Then I said you should separate View and Controller, not View and Model…

Thanks for the replies.

I redid everything. I thought I had everything covered, separated the model, view, controller, etc…

I clicked the Submit button and got a warning:

The attribute is unsafe? Wow, Yii is pretty strict.

I managed to resolve it, but wow, I thought C++ GUI programming was hard… :)

Anyway thanks.

That’s a very important safety measure (adhering to the “never trust user input” principle). Think of a model attribute $is_admin that gives administrator rights to a user, if set to 1. You obivously don’t want your users to overwrite this property just because they submit a fake POST request that contains this column, do you?