checkbox selected in codeigniter - codeigniter

Hi i have this select checkbox which is weekdays the checkboxes were sunday monday tuesday wednesday thursday friday saturday. Users can able to choose as many as they want. And i save the data to my database,like for example users choose sunday monday tuesday, i save that into my fields as 1-2-3 with only one field name available_days_per_week. Now my problem is when i queried the data and display into my edit page i want that only 1-2-3 will be checked. What happened is the data doubles. My code works but displays into double. Heres my code below
<tr>
<th>Available days per week</th>
<td>
<?php $daysPerWeeks = $query->available_days_per_week; ?>
<?php $daysPerWeeks1 = explode("-", $daysPerWeeks); ?>
<?php foreach($daysPerWeeks1 as $value): ?>
<?php foreach($daysPerWeekCheckboxes as $key=>$week): ?>
<input type="checkbox" name="available_days_week" value="<?php echo $key ?>" <?php echo($value == $key) ? 'checked="checked"' : ''?> ><?php echo $week; ?><br>
<?php endforeach; ?>
<?php endforeach; ?>
</td>
</tr>
that will display into my page like this
<tr>
<th>Available days per week</th>
<td>
<input type="checkbox" name="available_days_week" value="1" checked="checked" >Sunday<br>
<input type="checkbox" name="available_days_week" value="2" >Monday<br>
<input type="checkbox" name="available_days_week" value="3" >Tuesday<br>
<input type="checkbox" name="available_days_week" value="4" >Wendesday<br>
<input type="checkbox" name="available_days_week" value="5" >Thursday<br>
<input type="checkbox" name="available_days_week" value="6" >Friday<br>
<input type="checkbox" name="available_days_week" value="7" >Saturday<br>
<input type="checkbox" name="available_days_week" value="1" >Sunday<br>
<input type="checkbox" name="available_days_week" value="2" checked="checked" >Monday<br>
<input type="checkbox" name="available_days_week" value="3" >Tuesday<br>
<input type="checkbox" name="available_days_week" value="4" >Wendesday<br>
<input type="checkbox" name="available_days_week" value="5" >Thursday<br>
<input type="checkbox" name="available_days_week" value="6" >Friday<br>
<input type="checkbox" name="available_days_week" value="7" >Saturday<br>
<input type="checkbox" name="available_days_week" value="1" >Sunday<br>
<input type="checkbox" name="available_days_week" value="2" >Monday<br>
<input type="checkbox" name="available_days_week" value="3" checked="checked" >Tuesday<br>
<input type="checkbox" name="available_days_week" value="4" >Wendesday<br>
<input type="checkbox" name="available_days_week" value="5" >Thursday<br>
<input type="checkbox" name="available_days_week" value="6" >Friday<br>
<input type="checkbox" name="available_days_week" value="7" >Saturday<br>
</td>
</tr>
as youve noticed it wil double
heres my static code that will return days of weeks
//return days per week checkbox
public static function getDaysPerWeekCheckbox(){
return array(
1 =>'Sunday',
2 =>'Monday',
3 =>'Tuesday',
4 =>'Wendesday',
5 =>'Thursday',
6 =>'Friday',
7 =>'Saturday'
);
}
How can i avoid the data that will not doubled when displaying in the edit page?.
Can someone help me figured this thing out?.Any help is muchly appreciated.

You are looping unnecessarily. Try this:
<?php $days = getDaysPerWeekCheckbox(); ?>
<?php foreach($days as $index => $day_name): ?>
<input type="checkbox" name="available_days_week" value="<?php echo $index ?>" <?php echo(in_array($index, $daysPerWeeks1)) ? 'checked="checked"' : ''?> ><?php echo $day_name; ?><br>
<?php endforeach; ?>

Related

How can I color input fields border red when I get validation error laravel 5.3

