How to set checked for all selected checkboxes - select

I have many checkboxes. I want when user clicks submit button and he hasn't filled all required fields, these checkboxes that he has checked, to be checked. My code is the following, but when form submit, only last checkbox is cheked.
function teachers_show(yes, no){
$(".toggle, .all_teachers_show, .student, .school, .teacher_school, .teacher, .class, .teacher_class").hide();
if (no)
$('.all_teachers_show').show();
else
$('.all_teachers_show').hide();
$(":radio").prop('checked',false);
$(yes).prop('checked',true);
}
<?php
echo validation_errors();
echo "<div class='container' id='register_container'>";
echo form_open('home/register');
echo "<table border = '0' >";
echo "<tr><td><label> Username:* </label></td><td>";
$data=array(
'name' => 'username',
'class' => form_error('username') ? 'error' : '',
'value' => set_value('username')
);
echo form_input($data);
echo "</td></tr>";
echo "<tr><td><label> Password:* </label></td><td>";
$data=array(
'name' => 'password',
'class' => form_error('password') ? 'error' : ''
);
echo form_password($data);
echo "<tr><td><label> Choose role:* </label> </td><td>";
$selected_role = $this->input->post('role_id'); ?>
<input type="radio" name="role_id" id="radio1" onclick="showHide(this, true)" value="1"
<?php echo '1' == $selected_role ? 'checked="checked"' :
'' ?>/>
<?php echo " Role 1"; ?>
<input type="radio" name="role_id" id="radio2" onclick="showHide(this, true)" value="2"
<?php echo '2' == $selected_role ? 'checked="checked"' :
'' ?>/>
<?php echo " Role 2 "; ?>
<input type="radio" name="role_id" id="radio5" onclick="teachers_show(this, true)" value="5"
<?php echo '5' == $selected_role ? 'checked="checked"' :
'' ?>/>
<?php echo " Role 3 ";
echo "</td></tr>";
<?php
echo "<tr class='all_teachers_show' style='display:none;'><td><label> Teachers:* </label></td><td>";
?>
<table border='0'>
<tr>
<?php
$ind = 0;
foreach ($all_teachers_show as $row) {
$ind++;
?>
<td>
<?php $selected_teachers = $this->input->post('all_teachers_show[]'); echo $selected_teachers; ?>
<input type="checkbox" id='all_teachers_show' <?php echo set_checkbox('all_teachers_show',$row->user_id); ?> name="all_teachers_show[]"
value="<?= $row->user_id ?>" <?php if ( isset($selected_teachers[$row->user_id] ))
echo 'checked="checked"'; ?>><?= $row->first_name . ' ' . $row->last_name ?> <td>
<?php
if($ind % 3 == 0)
echo '</tr> <tr>';
} ?>
</table>
<?php
echo "</td></tr>";
echo "</div>";
echo "</table><br/>";
$data=array(
"name" => 'mysubmit',
'class' => 'btn btn-success ',
'id' => 'reg',
'value' => 'Register'
);
echo form_submit($data);
?>
</form>
That's my whole code. These chechboxes are for Role 3 - radio button with id='radio5'.
How could it be my if: <?php echo $selected_teachers == $row->user_id ? 'checked="checked"' :
'' ?>
$selected_teachers - it returns array and I compare it with $row->user_id
How could be done that?

try this:
<?php
foreach ($all_teachers_show as $row)
{
$userId = $row->user_id;
$first_name = $row->first_name;
$last_name = $row->last_name;
$teacherSelected = ( isset($_POST[$userId]) ) ? true : false;
?>
<td>
<input
type="checkbox"
class='all_teachers_show'
name="<?php echo $userId; ?>"
<?php
if ( $teacherSelected )
echo 'checked="checked"';
?>
value="<?php echo $userId; ?>"
>
<?php echo $first_name . ' ' . $last_name ?>
<td>
<?php
}

Related

Warning: Illegal string offset 'tag'

