Cannot use modal in panel in yii2 - yii2

I want to use panel in yii2 project, so I download helpers from kartik-v. In the panel I want to make a button open a popup window or modal, but there is a problem. The modal/popup window working only in the first panel, but when I click button in the second panel, third panel, etc.. nothing happen. What's the problem?
This is the code:
<?php
foreach ($dataProvider->models as $myKandidat)
{
echo '<div class="col-md-4">'
. Html::panel([
'body' => '<div class="panel-body ">'
. '<div class="row">'
. '<div class="col-md-6"><center>'
. Html::img(Yii::getAlias('#web') . '/uploads/' . "{$myKandidat->foto}", ['width' => '129px', 'height' => '150px'])
. '</center></div>'
. '<div class="col-md-6">'
. '<p>' . "{$myKandidat->nama}" . '<b>--' . "{$myKandidat->noInduk}" . '</b><br />From ' . "{$myKandidat->prodi}" .'</p>'
. '<p class="JustifyFull small"><b>Motivation: </b>' . substr("{$myKandidat->motivasi}", 0, 100)
. '... <br />'
. Html::button('Testing', ['value' => Url::to('index.php?r=kandidat/view&id=' . "{$myKandidat->idKandidat}"), 'class' => 'btn btn-success', 'id' => 'modalButton'])
. '</p>'
. '</div>'
. '</div>'
. '</div>',
'footer'=> '<center>Panel Footer</center>',
'footerTitle' => true,
Html::TYPE_DEFAULT,
])
. '</div>';
}
Modal::begin([
'header' => 'Testing',
'id' => 'modal',
'size' => 'modal-lg',
]);
echo "<div id='modalContent'></div>";
Modal::end();
?>
This is the main.js
$(function(){
// Get the click of the button
$('#modalButton').click(function(){
$('#modal').modal('show')
.find('#modalContent')
.load($(this).attr('value'));
});
});
And this is my controller:
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
Thanks.

You assign an id to a button,
but when you use a loop, your id is repeating
id should be unique or you can use class for showing your modal
<?php
foreach ($dataProvider->models as $myKandidat)
{
echo '<div class="col-md-4">'
. Html::panel([
'body' => '<div class="panel-body ">'
. '<div class="row">'
. '<div class="col-md-6"><center>'
. Html::img(Yii::getAlias('#web') . '/uploads/' . "{$myKandidat->foto}", ['width' => '129px', 'height' => '150px'])
. '</center></div>'
. '<div class="col-md-6">'
. '<p>' . "{$myKandidat->nama}" . '<b>--' . "{$myKandidat->noInduk}" . '</b><br />From ' . "{$myKandidat->prodi}" .'</p>'
. '<p class="JustifyFull small"><b>Motivation: </b>' . substr("{$myKandidat->motivasi}", 0, 100)
. '... <br />'
. Html::button('Testing', ['value' => Url::to('index.php?r=kandidat/view&id=' . "{$myKandidat->idKandidat}"), 'class' => 'btn btn-success modalButton'])
. '</p>'
. '</div>'
. '</div>'
. '</div>',
'footer'=> '<center>Panel Footer</center>',
'footerTitle' => true,
Html::TYPE_DEFAULT,
])
. '</div>';
}
Modal::begin([
'header' => 'Testing',
'id' => 'modal',
'size' => 'modal-lg',
]);
echo "<div id='modalContent'></div>";
Modal::end();
?>
in main.js
$(function(){
// Get the click of the button
$('.modalButton').click(function(){
$('#modal').modal('show')
.find('#modalContent')
.load($(this).attr('value'));
});
});

Related

My custom post type in WordPress is not showing content through short-code

