Check if radio button value is, then echo some HTML - javascript

I want to check if the value of the select field is some value and if it is, it needs to echo some text.
I use this code for input box in the form:
<li>
<div class="input-box">
<strong><?php echo $this->__('Would you recommend this product to a friend?') ?></strong>
<?php foreach ( $this->getOptions() as $option ): ?>
<label class="recommend">
<input type="radio" name="recommend" id="recommend_field
<?php echo $option['value'] ?>" class="radio-gender" value="
<?php echo $option['value'] ?>"
<?php if ($option['value'] == $value)
echo ' checked="checked"' ?> >
<?php echo $this->__($option['label']) ?></input>
</label>
<?php endforeach ?>
</div>
</li>
And I currently echo the entire value of the input box with this line:
<div id="reviewwriter">
<span class="recommendation">
<?php echo $this->getAnswer() ?>
</span>
</div>
Code is loaded by this php:
public function confRecommendItemsArray()
{
$resArray = array();
if (Mage::getStoreConfig('advancedreviews/recommend_options/recommend_field1')) {
$resArray[] = array(
'value' => 1,
'label' => Mage::getStoreConfig('advancedreviews/recommend_options/recommend_field1')
);
}
if (Mage::getStoreConfig('advancedreviews/recommend_options/recommend_field2')) {
$resArray[] = array(
'value' => 2,
'label' => Mage::getStoreConfig('advancedreviews/recommend_options/recommend_field2')
);
}
And
class AW_AdvancedReviews_Block_Recommend_Field extends Mage_Core_Block_Template
{
public function canShow()
{
return (Mage::helper('advancedreviews')->confShowRecommend()
&& count(Mage::helper('advancedreviews')->confRecommendItemsArray()));
}
public function getOptions()
{
return Mage::helper('advancedreviews')->confRecommendItemsArray();
}
}
The values of the select field are
1. Yes
2. No
I want to check if value is Yes and if so echo 'Value is Yes'.
And if value is No than echo ''.
See also this JSFiddle: http://jsfiddle.net/wL3xu9d7/1/
But I do not know why it is not working.
How can I achieve that?

i hope this solution you want...
<li>
<div class="input-box">
<strong><?php echo $this->__('Would you recommend this product to a friend?') ?></strong>
<?php foreach ( $this->getOptions() as $option ): ?>
<label class="recommend">
<input type="radio" name="recommend" id="recommend_field<?php echo $option['value'] ?>" class="radio-gender" value="<?php echo $option['value'] ?>"<?php if ($option['value'] == $value) echo ' checked="checked"' ?>><?php echo $this->__($option['label']) ?></input>
</label>
<?php endforeach ?>
</div>
</li>
add hidden field on answer
<div id="reviewwriter">
<span class="recommendation" id="reviewwriteranswer">
<?php echo $this->getAnswer() ?>
</span>
</div>
<script>
$$(".radio-gender").each(function(el) {
el.observe("click", function(event) {
if(el.checked)
{
sub = $('reviewwriteranswer').value;
sub ==sub =.trim();
if(el.value==sub)
{
$('reviewwriteranswer').update('value is yes');
}else {
$('reviewwriteranswer').update('value is No');
}
}
});
});
</script>
<scrip>
var allElements = document.body.getElementsByTagName("*");
for(var i = 0; i < allElements.length; i++) {
var text = allElements[i].innerHTML;
text=text.trim();
if (text == 'Yes') {
allElements[i].innerHTML = "Value is Yes";
}
if (text == 'No') {
allElements[i].innerHTML = "Value is No";
}
}
</scrip>

Related

To display data stored on the dropdown menu

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>

Hide and Display activity checkbox when click on services checkbox

PHP
<?php
//for service 1
$all_activities = "select * from activity join displayserviceactivitymap on activity.activity_id = displayserviceactivitymap.activity_id right join services on services.service_id = displayserviceactivitymap.service_id";
$all_activities = $conn->query($all_activities) or die ($conn>error.__LINE__);
$activities = [];
while ($row = $all_activities->fetch_assoc()) {
$activities[] = $row;
}
$repeated = 'repeated';
foreach ($activities as $act) {
if($act['servicename'] != $repeated){
echo '<br><input type="checkbox" name="arr['.$act['service_id'].'][service]" value="'.$act['service_id'].'" id="'.$act['service_id'].'">'.$act['service_id'].$act['servicename'].'<br>';
$repeated = $act['servicename'];
}
if($act['activity_id'] != '')
echo '<input type="checkbox" name="arr['.$act['service_id'].'][activity][]" value="'.$act['activity_id'].'" id="'.$act['activity_id'].'">'.$act['nameofactivity'].'<br>';
}
?>
Output of My code is
Incometax
Return
filling
GST
Form
Return
GSTR
TDS
Application
Refund
Here Incometax,GST,TDS are services and others are related activity of the services.
I want to display activity when I check checkbox and hide when I uncheck checkbox.
Please try to run this whole code for better understand
<?php
function array_group(array $data, $by_column) {
$result = [];
foreach ($data as $item) {
$column = $item[$by_column];
unset($item[$by_column]);
if (isset($result[$column])) {
$result[$column][] = $item;
} else {
$result[$column] = array($item);
}
}
return $result;
}
$activities = array(
array('service_id' => '1',
'servicename' => 'Incometax',
'activity_id' => '1',
'nameofactivity' => 'Return'),
array('service_id' => '1',
'servicename' => 'Incometax',
'activity_id' => '2',
'nameofactivity' => 'filling')
);
$activities = array_group($activities, 'servicename');
?>
<link href = "https://code.jquery.com/ui/1.12.0/themes/smoothness/jquery-ui.css" rel = "stylesheet" />
<script src = "https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>
<?php foreach ($activities as $key => $act) { ?>
<div id="accordion">
<h3>
<label for='product-44-44'>
<input type='checkbox' name="arr[<?php echo $act[0]['service_id']; ?>][service]" value="<?php echo $act[0]['service_id']; ?>" id="<?php echo $act[0]['service_id']; ?>"/>
<?php echo $key; ?>
</label>
</h3>
<div class="columns">
<?php foreach ($act as $value) { ?>
<div class="d1">
<label>
<input type="checkbox" name="arr[<?php echo $value['service_id']; ?>][activity][]" value="<?php echo $value['activity_id']; ?>" id="<?php echo $value['activity_id']; ?>">
<?php echo $value['nameofactivity']; ?>
</label>
</div>
<?php } ?>
</div>
</div>
<?php } ?>
<script>
$("#accordion")
.accordion({
collapsible: true,
active: false,
heightStyle: "content",
beforeActivate: function (event, ui) {
var oldInput = ui.oldHeader.find('input');
oldInput.prop('checked', !oldInput.prop('checked')) // this bit unchecks when the accordion is closed
var newInput = ui.newHeader.find('input');
// this bit checks when the accordion is opened
newInput.prop('checked', !newInput.prop('checked'))
}
}
);
</script>

codeigniter 3rd combo box does not display data

This is my Model
function getMatkul($dosen) {
$data = array();
$query = $this->db->get_where('input_jadwal', array('dosen' => $dosen));
if ($query->num_rows() > 0) {
foreach ($query->result_array() as $row){
$data[] = $row;
}
}
$query->free_result();
return $data;
}
function getKelas($matkul) {
$data = array();
$query = $this->db->get_where('input_jadwal', array('kode_matkul'=>$matkul));
if ($query->num_rows() > 0) {
foreach ($query->result_array() as $row){
$data[] = $row;
}
}
$query->free_result();
return $data;
}
This is my View
<select name="dosen" id="dosen" class="form-control" required="">
<option disabled="" selected="">Dosen</option>
<?php foreach($dosen as $d){ ?>
<option value="<?php echo $d['dosen']; ?>"><?php echo $d['dosen']; ?></option>
<?php } ?>
</select>
</div>
<div class="col-md-12 form-group">
<select name="matkul" id="matkul" class="form-control" required="">
<option disabled="" selected="">Mata Kuliah</option>
<?php foreach($matkul as $m) {?>
<option value="<?php echo $m['kode_matkul']; ?>"><?php echo $m['matkul']; ?></option>
<?php } ?>
</select>
</div>
<div class="col-md-12 form-group">
<select name="kelas" id="kelas" class="form-control" required="">
<option disabled="" selected="">Kelas</option>
<?php foreach($kelas as $k) {?>
<option value="<?php echo $k['kelas']; ?>"><?php echo $k['kelas']; ?></option>
<?php } ?>
</select>
</div>
<div class="col-md-12 form-group">
<button name="mysubmit" class="btn btn-primary pull-left btn-flat" type="submit">Show Record</button>
</div>
</div>
</div>
</form>
</div>
</section>
<script type="text/javascript">
<?php
$this->load->model('combobox_model');
foreach($dosen as $dm) { ?>
var <?php echo str_replace(' ','',$dm['dosen'].$dm['dosen']); ?> = [
<?php $resultM = $this->combobox_model->getMatkul($dm['dosen']); ?>
<?php foreach ($resultM as $rm) { ?>
{display: "<?php echo $rm['matkul']; ?>", value: "<?php echo $rm['kode_matkul']; ?>" },
<?php } ?>];
<?php } ?>
$("#dosen").change(function() {
var parent = $(this).val();
switch(parent){
<?php foreach($dosen as $dd){ ?>
case '<?php echo $dd['dosen']; ?>':
lista(<?php echo str_replace(' ','',$dd['dosen'].$dd['dosen']); ?>);
break;
<?php } ?>
default: //default child option is blank
$("#matkul").html('');
break;
}
});
function lista(array_list)
{
$("#matkul").html(""); //reset child options
$(array_list).each(function (i) { //populate child options
$("#matkul").append("<option value=\""+array_list[i].value+"\">"+array_list[i].display+"</option>");
});
}
</script>
<script type="text/javascript">
<?php
$this->load->model('combobox_model');
foreach($matkul as $mk) { ?>
var <?php echo str_replace(' ','',$mk['matkul'].$mk['kode_matkul']); ?> = [
<?php $resultK = $this->combobox_model->getKelas($mk['kode_matkul']); ?>
<?php foreach ($resultK as $rk) { ?>
{display: "<?php echo $rk['kelas']; ?>", value: "<?php echo $rk['kelas']; ?>" },
<?php } ?>];
<?php } ?>
$("#matkul").change(function() {
var parent = $(this).val();
switch(parent){
<?php foreach($matkul as $tt){ ?>
case '<?php echo $tt['kode_matkul']; ?>':
listb(<?php echo str_replace(' ','',$tt['matkul'].$tt['kode_matkul']); ?>);
break;
<?php } ?>
default: //default child option is blank
$("#kelas").html('');
break;
}
});
function listb(array_list)
{
$("#kelas").html(""); //reset child options
$(array_list).each(function (i) { //populate child options
$("#kelas").append("<option value=\""+array_list[i].value+"\">"+array_list[i].display+"</option>");
});
}
</script>
Combobox 1 linked to combobox 2, combobox 2 linked to combobox 3. when When I run it, only combo box 1 and 2 which have the data. I think the problem is on javascript. Need help, thank you. This is the pic of my combobox

post jquery array to php page when checking checkboxes

i have dynamic number of checkboxes and a button to post the checked elements to another page .. here is the code:
<div class="overSelect"></div>
<div id="checkboxes<?php echo $aa['id']; ?>">
<?php
$pid = select::property($aa['name']);
$arr2 = select::properties($pid);
$cc = 1;
foreach ($arr2 as $aa2) { ?>
<div style="padding-right: 20px" class="row"><label>
<input type="checkbox" value="<?php echo $aa2['name'] ?>" name="checks<?php echo $aa['id'] ?>[]"id="check<?php echo $aa['id']; ?>"><?php echo $aa2['name'] ?>
</label></div>
<?php } ?>
</div>
<a class="btn btn-primary btn-sm" name="update-all"onclick="update_all('prop_id<?php echo $aa['id']; ?><?php $counter; ?>','checks<?php echo $aa['id'] ?>')"id="btn-all<?php echo $aa['id']; ?><?php $counter; ?><?php echo $p['id']; ?>">save</a>
i tried this code but it is not working
<script type="text/javascript">
function update_all(a, b) {
var aa = document.getElementById(a).value;
var values = new Array();
$("input:checkbox[name=b]").each(function () {
if ($(this).is(":checked")){
values.push($(this).val());
}
});
alert(aa);
alert(values.length);
alert(values);
}
</script>
if the checkboxes checked .. it's value will be added to the array and posted to php page
any help please .. thanks in advance

JQuery checkbox when value is assigned

I've been trying to get JQuery to check a radio for me when I assign a value 'Z' to it.
My current code:
<?php if ($option['type'] == 'radio') { ?>
<div class="form-group<?php echo ($option['required'] ? ' required' : ''); ?>">
<label class="control-label"><?php echo $option['name']; ?></label>
<div id="input-option<?php echo $option['product_option_id']; ?>">
<?php if (isset($option_values)) foreach ($option_values as $option_value) { ?>
<div class="radio">
<label>
<input type="radio" name="option[<?php echo $option['product_option_id']; ?>]" value="<?php echo $option_value['product_option_value_id']; ?>" />
<?php echo $option_value['name']; ?>
<?php if ($option_value['price']) { ?>
<?php if ($option_value['price_prefix'] == 'z') {?>
<script>
$(".radio").prop("checked", true)
</script>
<?php echo 'Im Active!!'; ?><?php }
else { ?> <?php echo ' ';?> (<?php echo $option_value['price_prefix'];?>
<?php echo $option_value['price']; ?>) <?PHP }?>
<?php } ?>
</label>
</div>
<?php } ?>
</div>
</div>
<?php } ?>
<?php } ?>
As for now, my option 'Z' only shows 'I'm Active!!' in the list. The radio is still unchecked though.
What am I doing wrong?
I'm planning on releasing a free OpenCart module where people can activate an option by standard by assigning the value Z in product properties.
Thanks!

Categories