How to give hidden input value to dynamic form fields yii2 - yii2

I am confused with one problem from couple of days and i could not solve it. I have a form which has different classes and when user clicks on add button i want to generate couple of fields. I can generate the fields but i am not able to pass that particular class_id with those dynamic generated fields. I am using this widget And here is how i am generating the dynamic fields.
Only involved model is Registration form. Other than that i am just firing query and displaying the data.One class can have multiple Registrations
Class Registration
is id
name class_id
firstname
lastname
This is my view code
<?php if (!empty($data)) {
foreach($data as $event){
$modelsRegistration = [new Registration()];
DynamicFormWidget::begin([
'widgetContainer' => 'dynamicform_wrapper', // required: only alphanumeric characters plus "_" [A-Za-z0-9_]
'widgetBody' => '.container-items'.$model['id'], // required: css class selector
'widgetItem' => '.item'.$model['id'], // required: css class
'limit' => 4, // the maximum times, an element can be added (default 999)
'min' => 0, // 0 or 1 (default 1)
'insertButton' => '.add-item'.$model['id'], // css class
'deleteButton' => '.remove-item'.$model['id'], // css class
'model' => $modelsRegistration[0],
'formId' => 'registration-form',
'formFields' => [
'firstname',
'lastname',
],
]);
?>
<div class="container">
<div class="row">
<div class="col-lg-9 col-md-9 col-sm-12 col-xs-12">
<table class="table table-striped table-bordered">
<thead>
<tr>
<th class="col-lg-4 col-md-4 col-sm-4 col-xs-4">Class Name</th>
<th class="col-lg-2 col-md-2 col-sm-2 col-xs-2">Rounds</th>
</tr>
</thead>
<tbody>
<tr>
<td><?= $event['name'] ?></td>
<td><button type="button" id="<?= $event['id'] ?>" class="add-item<?=$model['id'] ?> btn btn-success btn-xs">Add Rounds <i
class="glyphicon glyphicon-plus"></i></button></td>
</tr>
</tbody>
</table>
<div class="row">
<div class="container-items<?=$model['id'] ?>"><!-- widgetBody -->
<?php foreach ($modelsRegistration as $i => $modelRegistration){
?>
<div class="item<?=$model['id'] ?>"><!-- widgetItem -->
<?php
// necessary for update action.
if (!$modelRegistration->isNewRecord) {
echo Html::activeHiddenInput($modelRegistration, "[{$i}]id");
}
?>
<div class="row">
<div class="col-sm-5">
<?= $form->field($modelRegistration, "[{$i}]firstname")->textInput(['maxlength' => true]) ?>
</div>
<div class="col-sm-5">
<?= $form->field($modelRegistration, "[{$i}]lastname")->textInput(['maxlength' => true]) ?>
<?= $form->field($modelRegistration, "[{$i}]class_id")->hiddenInput(['maxlength' => true])->label(false) ?>
</div>
<div class="col-sm-2">
<button type="button" class="remove-item<?=$model['id'] ?> btn btn-danger btn-xs"><i
class="glyphicon glyphicon-minus"></i></button>
</div>
</div>
<!-- .row -->
</div>
<?php } ?>
</div>
</div>
</div>
</div>
</div>
<?php
DynamicFormWidget::end();
} } ?>