I am getting this warning on the below line, any help?
<label class="inputLabel"<?php echo ($field['tag']) ? ' for="'.$field['field']['tag'].'"': ''; ?>><?php echo $field['title']; ?></label> **strong text**
Below is the section of the code.
<div class="discountForm<?php echo $selectionStyle; ?> discount<?php echo $box; ?>">
<fieldset class="discount">
<legend><?php echo $selection[$i]['module']; ?></legend>
<?php echo $selection[$i]['redeem_instructions']; ?>
<div class="gvBal larger"><?php echo $selection[$i]['checkbox']; ?></div>
<div class="gvBal">
<?php foreach ($selection[$i]['fields'] as $field) { ?>
<label class="inputLabel"<?php echo ($field['tag']) ? ' for="'.$field['field']['tag'].'"': ''; ?>><?php echo $field['title']; ?></label>
<?php echo $field['field']; ?>
<?php } ?>
<?php if ( ($selection[$i]['module'] != MODULE_ORDER_TOTAL_INSURANCE_TITLE) && ($selection[$i]['module'] != MODULE_ORDER_TOTAL_SC_TITLE) ) { ?>
<div class="buttonRow"><?php echo zen_image(zen_output_string($template->get_template_dir(BUTTON_IMAGE_UPDATE, DIR_WS_TEMPLATE, $current_page_base, 'buttons/' . $_SESSION['language'] . '/') . BUTTON_IMAGE_UPDATE), BUTTON_UPDATE_ALT, '', '', 'onclick="updateForm();"'); ?></div>
<?php } ?>
</div>
</fieldset>
You're trying to access a string using an index that is a string.
You are treating $field as an array while it is actually a string. Check your code and what is the $selection[$i]['fields'] output.

Codeigniter redirect to empty form

I have the follow form in Codeigniter:
The controller:
public function item($alias = NULL){
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Edit menu';
$data['menu_item'] = $this->menu_model->get_menu($alias);
$data['articole'] = $this->menu_model->get_articole();
$data['menuactive'] = $this->menu_model->get_menuactiv();
$data['errors'] = $this->form_validation->error_array();
$this->form_validation->set_rules('position','Position','required');
$this->form_validation->set_rules('position','Position','numeric');
$this->form_validation->set_rules('name','Name','required');
if ($this->form_validation->run() === FALSE) {
$data['name'] = $data['menu_item']['name'];
$this->load->view('templates/header', $data);
$this->load->view('templates/youarehere', $data);
$this->load->view('templates/menu', $data);
$this->load->view('templates/admin', $data);
$this->load->view('menu/item', $data);
$this->load->view('templates/footer');
}
else {
$this->menu_model->update_menu();
redirect('menu');
}
}
The item view is:
<?php echo validation_errors(); ?>
<?php echo form_open('menu/item'); ?>
<?php echo form_label('ID ', 'id'); ?>
<?php echo form_input('id', $menu_item['id'], 'readonly'); ?><br><br>
<?php echo form_label('Name ', 'name'); ?>
<?php echo form_input('name', $menu_item['name']); ?><br><br>
<?php echo form_label('Position ', 'position'); ?>
<?php echo form_input('position', $menu_item['position']); ?><br><br>
<?php foreach($articole as $articole_item):
$articol1[] = $articole_item['id'] . ' ' . $articole_item['title'];
endforeach; ?>
<?php echo form_label('Associated article ', 'associated_article'); ?>
<?php echo form_dropdown('associated_article', $articol1, $menu_item['articol_asociat']); ?><br><br>
<?php echo form_label('Menu activ ', 'activ'); ?>
<?php echo form_checkbox('activ', '1', TRUE); ?><br><br>
<input type="submit" name="submit" value="Save menu"/>
</form>
In this view I edited menu items. Everything works fine when when everything is right. When I introduce something wrong in a field, like string into "position" field, the form redirect to item view, but with empty fields and with the error message. I want to keep what is entered in field and the error message.
What is wrong with my code?
Change your form :
<?php echo form_label('ID ', 'id'); ?>
<?php echo form_input('id', set_value('id'), 'readonly'); ?><br><br>
<?php echo form_label('Name ', 'name'); ?>
<?php echo form_input('name', set_value('name')); ?><br><br>
<?php echo form_label('Position ', 'position'); ?>
<?php echo form_input('position', set_value('position')); ?>
set_value() replace your old values

Can't display date in wp_query

