Yii Framework Forum: $_POST Problems - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

$_POST Problems Rate Topic: -----

#1 User is offline   croker10 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 09-July 10

Posted 13 July 2010 - 04:07 PM

Now a week into php and Yii, I am running into some obstacles. I am trying to link to a new page depending on the selected dropdown item, passing the value to _POST, but for some reason, _POST is never set. I have my code listed below. Thanks for looking. Any help will be greatly appreciated.

controller code:

public function actionList()
{
if(isset($_POST['id']))
{
$this->render($industry->name, array('/industry/show','id'=>$industry->id));
}
else
{
$criteria=new CDbCriteria;

$pages=new CPagination(Contact::model()->approved()->count($criteria));
$pages->pageSize=self::PAGE_SIZE;
$pages->applyLimit($criteria);

$sort=new CSort('Contact');
$sort->attributes = array(
'user'=>array(
'asc'=>'lname, fname',
'desc'=>'lname DESC, fname DESC',
'lable'=>'Contact'
),
'graduation'=>'graduation',
);
$sort->applyOrder($criteria);

$models=Contact::model()->approved()->with('companies')->findAll($criteria);

$industries = array();
foreach(Industry::model()->findAll() as $industry)
{
$industries[$industry->id] = $industry->name;
}

$this->render('list',array('models'=>$models,'sort'=>$sort,'industries'=>$industries,'pages'=>$pages));
}
}

view code: (this isn't all of the view code, but the relevant parts. if you think I need to supply something else, let me know!)


<div id="filter">
Filter by Industry:

<?php echo CHtml::dropDownList('id','name', $industries); ?>

<?php echo CHtml::submitButton('Filter'); ?>
<br />
</div>
<? if(!empty($testMessage))
{
?><div id="test"> <?php echo $testMessage; ?></div><?
$testMessage = "";
} ?>



Thanks again!
0

#2 User is offline   whoopass 

  • Advanced Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 119
  • Joined: 27-May 09
  • Location:Ajax, Ontario, Canada

Posted 13 July 2010 - 06:44 PM

View Postcroker10, on 13 July 2010 - 04:07 PM, said:

Now a week into php and Yii, I am running into some obstacles. I am trying to link to a new page depending on the selected dropdown item, passing the value to _POST, but for some reason, _POST is never set. I have my code listed below. Thanks for looking. Any help will be greatly appreciated.

controller code:
<?php
  public function actionList()
        {
                if(isset($_POST['id']))
                {
                        $this->render($industry->name, array('/industry/show','id'=>$industry->id));
                }
                else
                {
                        $criteria=new CDbCriteria;

                        $pages=new CPagination(Contact::model()->approved()->count($criteria));
                        $pages->pageSize=self::PAGE_SIZE;
                        $pages->applyLimit($criteria);

                        $sort=new CSort('Contact');
                        $sort->attributes = array(
                                'user'=>array(
                                        'asc'=>'lname, fname',
                                        'desc'=>'lname DESC, fname DESC',
                                        'lable'=>'Contact'
                                ),
                                'graduation'=>'graduation',
                        );
                        $sort->applyOrder($criteria);

                        $models=Contact::model()->approved()->with('companies')->findAll($criteria);

                        $industries = array();
                        foreach(Industry::model()->findAll() as $industry)
                        {
                                $industries[$industry->id] = $industry->name;
                        }

                        $this->render('list',array('models'=>$models,'sort'=>$sort,'industries'=>$industries,'pages'=>$pages));
                }
        }

view code: (this isn't all of the view code, but the relevant parts. if you think I need to supply something else, let me know!)

<?php
<div id="filter">
        Filter by Industry:

        <?php echo CHtml::dropDownList('id','name', $industries); ?>

        <?php echo CHtml::submitButton('Filter'); ?>
        <br />
</div>
<? if(!empty($testMessage))
{
        ?><div id="test"> <?php echo $testMessage; ?></div><?
        $testMessage = "";
} ?>



Thanks again!


I see the problem, according to what you have here:

<?php

$industries = array();
                        foreach(Industry::model()->findAll() as $industry)
                        {
                                $industries[$industry->id] = $industry->name;
                        }


You are using a foreach statement to find all the industries and store it into $industry. So $industry is simply a variable, it's not a class thus $industry->id will not work.

Try this:

<?php

foreach(Industry::model()->findAll() as $industry)
{
     $industryID=$industry['id'];
     $industries[$industryID] = $industry['name'];
}


That should do the trick.

I'm assuming you want to make an array where the indexing values of the array are the ids of the industry and they hold the name of the industry as it's value.
0

#3 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 14 July 2010 - 01:37 AM

You should include the dropDownList in a form, in order to have the submit of his value:

<div id="filter">
        Filter by Industry:
<?php echo CHtml::Form();?>
        <?php echo CHtml::dropDownList('id','name', $industries); ?>

        <?php echo CHtml::submitButton('Filter'); ?>
        <br />

<?php echo CHtml::endForm();?>
</div>


You can also enjoy to use autoSubmit feature of dropDownList:

<div id="filter">
        Filter by Industry:
        <?php echo CHtml::dropDownList('id','name', $industries, array('submit'=>'')); ?>

        <br />
</div>


In this case is not needed even the submitButton.
0

#4 User is offline   croker10 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 09-July 10

Posted 14 July 2010 - 09:28 AM

Thanks a lot to both of you! It works great!
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users