If you have a view showing only people of one class (therefore, adding more people means they all are the same class) you can use in your dynamicform only the first/last name fields and add this id_class later in the controller just before saving it.
Example:
// load and validation of the model
// ...
foreach ($modelsRegistration as $modelRegistration) {
$modelRegistration->class_id = $myClassId;
if (! ($flag = $modelRegistration->save(false))) {
$transaction->rollBack();
break;
}
}
Now, if in your view you have people of all class divided by sections and each section have this add button, i strongly recommend you remove this add buttons from there and make a new section just for add people (with a select for choose a class).
Steps:
Remove the option insertButton in the DynamicFormWidget class in all those sections. And remove the html button also (won't work anymore).
Add the new section, something like this:
<?php
$newModelsRegistration = [new Registration()];
DynamicFormWidget::begin([
'widgetContainer' => 'dynamicform_wrapper_registration', // required: [A-z0-9_]
'widgetBody' => '.container-items-registration', // required: css class selector
'widgetItem' => '.item-registration', // required: css class
'limit' => 1, // the maximum times, an element can be added (default 999)
'min' => 0, // 0 or 1 (default 1)
'insertButton' => '.add-item-registration', // css class
'deleteButton' => '.remove-item-registration', // css class
'model' => $newModelsRegistration[0], //
'formId' => 'registration-form',
'formFields' => [
'firstname',
'lastname',
'class_id'
],
]);
?>
<div class="panel-body">
<button type="button" class="add-item-registration btn btn-success btn-sm pull-right margin-b-base">
<i class="glyphicon glyphicon-plus"></i> Add
</button>
<div class="clearfix"></div>
<div class="container-items-registration"><!-- widgetBody -->
<?php foreach ($newModelsRegistration as $i => $newModelRegistration): ?>
<div class="item-registration panel panel-default"><!-- widgetItem -->
<div class="panel-heading">
<h3 class="panel-title pull-left">Registration</h3>
<div class="pull-right">
<button type="button" class="remove-item-registration btn btn-danger btn-xs"><i class="glyphicon glyphicon-minus"></i></button>
</div>
<div class="clearfix"></div>
</div>
<div class="panel-body">
<div class="form-group row">
<div class="col-md-12">
<?= $form->field($newModelRegistration, "[{$i}]firstname")->textInput(['maxlength' => true]) ?>
<?= $form->field($newModelRegistration, "[{$i}]lastname")->textInput(['maxlength' => true]) ?>
<?= $form->field($newModelRegistration, "[{$i}]class_id")->dropDownList(
ArrayHelper::map(Class::find()->all(), 'id', 'name'),
['prompt' => 'Select']
) ?>
</div>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
<?php DynamicFormWidget::end(); ?>
Make sure the limit option is set to 1. So, will be easy to avoid people trying to add too much registrations in the same class (if i understood correctly each class have a limit amount of registrations). You can add a new rule in your Registration model and check if it already reached the limit of that class. Same thing for that find() method used in the dropDownList.
But if you still want to have all this add button in the same view, you can always use js to add that value for you.
Example:
// begin of form
// ...
<?= $form->field($modelRegistration, "[{$i}]class_id")->hiddenInput(['maxlength' => true, 'class' => 'form-control add-class-id', 'data-event' => $event['id']])->label(false) ?>
// end of form
<?php
$js = <<<JS
$('#registration-form').on('submit', function (e) {
$('.add-class-id').each(function() {
$(this).val($(this).attr('data-event'));
});
});
JS;
$this->registerJs($js);
?>
Let me know if any of this ideas works for you.

Related

Cannot get the Post data with specific term_id

I'm doing a nested tab by using bootstrap and so i want to query the post data with specific the term_id itself. please see my code what I'm doing wrong.
I'v get the result just the first one of the term_id but another term_id is not get the result.
I assume in my case the loop just working only one time.
<!-- Nav tabs -->
<ul class="nav nav-tabs" id="myTab-<?php echo $cat->slug?>" role="tablist">
<?php
$args = array('child_of' => $cat->term_id);
$categories = get_categories( $args );
//print_r($categories);
$ix = 1;
foreach($categories as $category) :?>
<li class="nav-item">
<a class="nav-link <?php echo $ix?> <?php if($ix==1){echo "active";}?>" id="tab-<?php echo $category->term_id?>" data-toggle="tab" href="#tab-<?php echo $category->term_id.$ix?>" role="tab" aria-controls="tab-<?php echo $category->term_id.$ix?>" aria-selected="<?php if($ix==1){echo "true";}else{echo "false";}?>"><?php echo $category->name;?></a>
</li>
<?php $ix++; endforeach ?>
</ul>
<div class="tab-content">
<?php
$ix = 1;
foreach($categories as $category) :?>
<div class="tab-pane <?php echo $ix?> <?php if($ix==1){echo "active";}?>" id="tab-<?php echo $category->term_id.$ix?>" role="tabpanel" aria-labelledby="tab-<?php echo $category->term_id.$ix?>">
<?php echo "term_ID: ". $category->term_id?>
<ul class="row child_of_cat">
<?php
$qr = new WP_Query( $args = array(
'post_type' => 'services',
'parent' => 1,
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => -1, // Limit: two products
//'post__not_in' => array( get_the_id() ), // Excluding current product
'tax_query' => array( array(
'taxonomy' => $taxonomy,
'field' => 'term_id', // can be 'term_id', 'slug' or 'name'
'terms' => $category->term_id,
), ),
));
?>
<?php
if ( $qr->have_posts() ):
while( $qr->have_posts() ):
$qr->the_post();
if(get_the_post_thumbnail($qr->post->ID) ==''){
$img = '<img class="img-fluid" src="https://via.placeholder.com/300/ddd/fff.png">';
} else{
$img = get_the_post_thumbnail($qr->post->ID);
}
echo '<li class="col-6 col-sm-3">
<div class="loop-box">';
if (has_term($term = 'best-seller', $taxonomy = 'collection', $post = $qr->post->ID)){
echo '<img class="conner-bage-bg" src="'.get_template_directory_uri().'/img/bage_new.png">';
}
echo ''.$img.'
<a href="'.get_permalink().'"><div class="description">
<p class="woocommerce-loop-product__title">'.$qr->post->post_title.'xx</p>
</div></a>
</div>
</li>';
endwhile;
//wp_reset_postdata();
rewind_posts();
endif; ?>
</ul>
</div><!-- tab-pane -->
<?php $ix++; endforeach ?>
</div>
I have already fix it by using get_posts().
enter link description here

Yii2: how to load different model base on user's preference

I have two model loaded when i user loads the page but only one of this model will take information base on user's preference, but my problem is when the user submit the form the two model are submitted and i get an error because the other model needs to take information before it can proceed too, i want to know how i can load just one of the model and ignore the other when base on which of the form the user filled
Contoller
public function actionSignup($mode)
{
//if($mode === 'personalAccount'){
$model = new SignupForm();
if ($model->load(Yii::$app->request->post())) {
if ($user = $model->signup()) {
if (Yii::$app->getUser()->login($user)) {
return $this->goHome();
}
}
}
// }else{
$model_business = new SignupFormbusiness();
if ($model_business->load(Yii::$app->request->post())) {
if ($user = $model_business->signup()) {
if (Yii::$app->getUser()->login($user)) {
return $this->goHome();
}
}
}
return $this->render('signup', [
'model' => $model,
'model_business' => $model_business,
]);
// }
}
view
<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
//if(isset($model_business))
$this->title = 'Signup';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="container" style="width: 100%;">
<div class="row" style="background: #fcfcfc;">
<div class="col-md-12 login-form-top">
</div>
<div class="col-lg-12 signup-form ">
<div class="signup-form-container">
<?php $form = ActiveForm::begin(['id' => 'login-form' ]);
$loginUrl = \Yii::$app->UrlManager->createUrl(['site/login']);
?>
<h1>Sign Up</h1>
<p style="text-align: center;">Please fill in this form to create an account.<br/>
Already a Member? Login
</p>
<div class="form-radio col-xs-6" style="padding:0;">
<label class="radio-container">Personal Account
<input type="radio" checked="checked" value="personal" name="acc-type" id="personal_radio">
<span class="radio-checkmark"></span>
</label>
</div>
<div class="form-radio col-xs-6" style="padding:0;">
<label class="radio-container">Business Account
<input type="radio" value="enterprise" name="acc-type" id="enterprise_radio">
<span class="radio-checkmark"></span>
</label>
</div>
<hr>
<?php if(isset($model) && !empty($model)): ?>
<div id="personal">
<div class="col-xs-6" style="padding-left:0; padding-right:5px;">
<?= $form->field($model, 'last_name', [
'template'=>'{input}<span class="asteric form-control-feedback">*</span>'
. ' {error}{hint}'
])->textInput(['placeholder'=> 'Surname'])->label(false) ?>
</div>
<div class="col-xs-6" style="padding-right:0; padding-left:5px;">
<?= $form->field($model, 'first_name', [
'template'=>'{input}<span class="asteric form-control-feedback">*</span>'
. ' {error}{hint}'
])->textInput(['placeholder'=> 'Given Name'])->label(false) ?>
</div>
<div class="col-xs-12 col-nopadding">
<?= $form->field($model, 'username',[
'template'=>'{input}<span class="asteric form-control-feedback">*</span>'
. ' {error}{hint}'
])->textInput(['placeholder'=> 'Username'])->label(false) ?>
</div>
<div class="col-md-12 col-nopadding">
<?= $form->field($model, 'email',[])->textInput(['placeholder'=> 'Email'])->label(false) ?>
</div>
<div class="col-md-12 col-nopadding">
<?= $form->field($model, 'phone1',[
'template'=>'{input}<span class="asteric form-control-feedback">*</span>'
. ' {error}{hint}'
])->textInput(['placeholder'=> 'Phone Number'])->label(false) ?>
</div>
<div class="col-md-12 col-nopadding">
<?= $form->field($model, 'password',[
'template'=>'{input}<span class="asteric form-control-feedback">*</span>'
. ' {error}{hint}'
])->passwordInput(['placeholder'=> 'Password'])->label(false) ?>
</div>
</div>
<?php endif;?>
<?php if(isset($model_business) && !empty($model_business)): ?>
<div id="enterprise">
<div class="col-xs-12" style="padding-right:0; padding-left:5px;">
<?= $form->field($model_business, 'business_name', [
'template'=>'{input}<span class="asteric form-control-feedback">*</span>'
. ' {error}{hint}'
])->textInput(['placeholder'=> 'Business Legal name'])->label(false) ?>
</div>
<div class="col-xs-12 col-nopadding">
<?= $form->field($model_business, 'username',[
'template'=>'{input}<span class="asteric form-control-feedback">*</span>'
. ' {error}{hint}'
])->textInput(['placeholder'=> 'Username'])->label(false) ?>
</div>
<div class="col-md-12 col-nopadding">
<?= $form->field($model_business, 'email',[])->textInput(['placeholder'=> 'Business email'])->label(false) ?>
</div>
<div class="col-md-12 col-nopadding">
<?= $form->field($model_business, 'phone1',[
'template'=>'{input}<span class="asteric form-control-feedback">*</span>'
. ' {error}{hint}'
])->textInput(['placeholder'=> 'Business Phone Number'])->label(false) ?>
</div>
<div class="col-md-12 col-nopadding">
<?= $form->field($model_business, 'password',[
'template'=>'{input}<span class="asteric form-control-feedback">*</span>'
. ' {error}{hint}'
])->passwordInput(['placeholder'=> 'Password'])->label(false) ?>
</div>
</div>
<?php endif;?>
<!--<input class="signup-input" type="password" placeholder="Repeat Password" name="psw-repeat" >-->
<div class="col-md-12 col-nopadding">
<p class="signup-condition">By creating an account you agree to our Privacy Policy.</p>
</div>
<div class="form-group">
<?= Html::submitButton('Sign Up', ['class' => 'btn', 'name' => 'signup-button']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
</div>
</div>
</div>
<?php
$script = <<< JS
$(document).ready(function(){
$(function (){
$("#enterprise_radio").on('click',function(){
$(this).prop('checked', true);
if($(this).is(':checked')){
$("#enterprise").show();
$("#personal").hide();
}
});
});
$(function (){
$("#personal_radio").on('click',function(){
$(this).prop('checked', true);
if($(this).is(':checked')){
$("#enterprise").hide();
$("#personal").show();
}
});
});
});
JS;
$this->registerJs($script);
?>
The two images above is a brief description of what i'm trying to achieve but the forms are loaded from different model and must be filled before it can be validate and i only need user to fill one of this form at a time which means the other will be empty and cannot validate which will trigger an error, is there any way to solve this problem or a better way to fix this.
i fill in the first form the presonal account and click submit, nothing happens, then i check on the business account form and got this error which means it also want me to fill out the form before it can proceed with the submition
You should either create a custom FormModel or create 2 separate forms,that would show up whenever the relevant choice/radio input is selected, what yo are doing is that the models are differen and the form is combined for the fields of both models which is odd and creating problems.
If you choose to create a single FormModel you should use Conditional Validation which provides when and whenClient inside the rules for the validation checks.
A quick suggestion would be to create 2 different forms like below as both are different models and you should assign the fields for the models to 2 different forms, rather than trying to merge all the fields in a single form and keeping the models separate
<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
//if(isset($model_business))
$this->title = 'Signup';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="container" style="width: 100%;">
<div class="row" style="background: #fcfcfc;">
<div class="col-md-12 login-form-top">
</div>
<div class="col-lg-12 signup-form ">
<div class="signup-form-container">
<?php
$loginUrl = \Yii::$app->UrlManager->createUrl(['site/login']);
?>
<h1>Sign Up</h1>
<p style="text-align: center;">Please fill in this form to create an account.<br/>
Already a Member? Login
</p>
<div class="form-radio col-xs-6" style="padding:0;">
<label class="radio-container">Personal Account
<input type="radio" checked="checked" value="personal" name="acc-type" id="personal_radio">
<span class="radio-checkmark"></span>
</label>
</div>
<div class="form-radio col-xs-6" style="padding:0;">
<label class="radio-container">Business Account
<input type="radio" value="enterprise" name="acc-type" id="enterprise_radio">
<span class="radio-checkmark"></span>
</label>
</div>
<hr>
<?php if (isset($model) && !empty($model)): ?>
<?php $form = ActiveForm::begin(['id' => 'signup-form']); ?>
<div id="personal">
<div class="col-xs-6" style="padding-left:0; padding-right:5px;">
<?=
$form->field($model, 'last_name', [
'template' => '{input}<span class="asteric form-control-feedback">*</span>'
. ' {error}{hint}'
])->textInput(['placeholder' => 'Surname'])->label(false)
?>
</div>
<div class="col-xs-6" style="padding-right:0; padding-left:5px;">
<?=
$form->field($model, 'first_name', [
'template' => '{input}<span class="asteric form-control-feedback">*</span>'
. ' {error}{hint}'
])->textInput(['placeholder' => 'Given Name'])->label(false)
?>
</div>
<div class="col-xs-12 col-nopadding">
<?=
$form->field($model, 'username', [
'template' => '{input}<span class="asteric form-control-feedback">*</span>'
. ' {error}{hint}'
])->textInput(['placeholder' => 'Username'])->label(false)
?>
</div>
<div class="col-md-12 col-nopadding">
<?= $form->field($model, 'email', [])->textInput(['placeholder' => 'Email'])->label(false) ?>
</div>
<div class="col-md-12 col-nopadding">
<?=
$form->field($model, 'phone1', [
'template' => '{input}<span class="asteric form-control-feedback">*</span>'
. ' {error}{hint}'
])->textInput(['placeholder' => 'Phone Number'])->label(false)
?>
</div>
<div class="col-md-12 col-nopadding">
<?=
$form->field($model, 'password', [
'template' => '{input}<span class="asteric form-control-feedback">*</span>{error}{hint}'
])->passwordInput(['placeholder' => 'Password'])->label(false)
?>
</div>
<div class="col-md-12 col-nopadding">
<p class="signup-condition">By creating an account you agree to our Privacy Policy.</p>
</div>
<div class="form-group">
<?= Html::submitButton('Sign Up', ['class' => 'btn', 'name' => 'signup-button']) ?>
</div>
</div>
<?php ActiveForm::end() ?>
<?php endif; ?>
<?php if (isset($model_business) && !empty($model_business)): ?>
<?php $form = ActiveForm::begin(['id' => 'signup-business']); ?>
<div id="enterprise">
<div class="col-xs-12" style="padding-right:0; padding-left:5px;">
<?=
$form->field($model_business, 'business_name', [
'template' => '{input}<span class="asteric form-control-feedback">*</span>'
. ' {error}{hint}'
])->textInput(['placeholder' => 'Business Legal name'])->label(false)
?>
</div>
<div class="col-xs-12 col-nopadding">
<?=
$form->field($model_business, 'username', [
'template' => '{input}<span class="asteric form-control-feedback">*</span>'
. ' {error}{hint}'
])->textInput(['placeholder' => 'Username'])->label(false)
?>
</div>
<div class="col-md-12 col-nopadding">
<?= $form->field($model_business, 'email', [])->textInput(['placeholder' => 'Business email'])->label(false) ?>
</div>
<div class="col-md-12 col-nopadding">
<?=
$form->field($model_business, 'phone1', [
'template' => '{input}<span class="asteric form-control-feedback">*</span>'
. ' {error}{hint}'
])->textInput(['placeholder' => 'Business Phone Number'])->label(false)
?>
</div>
<div class="col-md-12 col-nopadding">
<?=
$form->field($model_business, 'password', [
'template' => '{input}<span class="asteric form-control-feedback">*</span>'
. ' {error}{hint}'
])->passwordInput(['placeholder' => 'Password'])->label(false)
?>
</div>
</div>
<div class="col-md-12 col-nopadding">
<p class="signup-condition">By creating an account you agree to our Privacy Policy.</p>
</div>
<div class="form-group">
<?= Html::submitButton('Sign Up', ['class' => 'btn', 'name' => 'signup-button']) ?>
</div>
<?php ActiveForm::end(); ?>
<?php endif; ?>
</div>
</div>
</div>
</div>
<?php
$script = <<< JS
$(document).ready(function(){
$(function (){
$("#enterprise_radio").on('click',function(){
$(this).prop('checked', true);
if($(this).is(':checked')){
$("#enterprise").show();
$("#personal").hide();
}
});
});
$(function (){
$("#personal_radio").on('click',function(){
$(this).prop('checked', true);
if($(this).is(':checked')){
$("#enterprise").hide();
$("#personal").show();
}
});
});
});
JS;
$this->registerJs($script);
?>
Controller Code
function actionSignup($mode)
{
$model = new SignupForm();
$model_business = new SignupFormbusiness();
if (Yii::$aap->request->isPost) {
$accountType = Yii::$app->request->post("acc-type");
if ($accountType == 'personal' && $model->load(Yii::$app->request->post())) {
if ($user = $model->signup()) {
if (Yii::$app->getUser()->login($user)) {
return $this->goHome();
}
}
}
if ($accountType == 'enterprise' && $model_business->load(Yii::$app->request->post())) {
if ($user = $model_business->signup()) {
if (Yii::$app->getUser()->login($user)) {
return $this->goHome();
}
}
}
}
return $this->render('signup', [
'model' => $model,
'model_business' => $model_business,
]);
}
Hoping that by default validation is activated for the two forms, the following solution will apply
First, give the two forms different ids say first_form_id and secod_form_id as follows:
//For the first form:
<?php $form = ActiveForm::begin(['id' => 'first_form_id']); ?>
//For the second form:
<?php $form = ActiveForm::begin(['id' => 'second_form_id']); ?>
Then you can modify the last part of your view file as below:
<?php
$script = <<< JS
$(document).ready(function(){
$(function (){
$("#enterprise_radio").on('click',function(){
$(this).prop('checked', true);
if($(this).is(':checked')){
$("#enterprise").show();
$("#personal").hide();
// Here we are disabling the validation for the first form;
$('#first_form_id').yiiActiveForm('validate', false);
//Activate the validation of the second form just incase it was disabled
$('#second_form_id').yiiActiveForm('validate', true);
}
});
});
$(function (){
$("#personal_radio").on('click',function(){
$(this).prop('checked', true);
if($(this).is(':checked')){
$("#enterprise").hide();
$("#personal").show();
//Activate the validation of the first form
$('#first_form_id').yiiActiveForm('validate', true);
//De-activate the validation of the first form just incase it was disabled
$('#second_form_id').yiiActiveForm('validate', false);
}
});
});
});
JS;
$this->registerJs($script);
?>

How to Add Select2 to row Dynamically in Yii2

I'm using Yii2 for my developments and I'm trying to Add Rows to table using plus button.
I'm able To add new row from that. And i need to append Krajee Select2 to my appending row. is that possible? how Can I Do that?
Following is my code use for append.
$('#main_tbl_body').append(
'<tr class='+row_class+' id="tbl_rw_appent_'+new_count+'">\n\
<td><a onclick ="removeMoreCommentField(this.id)" id="a_link_remove_'+new_count+'"><img id="image_remove_'+new_count+'" src="images/minus.png" style="width: 20px;height: 20px;cursor: pointer;" ></a></td>\n\
<td><div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">'+' <?php
//echo '<label class="control-label">Part Code</label>';
$data = ArrayHelper::map(Mproduct::find()
// where(['P_model' => $model->relatedToVehicle->V_model])
->all(), 'idProduct', 'P_description');
//$items = DealerHelper::getRemainItems();
echo Select2::widget([
'name' => 'part_description_1',
'data' => $data,
'options' => [
// 'class'=>'textBox mobviewmargin',
'placeholder' => 'Select Product',
// 'multiple' => true
],
'pluginOptions' => [
'closeOnSelect' => true,
'tags' => false,
'multiple' => false,
'allowClear' => false,
],
]);
?>'+'</div></td>\n\
<td></td>\n\ \n\\n\
<td></td>\n\ \n\\n\
<td></td>\n\ \n\
<td></td>\n\ \n\
<td><a onclick ="addMoreCommentField()" id="a_link_add_'+new_count+'"><img id="image_add_'+new_count+'" src="images/plus.png" style="width: 20px;height: 20px;cursor: pointer;" ></a></td>\n\
</tr>'
);
Please Help me

codeigniter form submission using ajax and display return data in table

I submitting one form in view page and I want to, when I click on submit button the form data need to be saved in a database table and that data need to be shown in the table without refresh page.
Here is my view code:
<div class="col-md-12">
<div class="row">
<div>
<table class="table table-striped table-hover table-responsive">
<thead>
<tr class="">
<th>Director</th>
<th>Partner</th>
<th>Duration</th>
<th>Comments</th>
</tr>
</thead>
<tbody id="hodm_results">
</tbody>
</table>
</div>
</div>
</div>
<?php
$attributes = array('class' => 'form-horizontal','id'=>'hodm_comments');
echo form_open('', $attributes);
?>
<div class="row">
<div class="col-md-12">
<div class="col-sm-3">
<div class="input-group">
<span>Director</span>
<?php echo form_input(['class'=>'form-control autofocus','id'=>'director','name'=>'director','value'=>set_value('director',$wip->director)]); ?>
</div>
</div>
<div class="col-sm-3">
<div class="input-group">
<span>Partner</span>
<?php echo form_input(['class'=>'form-control autofocus','id'=>'partner','name'=>'partner','value'=>set_value('partner',$wip->partner)]); ?>
</div>
</div>
<div class="col-sm-3">
<div class="input-group">
<span>Duration</span>
<?php echo form_input(['class'=>'form-control autofocus','id'=>'duration','name'=>'duration']); ?>
</div>
</div>
<div class="col-sm-3">
<div class="input-group">
<span>Comments</span>
<?php echo form_textarea(['class'=>'form-control autofocus','id'=>'comments','name'=>'comments','rows'=>'3']); ?>
<input type="hidden" name="id_hidden" value="<?php echo $wip->id; ?>">
</div>
</div>
</div>
</div>
<input class="btn btn-primary" type="submit" name="submit" value="submit">
<?php
echo form_close();
?>
</section>
<!--main content end-->
</section>
Here is my JQuery code:
<script type='text/javascript' language='javascript'>
$('#hodm_comments').submit(function (event) {
$.ajax({
url:"<?php echo base_url();?>digital/dashboard/insert_hodm_comments",
type: 'POST',
dataType: 'JSON',
success:function (data) {
$('#hodm_results').html(data);
}
});
event.preventDefault();
});
</script>
Here is my controller code:
public function insert_hodm_comments(){
/* Checking the all validation of Hodm Comment form form*/
$this->form_validation->set_rules('director', 'Name of Director', 'required');
$this->form_validation->set_rules('partner', 'Partner', 'required');
$this->form_validation->set_rules('duration', 'No Of Hours', 'required');
$this->form_validation->set_rules('comments', 'Comments of the task', 'required');
if ($this->form_validation->run()) {
/* Taking the data from form*/
$partner = $this->input->post('partner');
$director = $this->input->post('director');
$duration = $this->input->post('duration');
$comments = $this->input->post('comments');
$id = $this->input->post('id_hidden');
$data = array(
'partner' =>$partner,
'director' =>$director,
'duration' =>$duration,
'comments' =>$comments,
'hodm_id' =>$id
);
$add=$this->pojo->add_hodm_comments($data);
/* Display Success message if data inserted successfully in database*/
if($add){
$result_html = '';
$result_set = $this->pojo->get_hodm_comments();
foreach($result_set as $result) {
$result_html .= '
<tr>
<td>' . $result->director . '</td>
<td>' . $result->partner . '</td>
<td>' . $result->duratrion . '</td>
<td>' . $result->comments . '</td>
</tr>';
}
echo json_encode($result_html);
//$this->session->set_flashdata('hodm_form',"All HODM Data Inserted Successfully.");
//$this->session->set_flashdata('hodm_form_class','alert-success');
}else{
/* Displaying the error message*/
$this->session->set_flashdata('hodm_form',"failed to add, Please Try again");
$this->session->set_flashdata('hodm_form_class','alert-danger');
}
return redirect('digital/dashboard/wip_hodm_comments_section');
} else {
$this->load->view('digital/hodm/dashboard_hodm_work/wip_hodm_comments');
}
}
Here is Model:
public function add_hodm_comments($data){
$this->db->insert('hodm_wip_comments', $data);
return TRUE;
}
public function get_hodm_comments(){
$this->db->select('*');
$this->db->from('hodm_wip_comments');
$query=$this->db->get();
return $result=$query->result();
}
Please help me to find the solution I stuck in this code.
Thank you
Just change your ajax part to below
<script type='text/javascript' language='javascript'>
$('#hodm_comments').submit(function (event) {
$.ajax({
url:"<?php echo base_url();?>digital/dashboard/insert_hodm_comments",
type: 'POST',
data : $('#hodm_comments').serialize(),
success:function (data) {
$('#hodm_results').html(data);
}
});
event.preventDefault();
});
</script>
and in your controller change this echo json_encode($result_html); to echo $result_html;
this works perfect in my side with your code :)
What you can do simply is to echo response from within your controller function, get the return data of ajax call and add it into a div or as tr in to an existing table.

vue.js not removing row

I am busy making a dynamic form using Vue.js and at the moment, I appear to have everything correct except the remove function. It's all correctly configured as far as I see, yet the console in my browser displays the error "this.rows.$remove is not a function".
Does anyone know the solution for this, or can help me out finding the solution? Thanks in advance.
======================================
Here is the HTML for the page where the form is displayed:
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>M06 Property-create</title>
<!-- Including nessecary javascript sources -->
<script src="https://unpkg.com/vue#next/dist/vue.js"></script>
</head>
{!! Form::open(array('route' => 'property.store')) !!}
#include('properties.form')
{!! Form::close() !!}
Return
<script> //This script handles the adding / removal of label/text fields upon clicking the add label / remove label button
var app = new Vue({
el: "#app",
data: {
rows: [
{name: ""}
]
},
methods: {
addRow: function () {
this.rows.push({name: ""});
},
removeRow: function (row) {
console.log(row);
this.rows.$remove(row);
}
}
});
</script>
</body>
</html>
======================================
Here is the HTML / Blade for the form itself that is included:
{!! Form::label('label', Lang::get('misc.label')) !!}
{!! Form::text('label', null, ['class' => 'form-control', 'required']) !!}
<br>
{!! Form::label('internal_name', Lang::get('misc.internal_name')) !!}
{!! Form::text('internal_name', null, ['class' => 'form-control', 'required']) !!}
<br>
{!! Form::label('field_type_id', Lang::get('misc.fieldtype_name')) !!}
{!! Form::select('field_type_id', $fieldTypes) !!}
<div class="dropdown box">
<div class="multi-field-wrapper">
<div class="multi-fields">
<div class="multi-field">
<div id="app">
<table class="table">
<thead>
<button type="button" class="button btn-primary" #click="addRow">Add label</button>
<tr>
<td><strong>Label</strong></td>
<td></td>
</tr>
</thead>
<tbody>
<tr v-for="row in rows">
<td><input type="text" v-model="row.name"></td>
<td><button type="button" class="button btn-primary" #click="removeRow(row)">Remove</button></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<br>
{!! Form::label('property_group_id', Lang::get('misc.group_name')) !!}
{!! Form::select('property_group_id', $groups) !!}
<br>
{!! Form::label('description', Lang::get('misc.description')) !!}
{!! Form::textarea('description', null, ['class' => 'form-control', 'required']) !!}
<br>
<br>
{!! Form::submit(Lang::get('misc.save'), ['class' => 'btn btn-success', 'id' => 'btn-save']) !!}
It looks like the $remove method has been deprecated in vue 2.0, so I assume you are using that. You will need to use splice instead:
HTML:
<tr v-for="(row, index) in rows">
<td><input type="text" v-model="row.name"></td>
<td><button type="button" class="button btn-primary" #click="removeRow(index)">Remove</button></td>
</tr>
Javascript:
removeRow: function (index) {
console.log(index);
this.rows.splice(index,1);
}
You can see the fiddle here: https://jsfiddle.net/rLes3nww/
Its really irritating to refer to the guide and then to find out that it is deprecated.
Anyway I found this as reference of which one is deprecated or not.
Reference to deprecated Vue stuff