rozniy
(Rozniy)
February 22, 2011, 3:24pm
1
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.
teher811
(Thomas Roch)
February 22, 2011, 3:43pm
2
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).
larissab
(Larissab)
February 22, 2011, 3:43pm
3
rozniy:
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.
Try print_r($_POST); you will see all the posted data
rozniy
(Rozniy)
February 22, 2011, 3:54pm
4
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…
teher811
(Thomas Roch)
February 22, 2011, 4:08pm
5
rozniy:
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…
rozniy
(Rozniy)
February 22, 2011, 6:29pm
6
teher811:
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:
18:23:19.153068 warning application
Failed to set unsafe attribute "username".
in D:\yiitesting\species\protected\controllers\LoginFormController.php
(25)
in D:\yiitesting\species\index.php (13)
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.
mikl
(Mike)
February 22, 2011, 10:24pm
7
rozniy:
The attribute is unsafe? Wow, Yii is pretty strict.
I managed to resolve it, but wow, I thought C++ GUI programming was hard…
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?