checkboxlist not rendering checkbox checked correctly - yii2

I have a two array that I'm going to include them into a checkbox,
echo Html::checkboxList('item', $selectedItem, $dataItem, [
'item' => function($index, $label, $name, $checked, $value) {
return "<label class='col-md-2'>
<input type='checkbox' {$checked} name='{$name}' value='{$value}'>
<span>{$label}</span>
</label>";
}
]);
I see in code in browser like this :
<label class="col-md-2">
<input 1="" name="item[]" value="1" disabled="" type="checkbox">
<span>Login</span>
</label>
The checked render into 1="" .
Please advise.
UPDATE
$dataItem
E:\wamp64\www\yii_tresnamuda\modules\it\views\request\preview.php:128:
array (size=6)
1 => string 'Login' (length=5)
2 => string 'Printer' (length=7)
3 => string 'Monitor' (length=7)
4 => string 'Computer' (length=8)
5 => string 'Network' (length=7)
6 => string 'Lain Lain' (length=9)
$selectedItem
E:\wamp64\www\yii_tresnamuda\modules\it\views\request\preview.php:129:
array (size=2)
2 => int 2
1 => int 1

The problem is in your {$checked} element of your item template. Try this:
echo Html::checkboxList('item', $selectedItem, $dataItem, [
'item' => function($index, $label, $name, $checked, $value) {
return "<label class='col-md-2'>
<input type='checkbox' name='{$name}' value='{$value}' ".($checked ? 'checked' : '').">
<span>{$label}</span>
</label>";
}
]);
Also, I think you have to change $selectedItem specification form [2=>2, 1=>1] to just [2,1]

Related

how to insert these data into mysql in wampserver

