Enter the user currently logged on to form - php

I have this code
enter <div class="formularz">
<div class="row">
<?php $form = ActiveForm::begin(['id' => 'contact-form']); ?>
<div class="col-md-6">
<?php echo $form->field($model, 'name')->label('Imię i nazwisko')?>
</div>
<div class="col-md-6">
<?php echo $form->field($model, 'email')->label('Adres email') ?>
</div>
<div class="col-md-6">
<?php echo $form->field($model, 'phone')->label('Telefon') ?>
</div> here
How can i get in this inputs values of actualy logged user for example i refresh page and i want to have filled inputs with some data.

I take you're using Yii. Here is the documentation of what you're asking:
http://www.yiiframework.com/doc-2.0/yii-bootstrap-activeform.html#field%28%29-detail
Just add any attribute you want inside an array and plug it in as the third parameter of the field method.
<?php echo $form->field($model, 'email', ['inputOptions' => ['value' => 'my Value']])->label('Adres email') ?>

Related

button with nested ul clases yii framework

I would like to this, but with yii framework:
<span class="btn default btn-file">
<span class="fileinput-new">Select image</span>
<span class="fileinput-exists">Change</span>
<input type="file" name="..."></input>
</span>
trying to do this way but doesn't work;
echo Button::widget([
'label' => 'Select Image',
'options' => ['class' => 'btn default btn-file'],
'options' => ['class' => 'fileinput-new'],
]);
I'm very newbie in yii framework and I've been spending some time trying to do this but without success. Anyhelp would be very apreciated
The make this input, cou can use either an ActiveForm (if you have a model) like this:
<?php
use yii\widgets\ActiveForm;
?>
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?>
<?= $form->field($model, 'attribute')->fileInput() ?>
<div class="form-group">
<?= Html::submitButton('Submit') ?>
</div>
<?php ActiveForm::end() ?>
Or the Html helper (if you don't want a model):
<?php
use yii\helpers\Html;
?>
<?php $form = Html::beginForm(['options' => ['enctype' => 'multipart/form-data']]); ?>
<?= Html::fileInput('...') ?>
<div class="form-group">
<?= Html::submitButton('Submit') ?>
</div>
<?php Html::endForm() ?>
I believe you won't have problem adapting the style after reading the docs. Both have the label method that allows you to edit the label as you want. Let me know if i wasn't clear on something.
try this :
<?= Html::a('label', ['/controller/action'], ['class'=>'btn btn-primary']) ?>
hope it will works as you want
If you want to use file field in the yii2. Create a form and place your field in the form.
<?php
use yii\widgets\ActiveForm;
?>
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?>
<?= $form->field($modelobj, 'fieldname')->fileInput() ?>
<?= Html::submitButton('Submit')?>
<?php ActiveForm::end() ?>

Data from a form don't register in a database (Yii framework)

I have view:
...
<div class="row">
<?php echo $form->labelEx($model,'content'); ?>
<?php $this->widget('application.extensions.ckeditor.CKEditor',array(
'model'=>$model,
'attribute'=>'content',
'language'=>'en',
'editorTemplate'=>'full',));
?>
<?php echo $form->error($model,'content'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'status'); ?>
<?php echo $form->dropDownList($model,'status', array('0' => 'Not published', '1' => 'Published')); ?>
<?php echo $form->error($model,'status'); ?>
</div>
<div class="row">
<?php
echo $form->labelEx($model,'category_id');
echo CHtml::dropDownList('category_id','', Category::allCategory(),
array(
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('subcategory/dynamicSubCategories'), //url to call.
//Style: CController::createUrl('currentController/methodToCall')
'update'=>'#subcategory_id', //selector to update
//'data'=>'js:javascript statement'
//leave out the data key to pass all form values through
)));
echo $form->error($model,'category_id');
?>
</div>
<div class="row">
<?php
echo $form->labelEx($model,'subcategory_id');
echo CHtml::dropDownList('subcategory_id','', array());
echo $form->error($model,'subcategory_id');
?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
...
Data from the fields 'category_id' and 'subcategory_id' don't write in a database. All other fields successfully register. Prompt as me to tie this field to a form and to solve the matter?
Use this method for dropDownLists
echo CHtml::activeDropDownList($model, 'category_id',Category::allCategory()
....
Or better use form method
echo $form->dropDownList($model, 'category_id', Category::allCategory()
....
this is because the data that is saved, should be part of the models array data, so change your new field name to reflect MODELNAME[ATTRIBUTENAME]
echo CHtml::dropDownList('MODELNAME[category_id]', ...
or simply change to activeDropdownList

Yii Ajax Update depending dropdown selection

I have dropdown list where there are five or six items (here items are services). I used here ajax request which updates the id “package_id” from the URL 'url'=>$this->createUrl('servicePackage'), once I select any service item. Till now, I have done successfully. But I want that ajax should use different URL and update different id i.e. “registrationid” if I select 4th number item, otherwise it should update “package_id”.
Is it possible?
Is it possible?
The form is:
<div class="form-group">
<?php echo $form->labelEx($model,'service_id', array('class' => 'control-label col-lg-4')); ?>
<div class="col-lg-8">
<?php echo $form->dropDownList($model,'service_id',CHtml::listData(Service::model()->findAll(),'id', 'name'),
array(
'class' => 'form-control',
'prompt'=>'Select a Service',
'ajax' => array('type'=>'POST',
'url'=>$this->createUrl('servicePackage'),
'update'=>'#'.CHtml::activeId($model, 'package_id'), // ajax updates package_id, but I want ajax update registration_id if I select item no 4
'data'=>array('service_id'=>'js:this.value'),
)));
?>
</div>
<?php echo $form->error($model,'service_id'); ?>
</div>
<div class="form-group" id="packageid">
<?php echo $form->labelEx($model,'package_id', array('class' => 'control-label col-lg-4')); ?>
<div class="col-lg-8">
<?php
echo $form->dropDownList($model,'package_id',$model->getServicePackage($model->service_id),array(
'class' => 'form-control','prompt'=>'Select a Package',
'ajax' =>
array('type'=>'POST',
'url'=>$this->createUrl('packagePrice'), //url to call.
'update'=>'#price', //selector to update
'data'=>array('package_id'=>'js:this.value'),
)));
?>
</div>
<?php echo $form->error($model,'package_id'); ?>
</div>
<div class="form-group" id="registrationid">
<?php echo $form->labelEx($model,'registration_id', array('class' => 'control-label col-lg-4')); ?>
<div class="col-lg-8">
<?php
$registrants = Registration::model()->findAll();
$data = CHtml::listData($registrants,'id', function($registrants) {
return CHtml::encode($registrants->name.' ('.$registrants->id.')');
});
echo $form->dropDownList($model,'registration_id',$data,
array(
'class' => 'required form-control chzn-select',
'prompt'=>'Select a Registrant',
'ajax' => array('type'=>'POST',
'url'=>$this->createUrl('Order'), //url to call.
'update'=>'#'.CHtml::activeId($model, 'order_id'), //selector to update
'data'=>array('registration_id'=>'js:this.value'),
)
));
?>
</div>
<?php echo $form->error($model,'registration_id'); ?>
</div>

how to insert multi records in yii session using ajaxSubmitButton

I have been developing an application in Yii framework. At this point, I fall in an issue that is, I have an Order form where I select a registrant (all registrants come from database) from a dropdown, an item (all product items come from database) from dropdown and a input textbox where I type the quantity. There is a "ajaxSubmitButton" button to send all these values to the controller "actionCart" using Ajax. After receiving all values in the controller, I want to put all values in session variable. When I add another new item, the session values are being replaced what I want to hold all newly added items into the session variable. In this circumstance, what should I do. Please help me. I am giving my code snippets below:
in form:
<div class="form">
<?php
$form=$this->beginWidget('CActiveForm', array(
'id'=>'order-form',
'enableAjaxValidation'=>false,
'htmlOptions'=>array('class'=>'form-horizontal' , 'enctype'=>'multipart/form-data', ),
)); ?>
<div class="alert alert-info" xmlns="http://www.w3.org/1999/html">
<p class="note">Fields with <strong><span class="required">*</span></strong> are required.</p>
</div>
<?php echo $form->errorSummary($model); ?>
<div class="form-group">
<?php echo $form->labelEx($model,'registration_id', array('class' => 'control-label col-lg-4')); ?>
<div class="col-lg-8">
<?php
$data = CHtml::listData(Registration::model()->findAll(),'id', 'name');
echo $form->dropDownList($model,'registration_id',$data,array('class' => 'form-control chzn-select','prompt'=>'Select a Registrant'));
?>
</div>
<?php echo $form->error($model,'registration_id'); ?>
</div>
<div class="form-group">
<?php echo $form->labelEx($model,'item', array('class' => 'control-label col-lg-4')); ?>
<div class="col-lg-8">
<?php
$data = CHtml::listData(Products::model()->findAll(),'id', 'name');
echo $form->dropDownList($model,'item',$data, array('class'=>'form-control chzn-select' , 'id'=>'item', 'prompt'=>'Select an Item')); ?>
<?php
?>
</div>
<?php echo $form->error($model,'item'); ?>
</div>
<div class="form-group">
<?php echo $form->labelEx($model,'quantity', array('class' => 'control-label col-lg-4')); ?>
<div class="col-lg-2">
<?php
echo $form->textField($model,'quantity',array('class' => 'form-control','size'=>60,'maxlength'=>11));
?>
</div>
<?php echo $form->error($model,'quantity'); ?>
</div>
<div class="form-group">
<div class="col-lg-8 pull-right">
<?php
echo CHtml::ajaxSubmitButton('Add to Cart',Yii::app()->createUrl('admin/order/cart'),
array(
'type'=>'POST',
'update'=>'#cartResult',
),
array('class'=>'btn btn-primary btn-sm',));
?>
</div>
</div>
<div class="form-group">
<div id="cartResult" class="col-lg-12">
</div>
</div>
<?php $this->endWidget(); ?>
</div>
In controller:
public function actionCart()
{
if(isset($_POST["Order"])){
$item = $_POST["Order"];
$registration_id = $item["registration_id"];
$productId = $item["item"];
$quantity = $item["quantity"];
$quantity = $item["quantity"]=='' ? 1 : $item["quantity"];
$productInfo = Products::model()->findByPk(array('id'=>$productId));
$totalPrice = $productInfo->price * $quantity;
$session = Yii::app()->session;
$session['cart'] = array("product_id" => "$productId" , "product_name" => "$productInfo->name", "quantity" => "$quantity","price" => "$productInfo->price");
}
}

Auto complete fields of a previous values

I am doing a small application in Yii Framework for that my database is something like this
=== Invoices ===
id (PK)
customer_id
invoice_title
order_no
invoice_issue_date
due_date
description
=== Customers ===
id (PK)
email_address
customer_name
address
city
state
postal_code
description
I have rendered the Customer model in Invoice model so that I can enter all the values for both models in a single Invoice form.But there is one problem,let us assume that I have a customer name xyz which I had saved before.Now when I am going to again fill the Customer name with xyz,it should show all the fields of both models like invoice_title,order_no,invoice_issue_date,due_date,description,email_address,customer_name,address etc. in that input fields of the form so that I don't have to re-enter all the fields again.So how this can be achive in Yii framework.Any help and suggestions will be highly appreciable.More clarification on codes that I have done can be shared if needed.
Please help me out.I am totally stuck here.
To do this as everyone has already mentioned you need ajax, and some javascript. The logic is something like this:
When a value is selected in the dropdown for customer name, trigger an ajax call to retrieve the information about that user. This can be easily done with the ajax option, which is available as an additional htmlOption for some html element helpers in CHtml, as part of clientChange.
echo $form->dropDownList($model,'customer_name',CHtml::listData(Customers::model()->findAll(),'id','customer_name'),
array(// htmlOptions
'ajax'=>array(// special htmlOption through clientChange, for ajax
'type'=>'GET',
'url'=>$this->createUrl('controllername/customerdetails'),// action that will generate the data
'data'=>'js:"id="+$(this).val()',// this is the data that we are sending to the action in the controller
'dataType'=>'json',// type of data we expect back from the server
'success'=>'js:updateFields'// a javascript function that will execute when the request completes successfully
)
)
);
The documentation for the above options for ajax can be seen in jquery's ajax documentation.
Then in the server side find the particular customer, and send a response to the browser. Example:
// in the controllername code an action that will return the values as json
public function actionCustomerdetails($id){
$var=Customers::model()->findByPk($id);
echo CJSON::encode($var);
}
When you receive the server response populate the respective fields. This can be done in the success function callback for ajax, in the above code it was updateFields:
Yii::app()->clientScript->registerScript('update','
function updateFields(data, textStatus, jqXHR){
// select each input field by id, and update its value
$("#Customers_postal_code").val(data.postal_code);
$("#Customers_city").val(data.city);
$("#Customers_address").val(data.address);
// similarly update the fields for the other inputs
}
');
Notes:
Your customer can have many invoices, so the question will be which invoice to select given a customer name. That's something you'll have to handle, i think my answer has enough code to get you going.
To know the input field ids, you can simply check the generated html.
You could do this in two stages, so that:
When the view is initially displayed, the customer is asked for their email address or customer_name.
The Controller Action that the form is submitted to then retrieves data from the Customer model for the submitted email address or customer_name (I'll use email_address in my example below). Once retrieved, you can display your Single Invoice Form View with the data pre-populated for the customer if available.
This concept could then be implemented as follows:
<?php
// file: controllers/InvoiceController.php
class InvoiceController extends CController
{
// ... other controller functions
public function actionCreate($step = null)
{
$invoice = new Invoice;
$customer = new Customer;
# Form has been submitted:
if ( isset($_POST['Customer']) )
{
# The submitted form was Step 1:
if ( $step == 1 )
{
# make sure the submitted email address is valid
$customer->setAttributes($_POST['Customer']);
if ( $customer->validate(array('email_address')) )
{
# retrieve the customer by email_address
$customer = Customer::model()->findByAttributes(array('email_address' => $_POST['Customer']['email_address']));
}
$this->render('createstep2', array('invoice' => $invoice, 'customer' => $customer));
}
# The submitted form was Step 2:
elseif ( $step == 2 )
{
$income->setAttributes($_POST['Invoice']);
$customer->setAttributes($_POST['Customer']);
# save the data
if ( $customer->save() )
{
$invoice->customer_id = $customer->id;
if ( $invoice->save() )
{
$this->redirect(array('view', 'id' => $invoice->id));
}
}
# display any errors
$this->render('createstep2', array('invoice' => $invoice, 'customer' => $customer));
}
}
$this->render('createstep1', array('invoice' => $invoice, 'customer' => $customer));
}
// ... other controller functions
}
?>
You could split that to two separate Controller Actions if you wish.
For Step 1 View, you could then have the following:
<!-- file: views/invoice/createstep1.php -->
<h1>Create Invoice: Step 1</h1>
<div class="form">
<?php
$form = $this->beginWidget('CActiveForm', array(
'id'=>'invoice-form',
'enableAjaxValidation'=>false,
'action'=>array('invoice/create','step' => 1)
));
?>
<?php echo $form->errorSummary($customer); ?>
<div class="row">
<?php echo $form->labelEx($customer,'email_address'); ?>
<?php echo $form->textField($customer,'email_address', array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($customer,'email_address'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Next'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
Step 2 view, you could then look like what you already have. Maybe something like:
<!-- file: views/invoice/createstep2.php -->
<h1>Create Invoice: Step 2</h1>
<div class="form">
<?php
$form = $this->beginWidget('CActiveForm', array(
'id'=>'invoice-form',
'enableAjaxValidation'=>false,
'action'=>array('invoice/create','step' => 2)
));
?>
<?php echo $form->errorSummary($invoce); ?>
<?php echo $form->errorSummary($customer); ?>
<h2>Customer Details</h2>
<div class="row">
<?php echo $form->labelEx($customer,'email_address'); ?>
<?php echo $form->textField($customer,'email_address', array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($customer,'email_address'); ?>
</div>
<!-- If the customer already exists, these field should be pre-populated: -->
<div class="row">
<?php echo $form->labelEx($customer,'customer_name'); ?>
<?php echo $form->textField($customer,'customer_name', array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($customer,'customer_name'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($customer,'address'); ?>
<?php echo $form->textField($customer,'address', array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($customer,'address'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($customer,'city'); ?>
<?php echo $form->textField($customer,'city', array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($customer,'city'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($customer,'state'); ?>
<?php echo $form->textField($customer,'state', array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($customer,'state'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($customer,'postal_code'); ?>
<?php echo $form->textField($customer,'postal_code', array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($customer,'postal_code'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($customer,'description'); ?>
<?php echo $form->textField($customer,'description', array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($customer,'description'); ?>
</div>
<h2>Order Details</h2>
<div class="row">
<?php echo $form->labelEx($invoice,'invoice_title'); ?>
<?php echo $form->textField($invoice,'invoice_title', array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($invoice,'invoice_title'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($invoice,'order_no'); ?>
<?php echo $form->textField($invoice,'order_no', array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($invoice,'order_no'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($invoice,'invoice_issue_date'); ?>
<?php $form->widget('zii.widgets.jui.CJuiDatePicker', array(
'model' => $invoice,
'attribute' => 'invoice_issue_date',
'value' => $invoice->invoice_issue_date,
'options' => array(
'showButtonPanel' => false,
'changeYear' => true,
'dateFormat' => 'yy-mm-dd',
),
)); ?>
<?php echo $form->error($invoice,'invoice_issue_date'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($invoice,'due_date'); ?>
<?php $form->widget('zii.widgets.jui.CJuiDatePicker', array(
'model' => $invoice,
'attribute' => 'due_date',
'value' => $invoice->due_date,
'options' => array(
'showButtonPanel' => false,
'changeYear' => true,
'dateFormat' => 'yy-mm-dd',
),
)); ?>
<?php echo $form->error($invoice,'due_date'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($invoice,'description'); ?>
<?php echo $form->textField($invoice,'description', array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($invoice,'description'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Create'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
Have you checked the Yii Autocomplete widget? And you wouldn't have to worry about AJAX implementation. It does it for you.
Yii Framework: CJui AutoComplete
A more customized autocomplete solution in this link.
Yii Framework: custom-autocomplete-display-and-value-submission

Categories