function diwp_services_custom_post_type()
{
$labels = array(
'name' => 'Services',
'singular_name' => 'Service',
'add_new' => 'Add Service',
'add_new_item' => 'Enter Service Details',
'all_items' => 'All Services',
'featured_image' => 'Add Service Image',
'set_featured_image' => 'Set Service Image',
'remove_featured_image' => 'Remove Service Image'
);
// Set Options for this custom post type;
$args = array(
'public' => true,
'label' => 'Services',
'labels' => $labels,
'description' => 'Services is a collection of services and their info',
'menu_icon' => 'dashicons-hammer',
'supports' => array('title', 'editor', 'thumbnail'),
'capability_type' => 'page',
);
register_post_type('services', $args);
}
add_action('init', 'diwp_services_custom_post_type');
// >> Create Shortcode to Display Services Post Types
function diwp_create_shortcode_services_post_type()
{
$args = array(
'post_type' => 'services',
'posts_per_page' => '10',
'publish_status' => 'published',
);
$query = new WP_Query($args);
if ($query->have_posts()) :
while ($query->have_posts()) :
$query->the_post();
$result .= '<div class="card">';
$result .= '<div class="card-block block-1">';
$result .= ' <h3 class="card-title">' . get_the_title() . '</div>';
$result .= ' <p class="card-text">' . get_the_content() . '</div>';
$result .= '</div>';
$result .= '</div>';
endwhile;
wp_reset_postdata();
endif;
return $result;
}
add_shortcode('services-list', 'diwp_create_shortcode_services_post_type');
// shortcode code ends here
I visited below link and implemented same as that but find no results
DIVEIN WP
Here is the screenshot as I am working on localhost
Any help regarding this will be appreciatable.
Thanks
First, you must fetch permalink from Setting >> permalink, and click Save changes
Seconds, Now you must add do_shortcode on the position you need to show all post from custom post type like :
echo do_shortcode('[services-list]');
I test your code in my end and work good https://abukotsh.me/wp/services/test-service/
Scroll down and you see test services

yii2 select2 asset bundle not loading css when called from ajax

I am using kartik select2 library. I have the modal which is loading in the page from ajax with renderAjax. The modal has the select2 dropdown.
The problem is it load the js but do not load the css files of select2.
echo $form->field($assign_model, 'applicant_id')->widget(Select2::classname(), [
'data' => $applicant,
'attribute' => 'applicant_id',
'options' => ['placeholder' => 'Select an applicant', 'multiple' => 'multiple', 'style' => "width:100%"],
'pluginEvents' => [
"select2:selecting" => "function() { "
. "no_position = $('body').data('no_position');"
. "if(no_position>= " . $model->no_of_persons . "){alert('You can select only " . $model->no_of_persons . " applicant(s)');return false;} }",
"select2:select" => "function() { "
. "no_position = $('body').data('no_position');"
. "$('body').data('no_position',++no_position);}",
"select2:unselect" => "function() { "
. "no_position = $('body').data('no_position');"
. "$('body').data('no_position',--no_position);}",
],
'pluginOptions' => [
'allowClear' => true,
]
]);

checkboxList use arrayHalper and get values yii2

This is my form checkboxList with arrayhalper:
<div class="form-group">
<?= $form->field($model, 'reg_id')->checkboxList(
arrayHelper::Map(Riders::find()->where(['user_id' => Yii::$app->user->identity->id])->all(),'rider_id',
function($model) { return **$model->rider_firstname . " (" . $model->cagoriesCategory->category_name . ")"**; }),
['class' => 'checkbox-inline', 'id' => 'person' ]), ?>
</div>
The label its work correctly, example: Jilly (MasterA). But I want to get value difference with label, example like this Jilly:MasterA. How to give values like this?
You need to change array value syntax. like as,
<div class="form-group">
<?= $form->field($model, 'reg_id')->checkboxList(
arrayHelper::Map(Riders::find()->where(['user_id' => Yii::$app->user->identity->id])->all(), function($model) { return $model->rider_firstname . " : " . $model->cagoriesCategory->category_name; },
function($model) { return $model->rider_firstname . " (" . $model->cagoriesCategory->category_name . ")"; }),
['class' => 'checkbox-inline', 'id' => 'checkbox' ]); ?>
</div>
Its work what i want, thank to #GAMITG
<div class="form-group">
<?= $form->field($model, 'reg_id')->checkboxList(
arrayHelper::Map(Riders::find()->where(['user_id' => Yii::$app->user->identity->id])->all(), function($model) { return $model->rider_firstname . ":" . $model->cagoriesCategory->category_name; },
function($model) { return $model->rider_firstname . " (" . $model->cagoriesCategory->category_name . ")"; }),
['item' => function($index, $label, $name, $checked, $value) {
$return = '<label>';
$return .= '<input type="checkbox" name="' . $name . '" value="' . $value . '" class="checkbox-inline" id="person1">';
$return .= '<span>' . ucwords($label) . '</span>';
$return .= '</label>';
return $return;
} ]); ?>
</div>
then javascript code to cek it:
$('[id="person1"]:checked').each(function() {
var arr = $(this).val();
alert(arr);
});