My Validation Controller:
public function store(Request $request)
{
// dd($request->all());
/////Validations////
$this->Validate($request, [
'name' => 'required|min:3|',
'email' => 'required|unique:customers' ,
'favorite' => 'required',
'Password' => 'required|min:6',
'Confirm_Password' => 'required|same:Password',
]);
$user = new customer;
$user ->name=input::get("name");
$user ->email=input::get("email");
$user ->country=input::get("country");
$user ->gender=input::get("gender");
$user ->favorite=implode(" , " , input::get("favorite"));
if(input::hasfile("image")){
$files = Input::file('image');
$name = time()."_". $files->getClientOriginalName();
$image = $files->move(public_path().'/image' , $name);
$user->image=$name;
}
$user ->psw=input::get("psw");
$user->save();
return redirect("showall");
}
Insert.blade.php
<form action="store" method="post" enctype="multipart/form-data">
<ul><span style="color:red;">{!! $errors->first('name') !!}</span></ul>
<label for="name">Name</label>
<input type="text" name="name" value="{{old('name')}}" id="name" autofill="off">
<input type="hidden" name="_token" value="{{csrf_token()}}">
<ul><span style="color:red;">{!! $errors->first('email') !!}</span></ul>
<label for="email">Email</label>
<input type="email" name="email" value="{{old('email')}}" id="email" autofill="off">
<br>
<br>
Country <select name="country" id="country">
<option value="USA">USA </option>
<option value="ENGLAND">ENGLAND </option>
<option value="JAPAN">JAPAN </option>
<option value="ITALY">ITALY</option>
</select>
<br>
<br>
Gender <input type="radio" name="gender" value="Male">Male
<input type="radio" name="gender" value="Female">Female
<br>
<br>
<ul><span style="color:red;">{!! $errors->first('favorite') !!}</span></ul>
<input type="checkbox" name="favorite[]" value="sout">South
<input type="checkbox" name="favorite[]" value="north">North
<input type="checkbox" name="favorite[]" value="east">East
<input type="checkbox" name="favorite[]" value="west">West
<br>
<br>
<input type="file" name="image">
<br>
<br>
<ul><span style="color:red;">{!! $errors->first('Confirm_Password') !!}</span></ul>
<label for="email">Password</label>
<input type="password" name="Password" value="" id="Password">
<ul><span style="color:red;">{!! $errors->first('Confirm_Password') !!}</span></ul>
<label for="email">Confirm Password</label>
<input type="Password" name="Confirm_Password" value="" id="cpsw">
<br>
<br>
<label for="submit"></label>
<input type="submit" name="submit" value="submit" id="submit">
</form>
Form where I want to change fields color:
I want to change input fields border color to change red when I get a validation error.
I try my best but I don't know how to rid off this problem.
Add this CSS code first:
.form-error {
border: 2px solid #e74c3c;
}
And HTML Code is ::
<input type="text" class="form-control{{($errors->first('name') ? " form-error" : "")}}" name="name" placeholder="Name">
if you need error message below the input field then ::
<input class="form-control" required="required" name="name" type="text" id="name">
{!! $errors->first('name', '<p class="help-block">:message</p>') !!}

Validating Arrays of radio buttons in laravel

