I am trying to pass the variable to view the page through the siteController, however when I try, I always get the error undefined Variable.
Here is my code in the siteController:
public function actionIndex(){
$Opere = Opere::find()->all();
return $this->render('index', [
'titolo' => $titolo, 'tecnica' => $tecnica, 'misura' => $misura, 'anno' => $anno,
'prezzo' => $prezzo, 'note' => $note, 'url_grandi' => $url_grandi]);
}
and here below is the code on the index page:
<?php foreach ($Opere as $o): //var_dump($o); ?>
<div class='grid-item'>
<img src='<?php $url_grandi;?>' class='grid-item-img'/>
<div class='grid-item-overlay'>
<div class='text'>
<div class='innerText'>
Titolo: <?php $titolo;?><br />
Tecnica: <?php $tecnica;?><br />
Misura: <?php $misura;?><br />
Anno: <?php $anno;?><br />
Prezzo: € <?php $prezzo;?><br />
Note: <?php $note;?>
</div>
</div>
</div>
</div>
<?php endforeach; ?>
Thank you in advance
I found the solution, I added the incorrect variable in my foreach loop. I was trying to call the variable $titolo, however, in my foreach loop I had specified that the variable $titolo as $o. This is why it did not recognize the variable $titolo.
The following below is the correct code string to output the db data:
<?= $o->titolo;?>
and not:
<?php echo $titolo;?>
hope this helps someone else with a similar problem.
Related
When I want to get the variable from the form the post action doesn't load .
This is my view:
<?php
$form = ActiveForm::begin();
?>
<div class="form-group">
<input type="text" name="username" placeholder="FullName">
<?= Html::a(Yii::t('app', 'Start'), ['start', 'link' => $model->link], ['type' => 'button','class' => 'btn btn-primary btn-round']) ?>
</div>
<?php ActiveForm::end(); ?>
This is my controller:
if ($model->load(Yii::$app->request->post())){
exit(var_dump('everything is ok'));
}else {
exit(var_dump('nothing is right'));
}
The result is 'nothing is right'.
Apart from using the anchor link instead of a submit button, you are not using model to create active input hence the field names are without model names or the standard array format that Yii accepts, you should pass empty string to the load method as second parameter which is formName like below
$model->load(Yii::$app->request->post(),'');
So your complete form should look like
<?php
$form = ActiveForm::begin(
[
'action' => 'start',
]
);
?>
<div class="form-group">
<input type="text" name="username" placeholder="FullName">
<?php echo Html::submitButton(Yii::t('app', 'Start'), ['class' => 'btn btn-primary btn-round']) ?>
</div>
<?php ActiveForm::end();?>
EDIT
and your controller code should look like below, mind the first check it needs to be there so that the code is run when you submit only not on page load
if (Yii::$app->request->isPost) { //this should be here before the rest of the code
if ($model->load(Yii::$app->request->post(), '')) {
exit(var_dump('everything is ok'));
} else {
exit(var_dump('nothing is right'));
}
}
This is because load() method looks for post data inside model name property and you are writing the input yourself instead of using the Yii method for forms.
So your post Yii::$app->request->post() returns:
array(
'username' => 'value of username',
)
And your $model->load looks for
array(
'modelName' => array(
'username' => 'value of username',
)
)
To make your post data too look like that you could do it the right way that is, delete your Input and use this method inside the form:
<?= $form->field($model, 'username')->textInput(['maxlength' => true]) ?>
Or the wrong way, modify your input and inside username use:
<input type="text" name="modelName[username]" placeholder="FullName">
Of course where I put username you must put your real model name.
Finally I find the solution
<?php
$form = ActiveForm::begin(
[
'action' => 'start',
]
);
?>
<div class="form-group">
<input type="text" name="username" placeholder="FullName">
<?= Html::a('submit', Url::to(['start', 'link' => $model->link]), ['data-method' => 'POST']) ?>
</div>
<?php ActiveForm::end();?>
thank you for all
I am able to send simple email to receiver.Now i have to be able to select a email format from existing different formats and this template must appear in my message box.i should also be able to change the content of the template that appears in message box before sending to receiver.How to do this?
My code for view form
<div class="form wide">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'mail-form',
'enableAjaxValidation'=>true,
'htmlOptions' => array('enctype' => 'multipart/form-data'), // ADD THIS
)); ?>
<div class="row col2">
<?php echo $form->labelEx($model,'email_from'); ?>
<?php echo $form->textField($model,'email_from',array('size'=>50,'maxlength'=>50,'readonly'=>'readonly')); ?>
<?php echo $form->error($model,'email_from'); ?>
</div>
<div class="row col2">
<?php echo $form->labelEx($model,'email_to'); ?>
<?php echo $form->textField($model,'email_to',array('size'=>50,'maxlength'=>50)); ?>
<?php echo $form->error($model,'email_to'); ?>
</div>
<div style="clear:both"></div>
<div class="row col2">
<?php echo $form->labelEx($model,'subject'); ?>
<?php echo $form->textField($model,'subject',array('size'=>60,'maxlength'=>250)); ?>
<?php echo $form->error($model,'subject'); ?>
</div>
<div style="clear:both"></div>
<div class="row col2">
<?php echo $form->labelEx($model,'message'); ?>
<?php echo $form->textArea($model,'message',array('style'=>'width: 680px; height: 300px;')); ?>
<?php echo $form->error($model,'message'); ?>
</div>
<div style="clear:both"></div>
<div class="row buttons">
<?php
echo CHtml::submitButton($model->isNewRecord ? 'Send' : 'Send',array('class' => 'btn'));
?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
Try below code:
public function actionContact() {
$model = new ContactForm();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$status = Yii::$app->mailer->compose()
->setTo($model->email_to)
->setFrom([$model->email_from => $model->email_from])
->setSubject($model->subject)
->setTextBody($model->message)
->send();
if ($status) {
Yii::$app->session->setFlash('success', 'Thank you for contacting us. We will respond to you as soon as possible.');
} else {
Yii::$app->session->setFlash('error', 'There was an error sending email.');
}
return $this->refresh();
} else {
return $this->render('contact', [
'model' => $model,
]);
}
}
You can also create a email_template directory under views folder and create template file in which you can set email template content.
you can use js or ajax to do the same.
1: Use jQuery:
I assume that you are using jQuery. So use ajax.
.......JS Cod........
/*I assume that you have your template data in an attribute like data-template-data=""*/
$('#select_template_id').chang(function(){
template=$(this).attr('data-template-data');
$('#template_edit_box').val(template);
});
I have one table where I am saving field name.
Now I want to use those fields in another model. So, How can I give name to that field?
e.g.
I have table named as Config with fields(id,key).
Data can be
1, Blog url
2, Site url
Now, I have 1 form where admin will add those value to database.
In Yii2 we create input field like
<?= $form->field($model, 'name')->textInput() ?>
But I want to create two textboxes with name blog url and site url.
So, How can I create it? What I have to write in place of name?
run this code on your command prompt
php composer.phar require --prefer-dist wbraganca/yii2-dynamicform "*"
after completion of above code.
add the following code in your form.
_form.php
<div class="panel panel-default">
<div class="panel-heading"><h4><i class="glyphicon glyphicon-envelope"></i> Addresses</h4></div>
<div class="panel-body">
<?php DynamicFormWidget::begin([
'widgetContainer' => 'dynamicform_wrapper', // required: only alphanumeric characters plus "_" [A-Za-z0-9_]
'widgetBody' => '.container-items', // required: css class selector
'widgetItem' => '.item', // required: css class
'limit' => 4, // the maximum times, an element can be cloned (default 999)
'min' => 1, // 0 or 1 (default 1)
'insertButton' => '.add-item', // css class
'deleteButton' => '.remove-item', // css class
'model' => $modelsAddress[0],
'formId' => 'dynamic-form',
'formFields' => [
'name',
],
]); ?>
<div class="container-items"><!-- widgetContainer -->
<?php foreach ($modelsAddress as $i => $modelAddress): ?>
<div class="item panel panel-default"><!-- widgetBody -->
<div class="panel-heading">
<h3 class="panel-title pull-left">Address</h3>
<div class="pull-right">
<button type="button" class="add-item btn btn-success btn-xs"><i class="glyphicon glyphicon-plus"></i></button>
<button type="button" class="remove-item btn btn-danger btn-xs"><i class="glyphicon glyphicon-minus"></i></button>
</div>
<div class="clearfix"></div>
</div>
<div class="panel-body">
<?php
// necessary for update action.
if (! $modelAddress->isNewRecord) {
echo Html::activeHiddenInput($modelAddress, "[{$i}]id");
}
?>
<?= $form->field($model, "[{$i}]name")->textInput(['maxlength' => true]) ?>
</div>
</div>
<?php endforeach; ?>
</div>
<?php DynamicFormWidget::end(); ?>
</div>
</div>
I have a form and I want to upload a file. here is my code:
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'show-form',
'enableAjaxValidation'=>false,
'htmlOptions' => array('enctype' => 'multipart/form-data'),
)); ?>
<fieldset>
<legend>DATI TECNICI</legend>
<div class="row">
<?php echo $form->labelEx($model,'tec_data_file'); ?>
<?php echo $form->fileField($model,'tec_data_file',array('size'=>45,'maxlength'=>45)); ?>
<?php echo $form->error($model,'tec_data_file'); ?>
</div>
</fieldset>
<?php $this->endWidget(); ?>
</div><!-- form -->
There was nothing added to the database after submitting, I did a bit of debuging with firebug and figured out that filefield generates a code like this:
<input id="ytShow_tec_data_file" type="hidden" name="Show[tec_data_file]" value="">
<input id="Show_tec_data_file" type="file" name="Show[tec_data_file]" maxlength="45" size="45">
and two data are sent by $_POST for tec_data_file (which is my file field in db). The first var is empty (I think it is related to first hidden input). and the second one contains my file. and when I'm assigning the variables to my model for saving:
$modelPhoto->attributes = $_POST['Photo'];
the tec_data_file gets an empty string! So nothing gets uploaded to my db. Anyone have an idea how to solve this? If you need more i
You need something like:
$model = new Photo;
$model->attributes = $_POST['Photo'];
$model->image = CUploadedFile::getInstance($model,'file');
where file is the name of the field.
I haven't tested this, it's from the documentation.
i am trying to make a dropdown , but i don't want i to be visible for now , here is the code.
<div class="row">
<?php echo $form->labelEx($model,'lang_id'); ?>
<?php echo $form->dropdownlist($model,'lang_id',CHtml::listData(Lang::model()->findAll(), 'id', 'name')); ?>
<?php echo $form->error($model,'lang_id'); ?>
</div>
How do i make this type = 'hidden' or something like that ?
In other words , i want to keep the field but i don't want it to be shown.
try this..
<div class='row' style='display:none'>
Also, you can define style attribute of your dropDownList.
public static string dropDownList(string $name, string $select, array $data, array $htmlOptions=array ( ))
You can try with:
<div class="row">
<?php echo $form->labelEx($model,'lang_id'); ?>
<?php echo $form->dropDownList($model,'lang_id',CHtml::listData(Lang::model()->findAll(), 'id', 'name'), array('style' => 'display: none'); ?>
<?php echo $form->error($model,'lang_id'); ?>
</div>