How to wrap submenu in wordpress wp_nav function

I am new to wordpress would be great help if anyone can help me out, i have designed a website using bootstrap framework, for making it CMS i am using wordpress. i need to get menus dynamically from wordpress which i have dine using the following code
<?php wp_nav_menu(array('menu_class' => 'nav nav-justified','container_class' => 'menu_bac')); ?>
but problem i am stuck with is to get sub menus dynamically, i have been advised to use navwalker , but no idea how to add it to my own code.
can someone please help me out
You need to put your code in you function.php file this file located in your theme folder. Implement below code for extend Walker_Nav_Menu class and put your custom code based on your requirement.
class Custom_Menu extends Walker_Nav_Menu {
public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
//print_r($args);
$children = get_posts(array('post_type' => 'nav_menu_item', 'nopaging' => true, 'numberposts' => 1, 'meta_key' => '_menu_item_menu_item_parent', 'meta_value' => $item->ID));
if (empty($children)) {
$output .= $indent . '<li' . $id . $class_names .'>';
} else {
$output .= $indent . '<li class="dropdown">';
}
$atts = array();
$atts['title'] = ! empty( $item->attr_title ) ? $item->attr_title : '';
$atts['target'] = ! empty( $item->target ) ? $item->target : '';
$atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : '';
$atts['href'] = ! empty( $item->url ) ? $item->url : '';
$atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args );
$attributes = '';
foreach ( $atts as $attr => $value ) {
if ( ! empty( $value ) ) {
$value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
$attributes .= ' ' . $attr . '="' . $value . '"';
}
}
if ($children) {
$item_output = $args->before;
$item_output .= '<a class="dropdown-toggle js-activated" data-toggle="dropdown" href="#">';
$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
$item_output .= '<b class="caret"></b>';
$item_output .= '</a>';
$item_output .= $args->after;
} else {
$item_output = $args->before;
$item_output .= '<a'. $attributes .'>';
/** This filter is documented in wp-includes/post-template.php */
$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
$item_output .= '</a>';
$item_output .= $args->after;
}
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}
public function start_lvl( &$output, $depth = 0, $args = array() ) {
$indent = str_repeat("\t", $depth);
$output .= "\n$indent<ul class=\"dropdown-menu\">\n";
}
}
below code put in your header.php file
$defaults = array(
'theme_location' => 'primary',
'menu' => '',
'container' => 'div',
'container_class' => 'navbar-collapse collapse',
'container_id' => '',
'menu_class' => 'nav navbar-nav navbar-right',
'menu_id' => '',
'echo' => true,
'fallback_cb' => 'wp_page_menu',
'before' => '',
'after' => '',
'link_before' => '',
'link_after' => '',
'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>',
'depth' => 0,
'walker' => new Custom_Menu
);
wp_nav_menu($defaults);
Hey you can use this code for your bootstrap menu in wordpress
PHP CODE:-include this code in your header.php file
<div class="navbar-collapse collapse no-padding-lr">
<?php
$site_defaults = array(
'theme_location' => 'primary',
'container' => 'div',
'container_class' => '',
'container_id' => '',
'menu_class' => '',
'menu_id' => '',
'echo' => true,
'fallback_cb' => 'wp_page_menu',
'before' => '',
'after' => '',
'link_before' => '',
'link_after' => '',
'items_wrap' => '<ul class="site-menu">%3$s</ul>',
'depth' => 0,
'walker' => '');
wp_nav_menu($site_defaults);
?>
</div>

My google captcha code is not working properly

