Creating combobox and checkboxes with jQuery and CodeIgniter - php

I have combobox named catid, which is filled with categories. On change, I am filling the content on other
2 comboboxes and I am adding checkboxes in my form, which are all based on selected category.
So three comboboxes in total, categories (#catid), subcategories (#catid2) and manufacturers (#manufacturerid)
in this order.
When there are no subcategories, everything is nice and smooth. In the subcategory combobox, i get the message
there are no subcategories, i am filling the manufacturers combobox with manufacturers from that category,
and i am drawing the comboboxes again related to this category.
Problem arise when sub category exist. I am trying using on change event to fetch my checkboxes, related to that subcategory.
I get undefined value.
Here is my code:
Controller:
public function __construct() {
parent::__construct();
$this->load->model('addsCategoriesModel');
$this->load->model('addsTypesModel');
$this->load->model('colorsModel');
$this->load->model('geographicAreasModel');
$this->load->model('categoriesFiltersModel');
}
// function index has been ommited for the sake of simplicity
function get_dropdown_values($catid){
$this->load->model('manufacturersModel');
$this->db->select('manufacturerid,manufacturername');
$this->db->where("catid = $catid");
$result = $this->manufacturersModel->get();
//echo form_dropdown('manufactirerid', $result, NULL, 'id="manufacturerid"');
echo "<select name='manufacturerid' id='manufacturerid' class='styled_select'>";
//"<option value=".$result[]['manufacturerid'].">".$result[]['manufacturername']
foreach ($result as $key=>$value) {
echo "<option value=".$key['manufacturerid'].">".$value['manufacturername']."</option>";
}
echo "</select>";
}
function get_dropdown_values2($catid){
// create the second drop down, where parent id is the selected category
$this->db->select('catid,parentid,categoryname');
$this->db->where("parentid = $catid");
$result2 = $this->addsCategoriesModel->get();
if(count($result2) > 0){
echo "<select name='catid2' id='catid2' class='styled_select'>";
foreach ($result2 as $key=>$value) {
echo "<option value=".$key['catid'].">".$value['categoryname']."</option>";
}
echo "</select>";
} else {
echo "<select name='catid2' id='catid2' class='styled_select'>";
echo "<option value='0'>No subcategories</option>";
echo "</select>";
}
}
function get_categories_filters($catid3){
// create the checkboxes according to selected category
// case 1: if the category id is NOT 4
$this->db->where("catid = $catid3");
$result3 = $this->categoriesFiltersModel->get();
if(count($result3) > 0){
// start the foreach loop
foreach($result3 as $key=>$value){
echo "<div class='chb_group'>";
echo "<span class='custom_chb_wrapper'>";
echo "<input type='checkbox' name=" .$value['filterid'] ." class='zcheckbox'/>";
echo "<label>" .$value['filtername']. "</label>";
echo "</span>";
echo "</div>";
}
} else {
$result3 = "No checkboxes in this category";
echo $result3;
//$this->get_categories_filters2(22);
}
}
function get_categories_filters2($catid4){
// create the checkboxes according to selected category
$this->db->where("catid = $catid4");
$result4 = $this->categoriesFiltersModel->get();
if(count($result4) > 0){
// start the foreach loop
foreach($result4 as $key=>$value){
echo "<div class='chb_group'>";
echo "<span class='custom_chb_wrapper'>";
echo "<input type='checkbox' name=" .$value['filterid'] ." class='zcheckbox'/>";
echo "<label>" .$value['filtername']. "</label>";
echo "</span>";
echo "</div>";
}
} else {
//$result4 = "No checkboxes in this category";
$result4 = "Are you in filters2??";
echo $result4;
}
}
}
Here is my view:
<select name="catid" id="catid" class="styled_select" onchange="load_dropdown_content($('#manufacturerid'), this.value);load_dropdown_content2($('#catid2'), this.value);load_dropdown_content3($('#catid3'), this.value);">
<option value="0">Please select</option>
<?php
// foreach loop to fetch the categories
foreach ($adds_categories as $category){
echo "<option value=".$category['catid'].">".$category['categoryname']."</option>";
}
?>
</select>
</div>
<div class="select_wrapper">
<label><strong>Subcategory: </strong></label>
<select name="catid2" id="catid2" class="styled_select" onchange="load_dropdown_content4($('#catid3'), this.value);">
<option value="0">Please select</option>
</select>
</div>
For the sake of simplicity, I am not showing here the manufactirer combobox, with which i have no problem whatsoever and the checkboxes
And the javascript:
<script type="text/javascript">
function load_dropdown_content(field_dropdown, selected_value){
var result = $.ajax({
'url' : '<?php echo site_url('submit/get_dropdown_values'); ?>/' + selected_value,
'async' : false
}).responseText;
field_dropdown.replaceWith(result);
}
function load_dropdown_content2(field_dropdown, selected_value){
var result2 = $.ajax({
'url' : '<?php echo site_url('submit/get_dropdown_values2'); ?>/' + selected_value,
'async' : false
}).responseText;
field_dropdown.replaceWith(result2);
}
function load_dropdown_content3(field_dropdown, selected_value){
if(selected_value == 4){
load_dropdown_content4();
}
var result3 = $.ajax({
'url' : '<?php echo site_url('submit/get_categories_filters'); ?>/' + selected_value,
'async' : false
}).responseText;
$('#catid3').empty(); // DIV CATID3 IS THE DIV WHERE CHECKBOXES ARE CREATED
$("#catid3").append(result3);
}
function load_dropdown_content4(field_dropdown, selected_value){
var result4 = $.ajax({
'url' : '<?php echo site_url('submit/get_categories_filters2'); ?>/' + selected_value,
'async' : false
}).responseText;
$('#catid3').empty(); // DIV CATID3 IS THE DIV WHERE CHECKBOXES ARE CREATED
$("#catid3").append(result4);
alert(result4);
}
</script>
If anyone can help me to overcome that undefined error which I am getting from subcategories combobox, I will deeply appreciated.
Regards...