I am using Laravel 5.1 and homestead.
My issue is when validation fails, the radio buttons do not repopulate with data from the original user's input.
My validation rules from PostRequest.php
public function rules()
{
//validating input text field --> this one works
$rules = ['owner' => 'required|max:15',];
//validating array of input text fields --> this too works
foreach($this->request->get('item') as $a => $z)
{
$rules['item.'.$a] = 'required';
}
//validating array of radio buttons --> does not return with data
foreach($this->request->get('radio') as $b => $y)
{
$rules['radio.'.$b] = 'required';
}
return $rules;
}
A segment of my view(blade.php)
<div class="row">
<div class="tcell col-xs-6">
<label for="item[0]" class="sr-only"></label>
<input class="form-control" id="item[0]" name="item[0]" type="text" placeholder="enter item" value="{ { old('item.0') } }"/>
</div>
<div class="tcell col-xs-6">
<div class="radio">
<input type="radio" id="s15" name="radio[0]" value="5" /><label for="s15" title="5">5</label>
<input type="radio" id="s14" name="radio[0]" value="4" /><label for="s14" title="4">4</label>
<input type="radio" id="s13" name="radio[0]" value="3" /><label for="s13" title="3">3</label>
<input type="radio" id="s12" name="radio[0]" value="2" /><label for="s12" title="2">2</label>
<input type="radio" id="s11" name="radio[0]" value="1" /><label for="s11" title="1">1</label>
</div>
</div>
</div>
...
<div class="tcell col-xs-6">
<div class="radio">
<input type="radio" id="s20" name="radio[1]" value="5" /><label for="s20" title="5">5</label>
<input type="radio" id="s19" name="radio[1]" value="4" /><label for="s19" title="4">4</label>
<input type="radio" id="s18" name="radio[1]" value="3" /><label for="s18" title="3">3</label>
<input type="radio" id="s17" name="radio[1]" value="2" /><label for="s17" title="2">2</label>
<input type="radio" id="s16" name="radio[1]" value="1" /><label for="s16" title="1">1</label>
</div>
</div>
The old() function worked for both input(text) and array of input(text). but I do not know how to apply it with radio buttons.
Thanks
Here's the solution
<input type="radio" id="s15" name="radio[1]" value="5" { { old('radio.1')=="5" ? 'checked='.'"'.'checked'.'"' : '' } } /><label for="s15" title="5">5</label>
<input type="radio" id="s14" name="radio[1]" value="5" { { old('radio.1')=="4" ? 'checked='.'"'.'checked'.'"' : '' } } /><label for="s14" title="4">4</label>
...
This will only help redirect back with the inputed data, however, validation will still fail if nothing is selected since nothing gets submitted. The fix is to use for loop instead of foreach loop. You must get the count of the radio buttons to iterate.
try to amend as following
blade template:
<div class="radio">
<input type="radio" id="s15" name="radio" value="5" /><label for="s15" title="5">5</label>
<input type="radio" id="s14" name="radio" value="4" /><label for="s14" title="4">4</label>
<input type="radio" id="s13" name="radio" value="3" /><label for="s13" title="3">3</label>
<input type="radio" id="s12" name="radio" value="2" /><label for="s12" title="2">2</label>
<input type="radio" id="s11" name="radio" value="1" /><label for="s11" title="1">1</label>
</div>
Form request
public function rules()
{
//validating input text field --> this one works
$rules = ['owner' => 'required|max:15', 'radio' => 'required'];
//validating array of input text fields --> this too works
foreach($this->request->get('item') as $a => $z)
{
$rules['item.'.$a] = 'required';
}
return $rules;
}

country drop down not being populate with data codeigniter