here are is the code.no matter what i did couldnt connect to mysql database in phpadmin in the wampserver all attempts has failed please help to solve
if (isset($_POST['continue'])) {
$j=0;
while ($j < $passengers)
{
$register_data2 = array(
'first_name' => $_POST["fname"][$j],
'last_name' => $_POST["lname"][$j],
'passport' => $_POST["passport"][$j],
'visa' => $_POST["visa"][$j],
'address1' => $_POST["address1"][$j],
'address2' => $_POST["address2"][$j],
'email' => $_POST["email"][$j],
'contact' => $_POST["contact"][$j],
'pin' => $_POST["pin"][$j],
'leaving_from' => $pieces[0],
'going_to' => $pieces[2],
'depart_date' => $pieces[7],
'depart_time' => $pieces[12],
'arrival_time' => $pieces[17],
'grand_fare' => $pieces[22],
'returning_from' => $pieces1[0],
'returning_to' => $pieces1[2],
'returning_date' => $pieces1[7],
'returning_time' => $pieces1[11],
'reaching_time' => $pieces1[16],
'fare' => $pieces1[21]
);
session_start();
$_SESSION['ticket'][] = $_POST["fname"][$j];
$_SESSION['ticket'][] = $_POST["lname"][$j];
$_SESSION['ticket'][] = $_POST["passport"][$j];
$_SESSION['ticket'][] = $_POST["visa"][$j];
$_SESSION['ticket'][] = $_POST["pin"][$j];
register_passenger($register_data2);
$j = $j+1;
}
$_SESSION['ticket1'] = $pieces[0];
$_SESSION['ticket2'] = $pieces[2];
$_SESSION['ticket3'] = $pieces[7];
$_SESSION['ticket4'] = $pieces[12];
$_SESSION['ticket5'] = $pieces[17];
$_SESSION['ticket6'] = $pieces[22];
$_SESSION['ticket11'] = $pieces1[0];
$_SESSION['ticket22'] = $pieces1[2];
$_SESSION['ticket33'] = $pieces1[7];
$_SESSION['ticket44'] = $pieces1[11];
$_SESSION['ticket55'] = $pieces1[16];
$_SESSION['ticket66'] = $pieces1[21];
}
?>
<?php
if (isset($_POST['pay'])){
if ($_POST['cash'] != $grand_total) {
echo "*Pay the given amount!"."<br>";
}
else{
header ('Location: ticket.php');
}
}
?>
<h2> Select payment method </h2>
<form action="payment.php" method="post">
<input type="radio" name="payment" id="cash" checked="checked" value="cash">
<label for="cash">Cash</label>
<input type="number" id="cash" name="cash" size="8"><br><br>
<input type="radio" name="payment" id="card" value="card">
<label for="card">Card</label>
<select>
<option>Debit card</option>
<option>Credit card</option>
</select>
<br>
<img src="Credit.jpg">
<br>
<input type="submit" name="pay" value="Make payment">
</form>
<?php
if(isset($_POST["Continue"])){
$firstname = $_POST['fname'];
$lastname = $_POST['lname'];
$passport = $_POST['passport'];
$visa = $_POST['visa'];
$address1 = $_POST['address1'];
$address2 = $_POST['address2'];
$email = $_POST['email'];
$contact = $_POST['contact'];
$pin = $_POST['pin'];
mysql_query( "INSERT INTO passengers (first_name,last_name,passport,visa,address1,address2,email,contact,pin) VALUES('$lastname','$passport','passport','$visa','$address1','$address2','$email','$contact','$pin')");
}
?>
i couldnt connect the data to mysql database in the phpadmin in the wampserver. it shows up nothing. as you can see below one of my many attempt to correct to make it work it up as fail. non of the method that i tried didnt work. please help to sove this problem
It seems you messed up the values list:
mysql_query( "INSERT INTO passengers
(first_name,last_name,passport,visa,address1,address2,email,contact,pin)
VALUES
('$lastname','$passport','passport','$visa','$address1','$address2','$email','$contact','$pin')");
First name is missing, 'passport' should not be there, etc. It should be:
('$firstname','$lastname','$passport','$visa','$address1','$address2','$email','$contact','$pin')");

How to remove 'label' decorator from Zend 2 form

I have the simple Radio element:
$form->add([
'name' => 'account_type',
'type' => 'Zend\Form\Element\Radio',
'options' => [
'label' => 'Account type',
'value_options' => [
1 => 'Employer',
2 => 'Performer'
]
]
]
);
But in my view I get this html:
<div class="zf-form-el-account_type">
<label for="account_type">Account type</label>
<div class="zf-radio">
<label>
<input type="radio" name="account_type" class="account_type" value="1">Employer
</label>
</div>
<div class="zf-radio">
<label>
<input type="radio" name="account_type" class="account_type" value="2">Performer
</label>
</div>
</div>
How I can to remove this empty label wrapper around an radio element? Or how I can to insert some tag after radio element? Thanks.
I extended standard view helper:
<?php
namespace Application\Form\View\Helper;
use Zend\Form\View\Helper\FormRadio;
use Zend\Form\Element\Radio as RadioEl;
class FormRadioElement extends FormRadio
{
protected function renderOptions(RadioEl $element, array $options, array $selectedOptions, array $attributes)
{ ...
... and set template in helper like:
$template = '%s%s';
Then I declared it in my bootstrap:
public function getViewHelperConfig() {
return [
'invokables' => [
'formRadioHelper' => 'Application\Form\View\Helper\FormRadioElement',
]
];
}
... and called in my view like:
<?php echo $this->formRadioHelper($form->get('account_type'))?>

How to display cakephp 2.5.3 model validation message.By default it shows html 5 validation message

How to display the validation message which is defined in CakePHP model.When I submit the form , CakePHP model validation works.But validation message for input field is not displayed. It shows HTML 5 validation Message. I want to display CakePHP model validation message. I am using CakePHP 2.5.3
Following is my code block:
Controller:
$this->User->set($this->request->data);
if ($this->User->validates()) {
if ($this->User->save($this->request->data)) {
$Email->send();
$this->Session->setFlash(__('The user has been created'));
if ($this->Auth->login()) {
$this->Session->setFlash(__('Welcome, ' . $this->Auth->user('username')));
$this->redirect($this->Auth->redirectUrl());
}
} else {
$this->Session->setFlash(__('The user could not be created. Please, try again.'));
}
}
Model:
public $validate = array(
'username' => array(
'nonEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'A username is required',
'allowEmpty' => false
),
'unique' => array(
'rule' => array('isUniqueUsername'),
'message' => 'This username is already in use'
),
'alphaNumericDashUnderscore' => array(
'rule' => array('alphaNumericDashUnderscore'),
'message' => 'Username can only be letters, numbers and underscores'
),
)
View:
<div id="divfirstname" class="input text required">
<label for="UserFirstname">First Name <span style="color:#FF0000">*</span></label>
<?php echo $this->Form->text('firstname');?>
</div>
<div id="divlastname" class="input text required">
<label for="UserLastname">Last Name <span style="color:#FF0000">*</span></label>
<?php echo $this->Form->text('lastname');?>
</div>
<div class="input text required">
<label for="UserUsername">Username <span style="color:#FF0000">*</span></label>
<?php echo $this->Form->text('username');?>
</div>
You don't need to wrap the validation-block around your save call.
So this is enough:
if ($this->User->save($this->request->data)) {
$Email->send();
$this->Session->setFlash(__('The user has been created'));
if ($this->Auth->login()) {
$this->Session->setFlash(__('Welcome, ' . $this->Auth->user('username')));
$this->redirect($this->Auth->redirectUrl());
}
} else {
$this->Session->setFlash(__('The user could not be created. Please, try again.'));
}
CakePHP handles automatically the validation when Model->save() is called and displays the error under your input fields. So if $this->save fails because of a validation error, cakephp adds <div class="error-message">Message Text from your model validation array</div> under the right input field.
You are left with 2 options :
Either dont use validation rule in cakePHP which validates on Client side.
On DOM ready , call $("#formId").removeAttr('novalidate'); Like
$(document).ready(function(){
$("#formId").removeAttr('novalidate');
});

Codeigniter - Input post array insert into mysql database

I dont understand how to exactly insert a row into a MySQL table. If I use my code, I add a new row for every post. Rather than that, I want only one row with all the values.
Here is the code sample.
HTML:
echo form_open('account/update');
echo "<p><label for='gender'>Gender</label>
<input type='text' name='gender' id='gender' value='".$gender."' />
</p>
<p>
<label for='age'>Age</label>
<input type='text' name='age' id='age' value='".$age."' />
</p>
<p>
<label for='bio'>Biografie</label>
<input type='text' name='bio' id='bio' value='".$bio."' />
</p>
<p>
<label for='skills'>Skills</label>
<input type='text' name='skills' id='skills' value='".$skills."' />
</p>
<p><input type='submit' value='Save' /></p>";
echo form_close();
Controller:
function update()
{
$userid = $this->session->userdata("userid");
$datanew = array(
'userid' => $this->session->userdata("userid"),
'gender' => $this->input->post('gender'),
'age' => $this->input->post('age'),
'bio' => $this->input->post('bio') ,
'skills' => $this->input->post('skills')
);
$this->session->set_userdata($data);
$this->load->model('model_account');
$this->load->model("model_user");
$this->model_account->profile_insert($datanew);
$this->load->view("change_avatar");
redirect("account/show/".$this->session->userdata("userid"));
}
Model:
function profile_insert($datanew)
{
$this->db->insert('profile', $datanew);
}
I get 5 rows if I submit the HTML form.
This works for me:
I Set the userid to unique and made a if statement in the controller ti switch between a insert and update function.
Controller:
function update()
{
$datanew = array(
'userid' => $this->session->userdata("userid"),
'gender' => $this->input->post('gender'),
'age' => $this->input->post('age'),
'bio' => $this->input->post('bio') ,
'skills' => $this->input->post('skills')
);
$exists = $this->db->select('profile')->where('userid', $this->session->userdata("userid"));
if($exists)
{
$this->session->set_userdata($datanew);
$this->load->model('model_account');
$this->load->model("model_user");
$this->model_account->profile_update($datanew);
$this->load->view("change_avatar");
redirect("account/show/".$this->session->userdata("userid"));
}
else
{
$this->session->set_userdata($datanew);
$this->load->model('model_account');
$this->load->model("model_user");
$this->model_account->profile_insert($datanew);
$this->load->view("change_avatar");
redirect("account/show/".$this->session->userdata("userid"));
}
}
model:
function profile_insert($datanew)
{
$this->db->insert('profile', $datanew);
}
function profile_update($datanew)
{
$this->db->update('profile', $datanew);
}
Thanks for helping me

Form elements into $_POST arrays

I'm probably being stupid, because its Friday afternoon, but I just can't work this out.
What I want is to have a form where users can add infinite "records". To do this they click an "add record" button. This runs javascript which adds a new row (tr) to a table within the <form>.
This new row has three input fields.
What I want is for this to be sent to the post variable in a format such as (but not exact if it can't be done, but there's a better way):
$_POST['record'] = array(
array(
"input1" => "value",
"input2" => "value",
"input3" => "value"
),
array(
"input1" => "value",
"input2" => "value",
"input3" => "value"
),
array(
"input1" => "value",
"input2" => "value",
"input3" => "value"
),
);
I know you can get arrays by using the name like so:
<input type="text" name="record[]" />
But this is only 1 input element. Is there any way to get a structure like above with 3 elements?
Thank you.
You can't easily get what you're looking for exactly, but you can use name="record[input1][]" (and input2 etc.) and the result is:
$_POST['record'] = array(
"input1"=>array(
"value", "value", "value"
),
"input2"=>array(
"value", "value", "value"
),
"input3"=>array(
"value", "value", "value"
)
);
You could then transform it into your desired format like so:
$out = Array();
foreach(array_keys($_POST['record']['input1']) as $i) {
foreach($_POST['record'] as $k=>$v) {
$out[$i][$k] = $v[$i];
}
}
I think you are on the right track. using name=record[]. You'll get something like
$_POST['record'] = array(
"record" => array(
"value",
"value",
"value"
),
"field2" => array(
"value",
"value",
"value"
),
"field3" => array(
"value",
"value",
"value"
)
);
So to get each row, you'd use
$cnt = count( $theArray['record'] );
for ($x=0; $x<$cnt; $x++){
echo $theArray['record'][$x];
echo $theArray['field2'][$x];
echo $theArray['field3'][$x];
}
I just tried --
<form action="" method="post">
<input type="text" name="record['set1'][]" />
<input type="text" name="record['set1'][]" />
<input type="text" name="record['set1'][]" />
<input type="text" name="record['set2'][]" />
<input type="text" name="record['set2'][]" />
<input type="text" name="record['set2'][]" />
<input type="text" name="record['set3'][]" />
<input type="text" name="record['set3'][]" />
<input type="text" name="record['set3'][]" />
<input type="submit" value="submit" />
</form>
Output --
Array
(
[record] => Array
(
['set1'] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
['set2'] => Array
(
[0] => 4
[1] => 5
[2] => 6
)
['set3'] => Array
(
[0] => 7
[1] => 8
[2] => 9
)
)
)
Is this how you wanted it?
I think this would be more clearly for you,
$record = array();
foreach($_POST['record'] as $val){
$record[] = $val;
}
print_r($record);