Hi all,
Im hoping someone may be able to assist as Im a little perplexed as to why the following does work.
I cannot understand why $_POST is always empty in the controller. Using FirePHP I can see that the browser is sending the POST data
Parametersapplication/x-www-form-urlencoded
Tour[0][date] klj
Tour[0][postCode] lkj
Tour[0][soldOut] 0
Tour[0][streetName] lkj
Tour[0][streetNumber] lkj
Tour[0][suburb] lkj
Tour[0][ticketTable_ticke… 1
Tour[0][time] lkj
Tour[0][tourName] lj
btnSubmit Save
Source
Content-Type: application/x-www-form-urlencoded Content-Length: 286 Tour%5B0%5D%5Bdate%5D=klj&Tour%5B0%5D%5Btime%5D=lkj&Tour%5B0%5D%5BstreetNumber%5D=lkj&Tour%5B0%5D%5BstreetName%5D=lkj&Tour%5B0%5D%5Bsuburb%5D=lkj&Tour%5B0%5D%5BpostCode%5D=lkj&Tour%5B0%5D%5BtourName%5D=lj&Tour%5B0%5D%5BsoldOut%5D=0&Tour%5B0%5D%5BticketTable_ticketId%5D=1&btnSubmit=Save
I have a form and controller as per below
TourController.php
<?php
class TourController extends Controller {
private $alias = array();
public $tempTours = array();
public function getAlias() {
return $this->alias;
}
public function actionCreate() {
$firephp = FirePHP::getInstance(true);
if (!$_GET['id'])
throw new CHttpException('Missing Data', 'No id parameter has been specified');
$band = Band::model()->find('id=:id', array(':id' => $_GET['id']));
if (!$band)
throw new CHttpException('Invalid Band', 'No such band or invalid band Id');
if (!isset($_POST['Tour']))
{
$tickets = Ticket::model()->findAll('bandTable_id=:bandTable_id', array(':bandTable_id' => $_GET['id']));
$ticketList = CHtml::listData($tickets, 'ticketId', 'ticketingURL');
}
$firephp->log(var_dump($_POST));
if (isset($_POST['Tour'])) {
$firephp->log('In tour list');
$tours = $_POST['Tour'];
foreach ($tours as $tour) {
$newTour = new Tour;
$newTour->bandTable_id = $band->id;
$newTour->date = $tour['date'];
$newTour->time = $tour['time'];
$newTour->streetNumber = $tour['streetNumber'];
$newTour->streetName = $tour['streetName'];
$newTour->streetNumber = $tour['streetNumber'];
$newTour->suburb = $tour['suburb'];
$newTour->postCode = $tour['postCode'];
$newTour->soldOut = $tour['souldOut'];
$newTour->tourName = $tour['tourName'];
$newTour->ticketTable_ticketId = $tour['souticketTable_ticketIdldOut'];
try {
$newTour->save();
} catch (CDbException $e) {
throw new CHttpException('Cannot create tour for ' . $band->name);
}
}
$this->redirect(array('tour/create&id=' . $band->id));
}
$this->alias['header'] = 'Create Tour Information';
$this->render('create', array('band' => $band, 'list' => $ticketList));
}
}
create.php
<?php
$baseUrl = Yii::app()->baseUrl;
$cs = Yii::app()->getClientScript();
$cs->registerScriptFile($baseUrl . ‘/js/jquery-1.6.1.min.js’);
$cs->registerScriptFile($baseUrl . ‘/js/jquery-ui-1.8.14.custom.min.js’);
$cs->registerScriptFile($baseUrl . ‘/js/jquery.format.js’);
$cs->registerScriptFile($baseUrl . ‘/js/template.js’);
$cs->registerScriptFile($baseUrl . ‘/js/jquery.calculation.min.js’);
?>
<style>
textarea.template
{
display: none;
}
</style>
<div class="grid_16 widget first">
<div class="widget_title clearfix">
<h2> Create Touring Dates For Band <?php echo $band->name; ?> </h2>
</div>
<div class="widget_body">
<div class="widget_content">
<div class="widget_options clearfix">
<p>Please enter ticketing information below. Select new to create a new row</p>
<?php
$form = $this->beginWidget('CActiveForm', array(
'id' => 'tour-form',
'enableAjaxValidation' => false,
));
?>
<div class="panel">
<table class="templateFrame grid" cellspacing="0">
<thead class="templateHead">
<tr>
<!-- These are the headings -->
<td>
<?php echo $form->labelEx(Tour::model(), 'date'); ?>
</td>
<td>
<?php echo $form->labelEx(Tour::model(), 'time'); ?>
</td>
<td>
<?php echo $form->labelEx(Tour::model(), 'streetNumber'); ?>
</td>
<td>
<?php echo $form->labelEx(Tour::model(), 'streetName'); ?>
</td>
<td>
<?php echo $form->labelEx(Tour::model(), 'suburb'); ?>
</td>
<td>
<?php echo $form->labelEx(Tour::model(), 'postCode'); ?>
</td>
<td>
<?php echo $form->labelEx(Tour::model(), 'tourName'); ?>
</td>
<td>
<?php echo $form->labelEx(Tour::model(), 'soldOut'); ?>
</td>
<td>
<?php echo 'Ticket URL'; ?>
</td>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="11">
<div class="add">
<?php
$this->widget('zii.widgets.jui.CJuiButton', array(
'buttonType' => 'link',
'name' => 'btnNew',
'value' => 'New',
'caption' => 'New',
'htmlOptions' => array('class' => 'btn medium'),
));
?>
<br><br>
</div>
<textarea class="template" rows="0" cols="0">
<tr class="templateContent">
<td>
<?php echo CHtml::textField('Tour[{0}][date]', '', array('style' => 'width:70px')); ?>
</td>
<td>
<?php echo CHtml::textField('Tour[{0}][time]', '', array('style' => 'width:50px')); ?>
</td>
<td>
<?php echo CHtml::textField('Tour[{0}][streetNumber]', '', array('style' => 'width:55px')); ?>
</td>
<td>
<?php echo CHtml::textField('Tour[{0}][streetName]', '', array('style' => 'width:80px')); ?>
</td>
<td>
<?php echo CHtml::textField('Tour[{0}][suburb]', '', array('style' => 'width:80px')); ?>
</td>
<td>
<?php echo CHtml::textField('Tour[{0}][postCode]', '', array('style' => 'width:50px')); ?>
</td>
<td>
<?php echo CHtml::textField('Tour[{0}][tourName]', '', array('style' => 'width:80px')); ?>
</td>
<td>
<?php echo CHtml::dropDownList('Tour[{0}][soldOut]', $select, array('0' => 'No', '1' => 'yes')); ?>
</td>
<td>
<?php echo CHtml::dropDownList('Tour[{0}][ticketTable_ticketId]', 'Tour', $list, array('empty' => 'Select')); ?>
</td>
<td>
<input type="hidden" class="rowIndex" value="{0}" />
<div class="remove">
<?php
$this->widget('zii.widgets.jui.CJuiButton', array(
'buttonType' => 'link',
'name' => 'btnRemove',
'value' => 'Remove',
'caption' => 'Remove',
'htmlOptions' => array('class' => 'btn medium'),
));
?>
</td>
</tr>
</textarea>
</td>
</tr>
</tfoot>
<tbody class="templateTarget">
<?php foreach ($this->tempTours as $i => $tour): ?>
<tr class="templateContent">
<td>
<?php echo $form->textField($tour, "[$i]date", array('style' => 'width:100%')); ?>
</td>
<td>
<?php echo $form->textField($tour, "[$i]time", array('style' => 'width:100%')); ?>
</td>
<td>
<?php echo $form->textField($tour, "[$i]streetNumber", array('style' => 'width:100%')); ?>
</td>
<td>
<?php echo $form->textField($tour, "[$i]streetName", array('style' => 'width:100%')); ?>
</td>
<td>
<?php echo $form->textField($tour, "[$i]suburb", array('style' => 'width:100%')); ?>
</td>
<td>
<?php echo $form->textField($tour, "[$i]postCode", array('style' => 'width:100%')); ?>
</td>
<td>
<?php echo $form->textField($tour, "[$i]tourName", array('style' => 'width:100%')); ?>
</td>
<td>
<!-- this needs to be yes or no -->
<?php echo $form->textField($tour, "[$i]soldOut", array('style' => 'width:100%')); ?>
</td>
<td>
<?php echo CHtml::dropDownList("[$i]ticketTable_ticketId", $tour, $list); ?>
</td>
<td>
<input type="hidden" class="rowIndex" value="<?php echo $i; ?>" />
<div class="remove">
<?php
$this->widget('zii.widgets.jui.CJuiButton', array(
'buttonType' => 'link',
'name' => 'btnRemove',
'value' => 'Remove',
'caption' => 'Remove',
'htmlOptions' => array('class' => 'btn medium'),
));
?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<div class="row buttons">
<?php
$this->widget('zii.widgets.jui.CJuiButton', array(
'buttonType' => 'submit',
'name' => 'btnSubmit',
'value' => 'Submit',
'caption' => 'Save',
'htmlOptions' => array('class' => 'btn medium'),
));
$this->widget('zii.widgets.jui.CJuiButton', array(
'buttonType' => 'link',
'name' => 'btnCancel',
'value' => 'Cancel',
'caption' => 'Cancel',
'htmlOptions' => array('class' => 'btn medium'),
'url' => 'index.php?r=band/default',
));
?>
</div>
</div><!--panel-->
</div><!--complex-->
<div class="action">
</div>
<?php $this->endWidget(); ?>
</div>
</div>
</div>
</div>