i want captcha code security in my signup page but when i fill the form with wrong captcha code and submit form. problem is view two page in my signup page. and fill this form again with correct captcha but they same problem again and show incorrect captcha.
CONTROLLER
public function user_signup($data = NULL){
$this->load->library('form_validation');
$this->recaptcha->recaptcha_check_answer($_SERVER['REMOTE_ADDR'],$this->input->post('recaptcha_challenge_field'),$this->input->post('recaptcha_response_field'));
$this->form_validation->set_rules('Username', 'Username', 'required|trim|is_unique[users.Username]');
$this->form_validation->set_rules('First_name', 'First Name', 'required|trim');
$this->form_validation->set_rules('Last_name', 'Last Name', 'required|trim');
$this->form_validation->set_rules('Email', 'Email', 'required|trim|valid_email');
$this->form_validation->set_rules('Password', 'Password', 'required|trim|md5');
$this->form_validation->set_rules('Conf_password', 'Confirm Password', 'required|trim|matches[Password]');
$this->form_validation->set_message('is_unique', 'Username already exists.');
if($this->form_validation->run() && $this->recaptcha->getIsValid()){
//Call to recaptcha to get the data validation set within the class.
$form_data = array();
$form_data['Username'] = $this->input->post('Username');
$form_data['First_name'] = $this->input->post('First_name');
$form_data['Last_name'] = $this->input->post('Last_name');
$form_data['Email'] = $this->input->post('Email');
$form_data['Password'] = $this->input->post('Password');
$form_data['Conf_password'] = $this->input->post('Conf_password');
$this->load->model('User');
$this->User->sign_up($form_data);
$data['msg']= "Account Created Sucessfuly";
}else{
if(!$this->recaptcha->getIsValid()){
$this->session->set_flashdata('error','incorrect captcha');
}
}
redirect("Users/signup", $data);
}
MODEL
public function sign_up($form_data){
$this->db->insert('users', $form_data);
}
VIEW
<?php
//$val = NULL;
if(isset($results->id)){
$val = "Update";
$action = "Users/user_update/".$results->id;
}else{
$val= "Register";
$action = "Users/user_signup";
}; ?>
<?php echo form_open($action); ?>
<?php echo form_input(array('name'=>'Username', 'class'=>'input-xlarge', 'type'=>'text', 'placeholder'=>'Username', 'required' => 'required', 'value'=>$u)); ?>
<?php echo form_error('Username', '<div class="alert alert-error">', '</div>'); ?>
<?php echo form_input(array('name'=>'First_name', 'class'=>'input-xlarge', 'type'=>'text', 'placeholder'=>'First Name', 'required' => 'required', 'value'=>$f)); ?>
<?php echo form_error('First_name', '<div class="alert alert-error">', '</div>'); ?>
<?php echo form_input(array('name'=>'Last_name', 'class'=>'input-xlarge', 'type'=>'text', 'placeholder'=>'Last Name', 'required' => 'required', 'value'=>$l)); ?>
<?php echo form_error('Last_name', '<div class="alert alert-error">', '</div>'); ?>
<?php echo form_input(array('name'=>'Email', 'class'=>'input-xlarge', 'type'=>'text', 'placeholder'=>'Email', 'required' => 'required', 'value'=>$e)); ?>
<?php echo form_error('Email', '<div class="alert alert-error">', '</div>'); ?>
<?php echo form_input(array('name'=>'Password', 'class'=>'input-xlarge', 'type'=>'password', 'placeholder'=>'Password')); ?>
<?php echo form_error('Password', '<div class="alert alert-error">', '</div>'); ?>
<?php echo form_input(array('name'=>'Conf_password', 'class'=>'input-xlarge', 'type'=>'password', 'placeholder'=>'Confirm Password')); ?>
<?php echo form_error('Conf_password', '<div class="alert alert-error">', '</div>'); ?>
<?php if(!isset($results->id)){echo $recaptcha_html;} ?>
<?php if ($this->session->flashdata('error') !== FALSE) { echo '<div class="alert alert-error">'.$this->session->flashdata('error').'</div>'; } ?>
<div class="login-actions">
<span><input type="checkbox"> <span class="remember">I have read & agree</span></span>
<span><?php echo form_submit(array('value'=>$val, 'class'=>'btn btn-large btn-warning pull-right', 'type'=>'submit')); ?></span>
</div>
<?php echo form_close(); ?>
Put this code after the if(!$this->recaptcha->getIsValid()){ and echo messages...
if($this->input->post('recaptcha_response_field') =="" ){
$this->session->set_flashdata('error','fill up this code');
}else{
$this->session->set_flashdata('error','incorrect captcha');
}
if your show the message "account create successfully". use the set flash message
read this link properly
http://ellislab.com/codeigniter/user-guide/libraries/sessions.html
How to display error messages in CodeIgniter
First you create a signup function in Controller and and view default signup page. as like
$this->load->view('User/User_sign_up');