How to set IDs for select elements of Quickform_date? - php

Is there some way to add ID attribute to every select element generated by Quickform_date so it would look for example like this?
<select id="date-d" name="date[d]">
<option value="1">01</option>
<option value="2">02</option>
...
<option value="31">31</option>
</select><select id="date-M" name="date[M]">
<option value="1">Jan</option>
...
<option value="12" selected="selected">Dec</option>
</select><select id="date-Y" name="date[Y]">
<option value="2001">2001</option>
...
<option value="2011">2011</option>
</select>
I need those IDs for changing selected options by JavaScript. Any alternative ideas how to achieve that?

HTML_QuickForm_group::getElements() method turn out to be the key, in case anyone's interested :-)
$elements['date'] = $form->addElement('date', 'date','Datum:',
array('language' => 'cs',
'minYear' => 2005,
'maxYear' => date('Y') + 2));
foreach ($elements['date']->getElements() as $key => $element) {
$element->updateAttributes(array('id'=>'dateSelect'.$key));
}

Related

Keeping lead Zero in MySQL

I have a table with a varchar column. I use a select form with values 01, 02, 03, 04 .... 10, 11, 12 etc to store data in this column. My problem is that, when I submit the form, the leading 'Zero' is removed. How can I maintain the leading zero?
**
NB. I have checked other similar posts but they don't have the
solution for my issue. I have tried var/char/text and still it's not
working hence why I posted this question if there's another way to get this done.
**
<form method=post enctype="multipart/form-data">
<select name="dd">
<option selected="selected">Day</option>
<option value='01'>01</option>
<option value='02'>02</option>
<option value='03'>03</option>
<option value='04'>04</option>
<option value='05'>05</option>
<option value='06'>06</option>
<option value='07'>07</option>
<option value='08'>08</option>
<option value='09'>09</option>
<option value='10'>10</option>
<option value='11'>11</option>
</select>
<select name="mm">
<option selected="selected">Month</option>
<option value='01'>01</option>
<option value='02'>02</option>
<option value='03'>03</option>
<option value='04'>04</option>
<option value='05'>05</option>
<option value='06'>06</option>
<option value='07'>07</option>
<option value='08'>08</option>
<option value='09'>09</option>
<option value='10'>10</option>
<option value='11'>11</option>
</select>
</form>
MySQL is as follows;
$caption = $_POST['dd'].-$_POST['mm'];
$sql = "INSERT INTO pages SET caption = '$caption'";
Use sprintf to format your value:
$caption = sprintf("%02d-%02d", $_POST['dd'], $_POST['mm']);
The key here is to work with the data as strings and don't do math on it like -$_POST['mm'] which will treat it as a number. In numbers leading zeros are always omitted because they always exist. 10 is actually something like 0x0000000a as far as the CPU is concerned.

Get multiple select data from Bootstrap Select using PHP

I'm using bootrap-select plugin
1: https://github.com/snapappointments/bootstrap-select/ I'm trying to grab data from it. I try so many ways but it doesn't work out. So the input is like this:
<div class="col-9">
<select id="generals" name="generals" class="form-control kt-selectpicker" multiple title="Choose one of the following...">
<optgroup label="Passport">
<option value="passport_number">Number</option>
<option value="passport_date">Date of issuance</option>
<option value="passport_expired">Date of expiry</option>
<option value="passport_office">Issuing Office</option>
</optgroup>
<optgroup label="Personal">
<option value="applicant_id">Applicant Id</option>
<option value="first_name">First Name</option>
<option value="first_name">Middle Name</option>
<option value="last_name">Last Name</option>
<option value="address">Address</option>
<option value="crew_id">Crew Id</option>
<option value="email">Email</option>
<option value="mobile_number">Mobile Number</option>
</optgroup>
</select>
</div>
I try to grab using PHP foreach and for loops, implode or explode but it doesn't work since it said it does not an array. When i see the Header data sent is like this:
so how can i get this data? I'm using Ajax to send data. Here's the Fiddle if you want to see it: https://jsfiddle.net/4u6xc9kd/
It's easy, you just put the input name like this name="generals[]" in your input select and the use foreach like this:
$generals = '';
if (isset($_POST['generals'])){
foreach ($_POST['generals'] as $value){
$generals .= $value . ', ';
}
}

php - multiple select with different values (security easier method)