I used <?php echo get_the_date( 'd.m.Y' ); ?> to display the date on my other pages, but I have dried the_time()l, the_date() etc. and none of these seem to work to display the date in my post.
I used wp_query to display the posts, but I can't get the date to show. Does anyone have a solution?
<?php
$args = array(
'posts_per_page' => '-1',
'post_type' => 'post',
'post_status' => 'publish',
'category__in' => $quicksand_categories
);
$query = new WP_Query( $args );
foreach ($query->posts as $item) {
$categories = wp_get_post_categories($item->ID);
?>
<li id="item" class="item" data-id="id-<?php echo $item->ID ?>" data-type="<?php foreach ($categories as $c) { echo $c . ' ';}?>" >
<div class="article_info">
<h3>
<?php if(get_option('titles') == 'yes') { ?>
<a href="<?php echo get_permalink($item->ID); ?>">
<?php echo get_the_title($item->ID); ?>
</a>
<?php } ?>
/ <?php $cat_id = $c; $cat_name = get_cat_name( $cat_id ); echo $cat_name;?>
</h3>
<span class="post_date"><?php echo get_the_date( 'd.m.Y' ); ?></span>
</div>
<?php if (get_option('featured') == 'yes') { ?>
<a href="<?php echo get_permalink($item->ID); ?>">
<?php echo get_the_post_thumbnail($item->ID,'full'); ?></a>
<?php } ?>
<br />
<p><?php $post = get_post($item->ID); echo $post->post_excerpt; ?></p>
<a href="<?php echo get_permalink($item->ID); ?>">
<img src="<?php bloginfo('template_url');?>/images/viemore_photos.png" alt="View More photos"/>
</a>
</li>
<?php } ?>

Codeigniter validate form is not working

I have the same implementation for my login for and is working, but on my contact form it is not, and I have no error message just re routed to the home page.
the weird thing is if in my controller i implement the index function like this:
public function index() {
$this->myprocess();
}
and my view using
<?php
$attributes = array('class' => 'form-contact', 'id' => 'myform2');
echo form_open('contact',$attributes);
?>
instead of calling my function myprocess all the validations work but no use for a form with out my header footers etc.
bellow that is how i want my code to work i hope yall can help me.
my controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class contact extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('contact_m');
}
public function index()
{
$data = array('title' => 'Contact', 'main_content' => 'contact_v');
$this->load->view('template', $data);
}
public function myprocess(){
$this->form_validation->set_rules('dfname', 'Name', 'required|trim|xss_clean|max_length[40]');
$this->form_validation->set_rules('dfemail', 'Email', 'required|trim|xss_clean|valid_email|max_length[50]|callback__verifyemail');
$this->form_validation->set_rules('dfmsg', 'dfmsg', 'trim|xss_clean');
$this->form_validation->set_error_delimiters('<br /><span class="error">', '</span>');
if ($this->form_validation->run() == FALSE) // validation hasn't been passed
{
$this->load->view('contact_v');
}
else
{
$form_data = array(
'dfname' => set_value('dfname'),
'dfemail' => set_value('dfemail'),
'dfmsg' => set_value('dfmsg')
);
if ($this->contact_m->SaveForm($form_data) == TRUE)
{
redirect('contact/success');
}
else
{
echo 'An error occurred saving your information. Please try again later';
}
}
}
function success()
{
echo 'this form has been successfully submitted with all validation being passed. All messages or logic here. Please note
sessions have not been used and would need to be added in to suit your app';
}
public function verifyemail(){
$name = $this->input->post('dfName');
$pass = $this->input->post('dfemail');
if($this->contact_m->email_exist($name,$pass)){
if ($this->session->userdata('site_lang') == 'portuguese')
{
$this->form_validation->set_message('_verifyuser','Usuario Inexistente!');
}else{
$this->form_validation->set_message('_verifyuser','User Not Found!');
}
$this->index();
return false;
}else{
return true;
}
}
}
?>
my view:
<div class="container">
<div class="row">
<div class=" col-md-4 col-md-offset-4">
<?php echo br(2)?>
<div class="account-wall drop-shadow">
<?php
$title = $this->my_library->my_title($this->session->userdata('site_lang'),FORM_CONTACT);
echo '<h1 class="text-center login-title">'. $title. '</h1>';
echo br(1)
?>
<?php
$attributes = array('class' => 'form-contact', 'id' => 'myform2');
echo form_open('contact/myprocess',$attributes);
?>
<p>
<label for="dfname">Name</span></label>
<?php echo form_error('dfname'); ?>
<input id="dfname" type="text" class="form-control" placeholder="Name" name="dfname" maxlength="40" value="<?php echo set_value('dfname'); ?>" autofocus />
<?php echo br(1)?>
</p>
<p>
<label for="dfemail">Email</span></label>
<?php echo form_error('dfemail'); ?>
<input id="dfemail" type="text" class="form-control" placeholder="Email" name="dfemail" maxlength="50" value="<?php echo set_value('dfemail'); ?>" />
<?php echo br(1)?>
</p>
<p>
<label for="dfmsg">Message</label>
<?php echo form_error('dfmsg'); ?>
<?php echo form_textarea( array( 'name' => 'dfmsg', 'rows' => '4', 'cols' => '43', 'value' => set_value('dfmsg') ) )?>
<?php echo br(1)?>
</p>
<p>
<?php echo form_submit( 'submit', 'Submit'); ?>
</p>
<?php form_close();?>
</div>
</div>
</div>
</div>
<?php echo br(5)?>
your callback function for validation need an underscore for prefix name :
public function _verifyemail()
and you need to call the process function :
public function index()
{
$data = array('title' => 'Contact', 'main_content' => 'contact_v');
if($this->input->post())
$this->myprocess();
$this->load->view('template', $data);
}
So after all this days and examining all my code and re-writing all of it i found the problem.
for some reason this line wasnt working : echo form_close(); so my first form was aways open messing up asll my validation after writing on all of then it starting working.