country drop down not being populate with data codeigniter, can any body help me out, i want list of country on view so that i could select country. when i add select code in view for drop down it hides the list and save button.
my model
public function get_Country() {
$this->db->select('code, name')->from("country");
$query = $this->db->get();
return $query->result_array();
}
my controller
public function get_Country() {
$ctry = $this->job_post_model->get_Country();
// debug($ctry);
}
my view code
<div class="row">
<div class="span8">
<div class="about-heading">
<div class="head-menu"> </div>
</div>
<div class="about-section">
<div class="about-content rigs-content">
<form name="jobpost" action=" <?php echo base_url('employer/job_post/post_job'); ?>" method="post">
<h2>Your Job Detail :</h2>
<div class="invest-form">
<ul>
<li class="pull-right">
<label>Job Category:</label>
<input type="text" name="jobcat" value="" placeholder="Enter Job Category*" />
</li>
<li class="pull-right">
<label>Job Title:</label>
<input type="text" name="jobtitle" value="" placeholder="Enter Job Title*" />
</li>
<li>
<label>Job Type:</label>
<input type="text" name="jobtype" value="" placeholder="Enter Job Type*" />
</li>
<li class="pull-right">
<label>Preffered Age:</label>
<input type="text" name="age" value="" placeholder="Enter Job Title*" />
</li>
<li>
<label>Preffered Gender:</label>
<input type="text" name="gender" value="" placeholder="Enter Job Type*" />
</li>
<li>
<label>Job Description:</label>
<input type="text" name="desc" value="" placeholder="Enter Job Type*" />
</li>
<li class="pull-right">
<label>Location:</label>
<input type="text" name="location" value="" placeholder="Enter the Country*" />
</li>
<li>
<label>Post Code:</label>
<input type="text" id="post" rel="popover" data-trigger="hover" name="postcode" placeholder="Enter the City*" />
</li>
<li class="pull-right">
<label>Salary:</label>
<input type="text" id="salaries" name="salary" placeholder="Enter the Salary*" />
</li>
<li>
<li>
<label>Qualification:</label>
<input type="text" id="qualification" rel="popover" data-trigger="hover" name="qualification" placeholder="Enter Benifits*" />
</li>
<li class="pull-right">
<label>Category:</label>
<input type="text" id="category" class="span12" required name="category" placeholder="Enter Job Tags" />
</li>
<label>Benifits:</label>
<input type="text" id="benefits" rel="popover" data-trigger="hover" name="benefits" placeholder="Enter Benifits*" />
</li>
<li class="pull-right">
<label>Job Tags:</label>
<input type="text" id="jobtags" class="span12" required name="jobtag" placeholder="Enter Job Tags" />
</li>
<li class="pull-right">
<label>Career Level:</label>
<input type="text" id="career" class="span12" required name="career" placeholder="Enter Job Tags" />
</li>
<li>
<label>Country:</label>
<?php $countries = get_Country(); ?>
<select name="country">
<option>Select your country*</option>
<?php foreach ($countries as $country) { ?>
<option value="<?php echo $country->name ?>" </option>
<?php }
?>
</select>
</li>
<li class="pull-right">
<table id="question">
<th>Add Killer Questions</th>
<tr>
<td><input type="text" id="questions" name="question[]"/></td>
</tr>
</table>
<td><input type="button" id="btnAdd" class="button-add" onClick="insertTextBox()" value="Add More"></input></td>
</li>
<br>
<input type="submit" value="Save As Draft" class="button-next" />
</div>
</form>
<div class="wid-social">
</div>
</div>
</div>
</div>
<script>
var index = 1;
function insertTextBox(){
var text = document.getElementById("question");
var row=text.insertRow(text.rows.length);
var cell1=row.insertCell(0);
var t1=document.createElement("input");
t1.id = "question"+index;
t1.name = "question[]";
cell1.appendChild(t1);
var cell2=row.insertCell(1);
index++;
}
</script>
In model:
Repalce
return $query->result_array();
With
return $query->result();
In View :
Repalce
<?php $countries = get_Country(); ?>
<select name="country">
<option>Select your country*</option>
<?php foreach ($countries as $country) { ?>
<option value="<?php echo $country->name ?>" </option>
<?php }
?>
</select>
With
<?php $countries = get_Country(); ?>
<?php echo form_dropdown('country',$countries, 0,'required'); ?>
In Codeigniter Use Codeigniter From Dropdown
Your model is correct, but you do need to decide if you are going to access the data as an array or an object. That will dictate how you access your data in the View.
$query->result_array() returns an array
$query->result() returns an object
In your controller you need to use your model.
// Load your model into your controller
$this->load->model('your_model');
// use your_model method to set $data['countries']
$data['countries'] = $this->your_model->get_Country();
// You will access $countries in your view by passing it to your view
$this->load->view('your_view'), $data);
In your view, if you choose $query->result() in your model :
<select name="country">
<option>Select your country*</option>
<?php foreach ($countries as $country): ?>
<option value="<?php echo $country->name ?>"><?php echo $country->name ?></option>
<?php endforeach; ?>
</select>
Your controller didn't return anything, so need to modify controller as below.
controller :
public function get_Country() {
return $ctry = $this->job_post_model->get_Country();
}
and also your VIEW returning result as an array, you need to use $country['name'] in the place of $country->name.
VIEW:
<?php $countries = get_Country(); ?>
<select name="country">
<option>Select your country*</option>
<?php foreach ($countries as $country) { ?>
<option value="<?php echo $country['code'] ?>"><?php echo $country['name'] ?></option>
<?php }
?>
</select>