I don't think this is a duplicate, I only found fairly similar questions.
I have 4 checkboxes and I want to make sure they are different, but I think my "if" is a mess. Let's imagine I have 20 select boxes, then I'd have an endless "if" (option1!=option2...option20, it'd be a really long "if").
How can I simplify this? A while loop or something?
Here is what I have:
PHP check it, then save it:
if(($option1!=$option2 and $option1!=$option3 and $option1!=$option4)
and ($option2!=$option1 and $option2!=$option3 and $option2!=$option4)
and ($option3!=$option1 and $option3!=$option2 and $option3!=$option4)
and ($option4!=$option1 and $option4!=$option2 and $option4!=$option3)) {
//insert it to database if everything is okay...
HTML:
<select name="option1">
<option value="">Choose one</option>
<option value="1">Option A</option>
<option value="2">Option B</option>
<option value="3">Option C</option>
<option value="4">Option D</option>
...
</select>
<select name="option2">
<option value="">Choose one</option>
<option value="1">Option A</option>
<option value="2">Option B</option>
<option value="3">Option C</option>
<option value="4">Option D</option>
...
</select>
<select name="option3">
<option value="">Choose one</option>
<option value="1">Option A</option>
<option value="2">Option B</option>
<option value="3">Option C</option>
<option value="4">Option D</option>
...
</select>
<select name="option4">
<option value="">Choose one</option>
<option value="1">Option A</option>
<option value="2">Option B</option>
<option value="3">Option C</option>
<option value="4">Option D</option>
...
</select>
If you have 20 select dropdown lists with all of them have name attributes in chronological order, like name='option1', name='option2', ... , name='option20', then you should use a for loop like this:
$numSelects = 20;
$selectValues = array();
$differentValues = true;
for($i = 1; $i <= $numSelects; ++$i){
if(in_array($_POST['option'.$i], $selectValues)){
$differentValues = false;
break;
}
$selectValues[] = $_POST['option'.$i];
}
if($differentValues){
// all the selected values are different
}else{
// selected values are not different
}
$numSelects is the number of select dropdown lists in your code, so you need to change this value as per your code. And what this for loop here does is, in each iteration of for loop it checks whether the user's selected value exists in $selectValues array or not, and if the value already exists in the array then it will disable $differentValues flag and break out from the loop. Moreover, in each iteration it appends user's selected value to $selectValues array. After coming out of the loop, you can check whether all submitted values are different or not, based on the status of $differentValues flag.
You could do it like this using array_unique(), you could build on it to know which key is missing to show an error in the right place. It also allows you to define which post keys your expecting to check/count.
<?php
$_POST = [
'name' => 'Steve McQueen',
'csrf' => 'y53zmSV0LhhjcjEg',
'option1' => '1',
'option2' => '3',
'option3' => '2',
'option4' => '1'
];
$expected = [
"option1",
"option2",
"option3",
"option4"
];
$result = [];
foreach ($expected as $key) {
$result[$key] = isset($_POST[$key]) ? $_POST[$key] : null;
}
$check = array_unique($result);
if (count($check) !== count($expected)) {
echo 'Please only select unique choices from the options';
} else {
echo 'All good!';
}

Codeigniter: Fetch the value from an option in the dropdown list and check SQL condition

I am having an issue fetching the values from a dropdown list to check a condition with the mysql query. It is using codeigniter. Let me post the code I have included below:
VIEW:
<select name="month" id="month">
<option>Select Month</option>
<option value="01">January</option>
<option value="02">February</option>
<option value="03">March</option>
<option value="04">April</option>
<option value="05">May</option>
<option value="06">June</option>
<option value="07">July</option>
<option value="08">August</option>
<option value="09">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
The above values should be equal to the month in the date column of the table. I will post my model below:
MODEL:
$month = $this->input->post('month');
$this->datatables->select('order_no,order_date,naturescrm_customers.name,total,naturescrm_users.username,naturescrm_order.id,
naturescrm_order_status.status_name')
->unset_column('naturescrm_order.id')
->add_column('Actions', btn_edit('agent/order/edit/' . '$1').' '.get_view('$1','agent/order/view/') ,'naturescrm_order.id')
->from($this->_table_name)
->join("naturescrm_customers","naturescrm_customers.id=naturescrm_order.customer_id","left")
->join("naturescrm_order_status","naturescrm_order_status.status_id=naturescrm_order.order_status","left")
->join("naturescrm_users","naturescrm_users.id=naturescrm_order.user_id","left")
->where("DATE_FORMAT(order_date, '%m') = $month");
$this->db->order_by("order_date","desc");
There is my code in the model and in the view, So when I Select a month in the dropdown, the values being fetched in the database should reflect based on the month. Any help will be much appreciated. Thanks

Laravel Creating A Custom Form Macro Select Year and Month

I need help building my laravel custom macro. In my start/global.php I required a macros.php
...
require app_path().'/macros.php';
and on my macros.php I have this code that I read in this site.
Form::macro('selectMonths', function()
{
$selectMonth = preg_replace('/></', '><option value="">Month</option><', Form::selectMonth('month'));
return $selectMonth;
});
and the result is this.
<select class="form-control" name="month">
<option value="">Month</option>
<option value="1">January</option>
<option value="">Month</option>
<option value="2">February</option>
<option value="">Month</option>
<option value="3">March</option>
<option value="">Month</option>
<option value="4">April</option>
<option value="">Month</option>
<option value="5">May</option>
<option value="">Month</option>
<option value="6">June</option>
<option value="">Month</option>
<option value="7">July</option>
<option value="">Month</option>
<option value="8">August</option>
<option value="">Month</option>
<option value="9">September</option>
<option value="">Month</option>
<option value="10">October</option>
<option value="">Month</option>
<option value="11">November</option>
<option value="">Month</option>
<option value="12">December</option>
<option value="">Month</option>
</select>
how can I add this Month before January and not after every month?
Thanks!
Try using this pattern:
/name="month">/
Code:
$selectMonth = preg_replace('/name="month">/', 'name="month"><option value="">Month</option>', Form::selectMonth('month'));
This should insert it only at the very top of your list.
Try this code:
Form::macro('selectMonths', function($name, $selected = null, $options = array())
{
$selectMonth = preg_replace('/></', '><option value="">Month</option><', Form::selectMonth($name, $selected, $options), 1);
return $selectMonth;
});
Form::macro('selectYears', function($name, $begin, $end, $selected = null, $options = array())
{
$selectYear = preg_replace('/></', '><option value="">Year</option><', Form::selectYear($name, $begin, $end, $selected, $options), 1);
return $selectYear;
});
Are you just trying to get a list of years? If so, you don't need a custom Macro. Laravel has you covered:
{{ Form::selectYear('year', 2013, 2015) }}
And for the month:
{{ Form::selectMonth('month') }}

Categories