ZF2 Elements "select" and "multicheckbox" not render correctly

Why elements "select" and "multicheckbox" not render correctly (in html)?
Element "text" and "checkbox" render correctly!
Example:
1) Code in form:
class Profile extends Form {
public function __construct($name = null) {
parent::__construct('page');
$this->setAttribute('action', 'info');
$this->setAttribute('method', 'post');
$this->setInputFilter($this->getFilters());`
$this->add(array(
'type' => 'Zend\Form\Element\Select',
'name' => 'usernames',
'attributes' => array(
'id' => 'usernames'
),
'options' => array(
'label' => 'User Name',
'options' => array(
'test' => 'Hi, Im a test!',
'Foo' => 'Bar',
),
),
));
$this->add(array(
'type' => 'select',
'name' => 'language',
'options' => array(
'label' => 'Which is your mother tongue?',
'value' => array(
'0' => 'French',
'1' => 'English',
'2' => 'Japanese',
'3' => 'Chinese',
),
)
));
}
2) Code in view:
<h1><?php echo $this->translate('Profile') ?></h1>
<?php $form = $this->form; $form->prepare();?>
<?php echo $this->form()->openTag($form) ?>
<dl class="zend_form">
<?php foreach ($form as $element): ?>
<?php if ($element->getLabel() != null): ?>
<dt><?php echo $this->translate($this->formLabel($element)); ?></dt>
<?php endif ?>
<?php if ($element instanceof Zend\Form\Element\Button): ?>
<dd><?php echo $this->formButton($element) ?></dd>
<?php elseif ($element instanceof Zend\Form\Element\Captcha): ?>
<dd><?php echo $this->formCaptcha($element) . $this->translate($this->formElementErrors($element)); ?></dd>
<?php else: ?>
<dd><?php echo $this->formInput($element) . $this->translate($this->formElementErrors($element)); ?></dd>
<?php endif ?>
<?php endforeach ?>
</dl>
<?php if ($this->redirect): ?>
<input type="hidden" name="redirect" value="<?php echo $this->redirect ?>" />
<?php endif ?>
<input type="submit" value="<?php echo $this->translate('Submit'); ?>" />
<?php echo $this->form()->closeTag() ?>
3) Code in html, after render:
...
<dt><label for="usernames">User Name</label></dt>
<dd><input type="select" name="usernames" id="usernames" value=""></dd>
<dt><label for="language">Which is your mother tongue?</label></dt>
<dd><input type="select" name="language" value=""></dd>
...
it is not correctly!?
...
<select>
<option>value 1</option>
<option>value 2</option>
</select>
... - that's correct)
The formInput() helper is specifically for outputting <input> elements, so ZF is doing exactly what you've told it to. You probably want something more like this:
<?php foreach ($form as $element): ?>
<?php if ($element->getLabel() != null): ?>
<dt><?php echo $this->translate($this->formLabel($element)); ?></dt>
<?php endif ?>
<dd><?php echo $this->formElement($element) . $this->translate($this->formElementErrors($element)) ?></dd>
<?php endforeach ?>
The formElement() helper will call whatever helper is appropriate.