receive post information with codeigniter

I have an order form with data. on submit the controller function new_blank_order_summary is called.
the syntax for this is:
function new_blank_order_summary()
{
echo "orderlines: ".$this->input->post(orderlines);
echo "customer: ".$this->input->post('customer');
echo "period: ".$this->input->post('period');
echo "creditlimit: ".$this->input->post('creditlimit');
$this->load->view('sales/new_blank_order_summary');
}
I cannot get the post information to display or echo? my input Name and ID is orderlines but this is not being posted or received properly.
Apologies for the 'stupid' question.
any advice welcome.
HTML
<form id="sales_blank_order_details" action="/sales/new_blank_order_summary" method="post">
<table >
<tr><td>Customer</td><td>Period</td><td>UoM</td><td>Credit Limit</td><td>Balance</td><td>New Balance</td><td>Order Lines</td><td>Round to Bundle</td></tr>
<tr><td>
<input type="text" name="customer" id="customer" value="<?php echo $customer; ?>" disabled>
</td><td>
<input type="text" name="period" id="period" value="<?php echo $period." to ".$newperiod; ?>" disabled>
</td><td>
<input type="text" name="buom" id="buom" value="<?php echo $buom; ?>" disabled>
</td><td>
<input type="text" name="creditlimit" id="creditlimit" value="<?php echo $creditlimit['creditlimit']; ?>" disabled>
</td><td>
<input type="text" name="currentbalance" id="currentbalance" value="<?php echo $creditlimit['currentbalance']; ?>" disabled>
</td>
<td>
<input type="text" name="newbalance" id="newbalance" value="<?php echo $creditlimit['currentbalance']; ?>" disabled>
</td><td>
<input type="text" name="orderlines" id="orderlines" value="1" disabled>
</td><td>
<input type="checkbox" name="rounduptobundle" id="rounduptobundle" checked>
</td></tr></table>
<input type="submit" name="blank_order_lines" id="blank_order_lines" value="Save Order and Proceed">
</form>
None of these posts are outputted by controller.
You've disabled the orderlines text input field so that's why it will not get posted. Remove disabledand it will show up in $this->input->post().
what is the name and value of your submit button. Usually the name of my submit button is name="submit" and the value is value="submit". So then I try $this->input->post('submit')=='submit'. try this in your if condition. I hope it will work. best of luck.
function new_blank_order_summary()
{
echo "orlderlines: ".$this->input->post('orderlines');
echo "customer: ".$this->input->post('customer');
echo "period: ".$this->input->post('period');
echo "creditlimit: ".$this->input->post('creditlimit');
$this->load->view('sales/new_blank_order_summary');
}
Please try this code. I didn't change a single line in it. Only I put the single quotes in the 3rd line. And, changed it's variable name is wrong so I changed it

jQuery validation plugin checkbox validation

