So, I am fairly new to codeignitor and was hoping to get some help. Below I have my controller(Estimates.php), my model(Invoice_items_model.php) and my view(_add_edit_items.php). These are shortened versions as the full versions are pretty long.
What I am looking to do is have it so that when one of the select options is clicked it will then check the $suboption['non_compatible'] field for that suboption. If the suboption in any of the other select options matches that non_compatible field it will be disabled.
I found this post that I could probably work with but i'm not sure how to do an onchange event for dynamically generated selects.
Estimates.php
`public function estimate($id = ''){$data['platforms'] = $this->invoice_items_model->get_platforms();
$data['options'] = $this->invoice_items_model->get_options();
$data['suboptions'] = $this->invoice_items_model->get_suboptions();
$this->load->view('admin/estimates/_add_edit_items', $data);}`
Invoice_items_model.php
public function get_platforms()
{
$platforms = array();
$this->db->order_by('name', 'asc');
return $this->db->get('tblplatforms')->result_array();
}
public function get_options()
{
$options = array();
return $this->db->get('tblplatform_options')->result_array();
}
public function get_suboptions()
{
$suboptions = array();
return $this->db->get('tblplatform_suboptions')->result_array();
}
}'
_add_edit_items.php
<?php foreach($options as $option) { ?>
<tr>
<td></td>
<td><p><?php echo $option['name']; ;?>:</p></td>
<td>
<select name="<?php echo $option['name']; ?>" id="<?php echo $option['name']; ?>" rows="4" class="form-control">
<?php foreach($suboptions as $suboption) { ?>
<?php if($suboption['plat_option'] == $option['name']) { ?>
<option value="<?php echo $suboption['name']; ?>" id="<?php echo $suboption['name']; ?>"><?php echo $suboption['name']; ?></option>
<?php } ?>
<?php } ?>
</select>
</td>
</tr>
<?php } ?>
<tr>
keep your code as it is just change the event like this.
$(document).on("onChange", "your_selector", function() {
// your code
}
Related
I have a problem for the results to appear in my dropdown menu. For the dropdown menu I don't directly choose the type that matches the id that appears.
How to make the dropdown menu to appear according to the item ID?
This View.
label for="type">Type</label>
<select class="form-control" id="type_id" name="type_id">
<?php foreach($type as $types) : ?>
<?php if( $types == $barang['type_id']) : ?>
echo '<option value="<?php echo "$types->type_id"?>" selected><?php echo "$types->type_nama"?></option>';
<?php else : ?>
echo '<option value="<?php echo "$types->type_id"?>"><?php echo "$types->type_nama"?></option>';
<?php endif; ?>
<?php endforeach; ?>
</select>
This Model.
public function getTypeQuery()
{
$this->db->order_by('type_id', 'asc');
$query = $this->db->get('type');
return $query->result();
}
public function getTypeById($id)
{
return $this->db->get_where('barang', ['barang_id' => $id])->row_array();
}
public function ubahTypeBarang ()
{
$data = [
"barang_kode"=> $this->input->post('barang_kode', true),
"type_id"=> $this->input->post('type_id', true),
"barang_nama"=> $this->input->post('barang_nama', true),
];
$this->db->where('barang_id', $this->input->post('barang_id'));
$this->db->update('barang', $data);
}
This Controller.
public function ubah_type($id)
{
$data['judul'] = 'Form Ubah Data Mahasiswa';
$data['barang'] = $this->Gudang_model->getTypeById($id);
$data['type'] = $this->Gudang_model->getTypeQuery();
$this->form_validation->set_rules('barang_kode', 'Kode', 'required');
$this->form_validation->set_rules('type_id', 'Type', 'required');
$this->form_validation->set_rules('barang_nama', 'Nama Barang', 'required');
if($this->form_validation->run() == FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('gudang/ubah_type', $data);
$this->load->view('templates/footer');
} else {
$this->Gudang_model->ubahTypeBarang();
$this->session->set_flashdata('flash', 'Diubah');
redirect('gudang/lihat_type');
}
}
`
Try change the views like this :
<label for="type">Type</label>
<select class="form-control" id="type_id" name="type_id">
<?php foreach($type as $types) : ?>
<?php if( $types->type_id == $barang['type_id']) : ?>
<option value="<?php echo $types->type_id ?>" selected><?php echo $types->type_nama ?></option>
<?php else : ?>
<option value="<?php echo $types->type_id ?>"><?php echo $types->type_nama ?></option>
<?php endif; ?>
<?php endforeach; ?>
</select>
Hi guys I'm having a hard time with this one please help me.
So I have an add row function where I can add dynamic rows to the form. My plan is to get the value of the Particulars with onchange event. So when you change the particular if will get the current value of the selected Particulars specifically on the selected row only.
The form looks like this:
So to test this out I'm trying to put the value in alert messages.
So this is the one I've done so far, I don't know if I'm making this correctly.
This is my html code.
<table class="table table-bordered" id="addSubPaymentTable">
<thead>
<tr>
<th >Particulars</th>
<th style="width:10%;"><button type="button" class="btn btn-success btn-sm" onclick="addPaymentRow()">Add Row</button></th>
</tr>
</thead>
<tbody>
<?php $arrayNumber = 0; for($x = 1; $x < 2; $x++) { ?>
<tr id="row<?php echo $x; ?>" class="<?php echo $arrayNumber; ?>">
<td class="form-group">
<select class="form-control particulars" name="subparticulars[<?php echo $x; ?>]" id="subparticulars<?php echo $x; ?>"
onchange="specificBalance('<?php echo $x; ?>')" /> //AS YOU CAN SEE I HAVE AN ONCHANGE EVENT HERE
<option value="">Select Particulars</option>
<?php foreach ($particularsData as $particulars)
{
?>
<option value="<?php echo $particulars['feetype_id']; ?>"> <?php echo $particulars['feetype_name']; ?> </option>
<?php
}
?>
</select>
<?php $arrayNumber++; } // /.foreach?>
</tbody>
So this is the function for the onchange event of Specific Balance
Javascript
// Get Specific Balance
function specificBalance(row = null)
{
var particular = $(this).val();
alert('Testing value is' +particular );
}
Please help me to get the value of the Particular, that is different in every row.
replace this line
<select class="form-control particulars" name="subparticulars[<?php echo $x; ?>]" id="subparticulars<?php echo $x; ?>"
onchange="specificBalance('')" />
with
<select class="form-control particulars" name="subparticulars[<?php echo $x; ?>]" id="subparticulars<?php echo $x; ?>"
onchange="specificBalance(this)" />
and in the jquery function use this
function specificBalance(row = null)
{
var particular = $(row).val();
alert('Testing value is' +particular );
}
Use this.value in onchange() function for select tag in HTML
<select class="form-control particulars" name="subparticulars[<?php echo $x; ?>]" id="subparticulars<?php echo $x; ?>"
onchange="specificBalance(this.value)" /> //AS YOU CAN SEE I HAVE AN ONCHANGE EVENT HERE
<option value="">Select Particulars</option>
<?php foreach ($particularsData as $particulars)
{
?>
<option value="<?php echo $particulars['feetype_id']; ?>"> <?php echo $particulars['feetype_name']; ?> </option>
<?php
}
?>
</select>
I'm developing an app that have a form that contains two selects populated dynamically with arrays.
The second select is disabled if there isn't a choice in the first of them and i need to filter the options of the second select respect the choice of the first.
Whereas I must to pass these values to another PHP page I mustn't do the filter using the value attribute.
I've tried to use a custom attribute but without results.
This is my code,
<tr>
<td style="color: white;">Seleziona data: </td>
<td>
<select class="form-control" name="data" id="select1">
<option value="prova" disabled selected> Scegli</option>
<?php for ($y=0; $y<count($giorni); $y++){ $giorno=$ giorni[$y];?>
<option value="<?php echo $giorno; ?>" id="<?php echo $y; ?>">
<?php echo $giorno; ?> </option>
<?php } ?>
</select>
</td>
</tr>
<tr>
<td style="color: white;">Seleziona ora: </td>
<td>
<select class="form-control" name="ora" id="select2" disabled>
<?php $count=1 ; while ($count <=3 ){ if ($count==1 ){ for ($g=0; $g<count($ora1); $g++){ ?>
<option id="<?php echo $count-1; ?>" value="<?php echo $ora1[$g]; ?>">
<?php echo $ora1[$g]; ?>
</option>
<?php } $count ++; } ?>
<?php if ($count==2 ){ for ($g=0; $g<count($ora2); $g++){ ?>
<option id="<?php echo $count-1; ?>" value="<?php echo $ora2[$g]; ?>">
<?php echo $ora2[$g]; ?>
</option>
<?php } $count ++; } ?>
<?php if ($count==3 ){ for ($g=0; $g<count($ora2); $g++){ ?>
<option id="<?php echo $count-1; ?>" value="<?php echo $ora3[$g]; ?>">
<?php echo $ora3[$g]; ?>
</option>
<?php } $count ++; } } ?>
</select>
</td>
</tr>
JAVASCRIPT:
<script>
document.getElementById('select1').onchange = function() {
document.getElementById('select2').disabled = false;
};
</script>
<script>
$("#select1").change(function() {
if ($(this).data('options') == undefined) {
/*Taking an array of all options-2 and kind of embedding it on the select1*/
$(this).data('options', $('#select2 option').clone());
}
var id = $(this).id();
var options = $(this).data('options').filter('[id=' + id + ']');
$('#select2').html(options);
});
</script>
How can I solve it?
Thanks
There is an issue with your html. There is more than one option with an identical id. On a html document, ids must be unique.
For the same reason, you cannot filter your options on the id value.
What you should do is to use a data- attribute instead the id attribute. This will allow you to filter on it.
You could use the json_encode function to generate JSON objects in your PHP code and create the select2 options dynamically on the client side.
<tr>
<td style="color: white;">Seleziona data: </td>
<td>
<select class="form-control" name="data" id="select1">
<option value="prova" disabled selected> Scegli</option>
<?php
foreach ($giorni as $key => $value) {
?>
<option value="<?php echo $value; ?>" id="<?php echo $key; ?>" ><?php echo $value; ?></option>
<?php
}
?>
</select>
</td>
</tr>
<tr>
<td style="color: white;">Seleziona ora: </td>
<td>
<select class="form-control" name="ora" id="select2" disabled></select>
</td>
</tr>
<script>
// the options object contains the possible option values for select2
// it can be accessed as an associative array using the selected value from select1
var options = {
"0" : <php echo json_encode($ora1, JSON_FORCE_OBJECT) ?>,
"1" : <php echo json_encode($ora2, JSON_FORCE_OBJECT) ?>,
"2" : <php echo json_encode($ora3, JSON_FORCE_OBJECT) ?>
};
// a helper variable to prevent unnecessary work
var prevSelectedValue = null;
// onChange handler for select1
document.getElementById('select1').onchange = function () {
var selectedValue = document.getElementById('select1').value;
if (selectedValue && (prevSelectedValue != selectedValue)) {
// select1 has changed so there is work to do
// 1 - get a handle on select2 to make code more legible
var select2 = document.getElementById('select2');
// 2 - remove all existing options
select2.options.length = 0;
// 3 - add new options
var obj = options[selectedValue];
// iterate through the object to get the keys and values
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
var newOption = document.createElement('option');
newOption.text = obj[key];
newOption.value = key;
select2.options.add(newOption);
}
}
// 4 - enable select2
select2.disabled = false;
// 5 - record select1 change in prevSelectedValue
prevSelectedValue = selectedValue;
}
};
</script>
I want to get the product id called "product_id" through a alert in javaScript. But it gives "undefined" as the alert. I am getting data from a database.
Here is my PHP code.
$jsql_ae7 = mysql_query("select request_list.product_id from request_list where request_list.product_id='{$jrowa2['id']}' and request_list.email='$visit_email'") or die(mysql_error());
$jfeta7 = mysql_fetch_assoc($jsql_ae7);
Here is my HTML code.
<div class="col-sm-2">
<select id="<?php echo $jfeta7['product_id']; ?>" name="aformats" onchange="showFormat(this);">
<option value="<?php echo $jrowa2['formats']; ?>"><?php echo $jrowa2['formats']; ?></option>
<?php foreach($formats3 as $v3){ ?>
<?php if($v3 !== $jrowa2['formats']) { ?>
<option value="<?php echo $v3; ?>"><?php echo $v3; ?></option>
<?php } ?>
<?php } ?>
</select>
</div>
Here is my javaScript code.
var showFormat = function(dd) {
var format_select_id = $(this);
var product_id = format_select_id.attr("id");
alert(product_id);
};
Here is a screenshot of my page.
change with this
var showFormat = function(dd) {
var product_id = dd.id;
alert(product_id);
};
Why not just pass the product id directly?
<select id="<?php echo $jfeta7['product_id']; ?>" name="aformats" onchange="showFormat(<?php echo $jfeta7['product_id']; ?>);">
and your showFormat()
function showFormat(dd) {
alert(dd);
};
I have a while in PHP that is building a table. Inside my while, I have a dropdown menu that I want to execute code on change.... The form submit on change doesn'T work...
This is my code:
<?
while ($data2 = mysql_fetch_array( $data))
{ ?>
<form name="FormSize" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" >
<td>
<select name="size[]" class="form-field3" OnChange="document.FormSize.submit();" >
<? foreach ($sizearray as $value) { ?>
<option value="$value" <? if($data2['size'] == $value) { echo "selected";} ?> ><? echo $value; ?></option>
<? } ?>
</select>
<input type="hidden" name="product_id[]" value="<? echo $data2['product_id']; ?>">
</td>
</form>
<? } ?>
and on top of my page, I have this to execute the code..
<?
//submit size form
if (isset($_POST['size']))
{
foreach($_POST['product_id'] as $key => $id)
{
$product_id = $id;
$newsize = $_POST['size'][$key];
$sql3 = mysql_query("update cart SET size = '".$newsize."' where product_id = '".$product_id."' ");
}
?>
any idea why it's not executing ?
You have multiple forms with the same name="FormSize". So document.FormSize will be an array-like object, and you need to access the appropriate one. You can do this by using this.form to refer to the <form> that contains the <select>.
You also need to fix the element nesting -- the <form> has to be inside the <td>, not the other way around.
<?
while ($data2 = mysql_fetch_array( $data))
{ ?>
<td>
<form name="FormSize" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" >
<select name="size[]" class="form-field3" OnChange="this.form.submit();" >
<? foreach ($sizearray as $value) { ?>
<option value="$value" <? if($data2['size'] == $value) { echo "selected";} ?> ><? echo $value; ?></option>
<? } ?>
</select>
<input type="hidden" name="product_id[]" value="<? echo $data2['product_id']; ?>">
</form>
/td>
<? } ?>
Could be because the html is not valid, you cannot put your form tag as a direct child of a tr.
Apart from that, naming all your forms the same will make it pretty hard to submit a specific one by its name.
As you are using arrays for your input names, I assume that you mean all of them to be in the same form, so you should just have your form wrap the table and take it out of the while loop.