Instead of $("$cat3"), you can do like below also.
onchange = 'load_dropdown_content4("cat3",this.value)'
In load_dropdown_content4
function load_dropdown_content4(field_dropdown, selected_value)
{
//replace field_dropdown.replaceWith(result2);
$("#"+field_dropdown).replaceWith(result2);
}
Try this code.

Related

Disable select option when other specifig value is selected

I have a question by these given information:
private function loadIntervallOptions($intervallid = 1, $intervallfactor=1){
echo "<select name='intervallfactor'>";
for($intervallnumber=1;$intervallnumber<10;$intervallnumber++){
$checked = "";
if($intervallnumber== $intervallfactor){
$checked = "selected";
}
echo "<option $checked>$intervallnumber</option>";
}
echo "</select>";
echo "<select name='intervallid'>";
for($i=0;$i<count($this->todo_intervall);$i++){
echo "<option value='{$this->todo_intervall[$i]->getintervallid()}' ";
if($this->todo_intervall[$i]->getIntervallid() == $intervallid){
echo "selected";
}
echo ">{$this->todo_intervall[$i]->getIntervall()}</option>";
}
echo "</select>";
}
What I want to do:
If intervallid=1 I want to hide the select 'intervallfactor'. Is this possible?
Thank you in advance!
EDIT:
I'm trying this but it won't work
$('.intervallid').change(function(e) {
if ($('.intervallid').val()==1){
$('.intervallfactor').attr('disabled','disabled');
}
else{
$('.intervallfactor').removeAttr('disabled');
}
});
$('.intervallid').trigger('change');
You are trying to reference the html element using a class name .intervallid which doesnt exist.
either you should use $('select[name="intervalid"]) or add an id to the select from php and, use that to reference the element.
PHP
echo "<select name='intervallid' id='intervall_id'>";
JS
$('#intervall_id').attr('disabled', 'true')

Check chekbox when value of another element is changed

I want to check checkbox when value of "select" is changed. No idea what is wrong with this code, maybe you will be able to see the mistake:
var selected; var div_id;
$(".live_editor_test").focus(function() {
selected = $(this).val();
div_id = $(this).closest("div").attr('id');
});
$(".live_editor_test").blur(function() {
if (selected != $(this).val()) {
$('#live-assigning_check_' + div_id).attr('checked', true);
}
});
live_editor_test is the class of the select.
div is the row element and parent of live_editor_test.
live-assigning_check is the checkbox id.
div_id increments as there is one row for each mySql query.
Thanks in advance.
.php :
echo "<span class='content-row live-row-8' style=\"$colorcov\">";
echo "<select class=\"live_editor_test\" name=\"live_editor[$i]\" style=\"width:65px;font-size:10px;\">";
echo "<option value=\"\" selected></option>";
foreach ($storage_access->userAuth($row1, $where) as $row45)
{
if ($row45->match_id > '0') { $selected = "selected"; } else { $selected = ""; }
echo "<option value=\"$row45->user_id\" $selected>$row45->user_name</option>";
}
echo "</select>";
echo "</span>";
echo "<span style=\"width:20px;display:table-cell;\"><input class='checkbox-row' id='live-assigning_check_$i' type='checkbox' name='checked[$i]' ></span>";
Use .prop() instead of .attr(). Also use change event for select box.
Note: assuming that '#live-assigning_check_' + div_id evaluates to valid checkbox id.
$(".live_editor_test").change(function() {
if (selected != $(this).val()) {
$('#live-assigning_check_' + div_id).prop('checked', true);
}
});

PHP dropdown default value

I have this code,How would you set a default selection in this dropdown menu.
<tr><td class="tdt">
<?php te('Hypervisor');?>:</td> <td title='Add more from Hypervisor menu'>
<select validate='required:true' class='mandatory' name='hyp'>
<option value=''>Select</option>
<?php
foreach ($hyper as $a) {
$dbid=$a['id'];
$atype=$a['typedesc']; $s="";
if (isset($hyp) && $hyp==$a['id']) $s=" SELECTED ";
echo "<option $s value='$dbid' title='$dbid'>$atype</option>\n";
}
echo "</select>\n";
?>
Thanks for your help.
UPDATE:
I have a table connected to this code called hypervisors.in that table theres id field and typedesc field.I'd like to set "ESXi" as the default value which is represented by an ID num 1.
Update 2:
$sql="SELECT id,typedesc FROM hypervisors";
$sth=db_execute($dbh,$sql);
while ($r=$sth->fetch(PDO::FETCH_ASSOC))
$hyper[$r['id']]=$r;
that is SQL query
use this:
foreach ($hyper as $a) {
$dbid=$a['id'];
$atype=$a['typedesc'];
if (isset($hyp) && $hyp==$dbid) {
echo "<option selected='selected' value='$dbid'>$atype</option>\n";
} else {
echo "<option value='$dbid'>$atype</option>\n";
}
}
With selected within the option tag, you set the selected default value of your dropdown
http://www.w3schools.com/tags/att_option_selected.asp
if (isset($hyp) && $hyp==$a['id']){
$s=" SELECTED ";
echo "<option $s value='$dbid' selected>$atype</option>\n";
}
else {
echo "<option value='$dbid'>$atype</option>\n";
}

PHP selection based on previous selection

I have 2 functions in PHP, one of them displays the ISBN and the title of a book and the other displays the editions that exist in the database based on the ISBN selected on the previous selection.
Here are the 2 functions:
ISBN - Book dropdown list:
<?php include ("includes/connections.php");
function dropdown($intIdField, $strNameField, $strTableName, $strOrderField, $strNameOrdinal, $strMethod="asc") {
echo "<select name=\"$strNameOrdinal\" onchange=\"selection($id)\">\n";
echo "<option value=\"NULL\">Select Value</option>\n";
$strQuery = "select $intIdField, $strNameField
from $strTableName
order by $strOrderField $strMethod";
$rsrcResult = mysql_query($strQuery);
while($arrayRow = mysql_fetch_assoc($rsrcResult)) {
$strA = $arrayRow["$intIdField"];
$strB = $arrayRow["$intIdField"] . " - " . $arrayRow["$strNameField"];
echo "<option value=\"$strA\">$strB</option>\n";
}
echo "</select>";
}
?>
Edition dropdown list:
<?php include ("includes/connections.php");
function dropdownEdition($intId1Field, $intId2Field, $strTableName, $strOrderField, $strNameOrdinal, $strMethod="asc") {
$intId2Field = $GLOBALS['book'];
var_dump($intId2Field);
var_dump($_POST["book"]);
echo "<select name=\"$strNameOrdinal\">\n";
echo "<option value=\"NULL\">Select Value</option>\n";
$strQuery = "SELECT $intId1Field, $intId2Field
FROM $strTableName
ORDER BY $strOrderField $strMethod";
$rsrcResult = mysql_query($strQuery);
while($arrayRow = mysql_fetch_assoc($rsrcResult)) {
$strA = $arrayRow["$intId1Field"];
echo "<option value=\"$strA\">$strA</option>\n";
}
echo "</select>";
}
?>
What I have been trying to do is pass the ISBN selected on the previous selection with a onchange function which would return the ISBN of the book but it failed a lot.
<?php
function selection($id){
echo $id;
}
?>
I know I'm terrible at this but I don't know what else to do if you could point me to a direction it would be much appreciated.
I would prefer a PHP solution rather than a JavaScript one if possible.
You are trying to call a PHP function via the onchange event.
You'll need to write a JavaScript function to make an AJAX call to the PHP file in order to get the result.
You can easily make the AJAX request with jQuery
$.get({
url: "/selection.php",
data: {
id: "ISBN HERE"
},
success: function(data) {
alert(data)
}
})
You would also have to add something like this to the PHP file to display the result
echo selection($_GET["id"]);
The first page, which has the list of books and a Go button
<form action="page2.php" method="get">
<!-- book selection list -->
<input type="submit" value="Go">
</form>
The second page could list all the editions for the book with the ID $_GET[$strNameOrdinal]
To do this on the same page, with a reload, you need to check if the form has been submitted
<?php
if (empty($_GET[$strNameOrdinal])) {
// List Books
} else {
// List editions
}
?>

Option on dropdown as anchor

I am pulling out record from the database and inserting them inside a dropdown like this:
echo "<select>";
while ($drow = mysql_fetch_assoc($request))
{
echo "<option>" . $drow['id'] . "</option>";
}
echo "</select>";
It works but I need to be able to click on an option on the dropdown and make it link just like:
Record1Here
Record2Here
Record3Here
UPDATE: Latest code:
<script>
function doSomething() {
var currentval = this.options[this.selectedIndex].value;
// you could navigate away at that point ?
window.location = currentval;
}
</script>
...
echo "<select onchange='doSomething();'>";
while ($drow = mysql_fetch_assoc($request))
{
echo "<option value=\"view.php\">" . $drow['id'] . "</option>";
}
echo "</select>";
You can't place anchors on an option within a select list.
What you can do is use JavaScript and then do something on the change event of the select list :
echo "<select onchange='doSomething(this);>';
then in JavaScript do something based on the selected value :
function doSomething(elem) {
var currentval = elem.options[elem.selectedIndex].value;
// you could navigate away at that point ?
window.location = currentval;
}
Example here
you could update your PHP code to include a value in each option :
echo "<option value=\"urlhere.php\">" . $drow['id'] . "</option>";

Categories