I need to validate the form. I am using jQuery validation plugin but I cannot seem to get this working.
<form id="form1" name="form1" action=<?=$_SERVER['PHP_SELF'];?> method="POST">
<div>
<h3>Cardio-Pulmonary System</h3>
<div style="border: medium none;display: inline;padding: 0;text-align:left">
<h4>1. Do you have, or have you had, or do you take medications for?</h4>
<label><input class="checkbox" type="checkbox" value="none" name="q1[]" />no / or none of the below</label>
<p><input class="checkbox" type="checkbox" value="q1-1" name="q1[]" />heart disease (please specify)
<input class="checkbox" type="text" id="text" name="q1-1" value="" /></p>
<p><input class="checkbox" type="checkbox" value="high blood pressure" name="q1[]" />high blood pressure</p>
<p><input class="checkbox" type="checkbox" value="high cholesterol" name="q1[]" />high cholesterol</p>
<p><input class="checkbox" type="checkbox" value="diabetes" name="q1[]" />diabetes</p>
<p><input class="checkbox" type="checkbox" value="q1-2" name="q1[]" />lung disorder (eg asthma, emphysema)
<input class="checkbox" type="text" id="text" name="q1-2" value="" /></p>
<p><input class="checkbox" type="checkbox" value="other cardiac problem" name="q1[]" />other cardiac problem (include pacemaker)</p>
<h4>2. Do you have a family history of?</h4>
<p><input type="checkbox" value="none" name="q2[]" />no / or none of the below</p>
<p><input type="checkbox" value="heart murmur" name="q2[]" />heart murmur</p>
<p><input type="checkbox" value="valve defect" name="q2[]" />valve defect</p>
<p><input type="checkbox" value="racing heart" name="q2[]" />racing heart</p>
<p><input type="checkbox" value="irregular beats" name="q2[]" />irregular beats</p>
<p><input type="checkbox" value="angina" name="q2[]" />angina</p>
<p>other<br/><input type="text" id="text" value="" name="q2[]" /></p>
<h4>3. Have you ever been told that you have heart problems? Eg</h4>
<p><input type="checkbox" value="none" name="q3[]" />no / or none of the below</p>
<p><input type="checkbox" value="heart disease" name="q3[]" />heart disease</p>
<p><input type="checkbox" value="high blood pressure" name="q3[]" />high blood pressure</p>
<p><input type="checkbox" value="high cholesterol" name="q3[]" />high cholesterol</p>
<p><input type="checkbox" value="diabetes" name="q3[]" />diabetes</p>
<p><input type="checkbox" value="stroke" name="q3[]" />stroke</p>
<h4>4. Do you have, or have you experienced?</h4>
<p><input type="checkbox" value="none" name="q4[]" />no / or none of the below</p>
<p><input type="checkbox" value="epilepsy" name="q4[]" />epilepsy</p>
<p><input type="checkbox" value="fainting" name="q4[]" />fainting</p>
<p><input type="checkbox" value="seizures" name="q4[]" />seizures</p>
<p><input type="checkbox" value="dizzy spells" name="q4[]" />dizzy spells</p>
<p><input type="checkbox" value="convulsions" name="q4[]" />convulsions</p>
<h4>5. Have you ever smoked cigarettes?</h4>
<p><input type="checkbox" value="q5-1" name="q5[]" />Yes, still do approx <input type="text" name="q5-1" style="width:20px" maxlength="3" /> a day</p>
<p><input type="checkbox" value="q5-2" name="q5[]" />Yes, but stopped <input type="text" name="q5-2" style="width:20px" maxlength="3" /> months / <input type="text" name="q5-3" style="width:20px" maxlength="3" /> years ago. </p>
<p><input type="checkbox" value="never" name="q5[]" />Never</p>
</div>
</form>
How do you validate checkboxes so that they need to select at least one for each question?
This is my form...http://test9.favstay.com/form.php
Use min and max length
<input type="checkbox" value="palin" name="upd" id="upd"/>
</p>
<p>
<label for="fox">Fox</label>
<input type="checkbox" value="fox" name="upd" id="upd" />
</p>
<p>
<label for="left">Left</label>
<input type="checkbox" value="left" name="upd" id="upd" />
</p>
Validate
$("#upd").rules("add", {
required: true,
minlength: 1,
maxlength: 1,
messages: {
required: "Please pick a category",
minlength: jQuery.format("Please, Check at least one box"),
maxlength: jQuery.format("Please, You checked too many boxes"),
}
});
Add an ID for each checkbox(q4, q5 etc), Substitute your q3, q4 etc where I have #upd. Just adapt this to your needs.
You can use jquery validation plugin to validate an HTML form, and to validate the checkboxes, you can add "custom selectors" to the validate options. Check out the example here

Resources