change dropdown to textBox in Yii - javascript

I am stuck in a place in Yii. I have two drop Down box, second is dependent on the first. In this first drop Down, I have many options. By referring to these options, I must decide whether the second box must be a drop Down or a text Field.
I have uploaded My code here. Please help me solving this.
Thanks in advance.
My View:
<td>
<?php echo $form->labelEx($model,'cm_classification_id'); ?>
<?php echo $form->dropDownList($model,'cm_classification_id', CHtml::listData(masterClassification::model()->findAll(array('order' => 'cm_classification_id ASC', 'condition'=>'cm_classification_type=:type', 'params'=>array('type'=>'initiate'))), 'cm_classification_id', 'cm_classification_name'), array('empty'=>'Select classification')); ?>
<?php echo $form->error($model,'cm_classification_id'); ?>
</td>
<td>
<label>Change Description <span class="required" id="desc_req_note" style="display:none;">*</span></label>
<?php echo $form->dropDownList($model,'cm_description',array(),array('empty'=>'Select Change Description')); ?>
<?php echo $form->error($model,'cm_description'); ?>
</td>
based on the change classification, I must decide where Change Description must be a drop Down or a text field.
This must be done using Javascript.

Have you tried this -
Change the code to -
<label>Change Description <span class="required" id="desc_req_note" style="display:none;">*</span></label>
<span id="cm_desc_select" style="display:none;"><?php echo $form->dropDownList($model, 'cm_description', array(), array('empty'=>'Select Change Description')); ?></span>
<span id="cm_desc_input" style="display:none;"><?php echo $form->textField($model,'cm_description'); ?></span>
<?php echo $form->error($model,'cm_description'); ?>
2.
$('#cm_classification_id').change(function() {
var val = $(this).val();
var cm_desc_select_elem = $('#cm_desc_select');
var cm_desc_input_elem = $('#cm_desc_input');
if(val === COMPARE_WITH_YOUR_VALUE) {
$(cm_desc_select_elem).show();
} else {
$(cm_desc_input_elem).show();
}
});
I hope is helps for a quick fix.

Related

Javascript form select option onchange remove last style

I am trying to mark a table that the user selected in a form, but if he changes it while he still in the same form selection, I want the last marking disappear and the new one showing. However, I've tried several approaches and this one is not working, it won't mark anything but if I call only the select(x)function it marks every single one I choose without removing the last one. I'll be glad for some help or ideas for improving my code.
View Code
<select type="number" id="tnum2" name="num" style="display: none;" onchange="table_selection(this.value)">
<?php for ($i = 12; $i <= 23; $i++) : ?>
<option id="out" value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php endfor; ?></select>
JavaScript Code
function table_selection(x){
remove_selection(),
select(x);
}
function remove_selection(){
for(var i=2;i<34;i++){
document.getElementById(i).style.outline="none";
}
}
function select(x){
document.getElementById(x).style.outline="8px dotted #F2FF21";
}

Ajax fresh input field generated by PHP DOM

I am a student studying Ajax to change the value of an input field generated with PHP DOM.
Here is the code that generates the table rows.
<?php
$totalval = 0;
foreach($_SESSION as $name=>$value){
$total = 0;
if(substr($name,0,5) == "item_"){
$id = substr($name,5,4);
$cart_xml= new DomDocument;
$cart_xml->Load('prod_db.xml');
$root=$cart_xml->getElementsByTagName('root')->item(0);
$product=$root->getElementsByTagName('product');
foreach($product as $prod){
$itms_id=$prod->getElementsByTagName('prod_id')->item(0)->nodeValue;
$itms_categ=$prod->getElementsByTagName('prod_categ')->item(0)->nodeValue;
$itms_imgsrc=$prod->getElementsByTagName('prod_imgsrc')->item(0)->nodeValue;
$itms_name=$prod->getElementsByTagName('prod_name')->item(0)->nodeValue;
$itms_price=$prod->getElementsByTagName('prod_price')->item(0)->nodeValue;
$itms_stock=$prod->getElementsByTagName('prod_stock')->item(0)->nodeValue;
if($id==$itms_id){
$price = floatval($itms_price);
$total=$price*$value;
?>
<tr>
<td><img src="<?php echo $itms_imgsrc?>" class="citemimg"/></td>
<td>
<p class="prodname"><?php echo ucfirst($itms_name);?></p>
<p class="prodcateg"><?php echo ucfirst($itms_categ)?></p>
<p class="prodprice"><?php echo "Php. ".number_format($itms_price,2)?></p>
</td>
<td class="cartAmount;">
<img onclick="ajaxMinus('<?php echo $itms_id;?>','<?php echo $itms_stock;?>')" class="carticons" src="images/logos/Minus_r_50px.png"/>
<input type="number" min="1" max="<?php echo $itms_stock;?>" class="cartqty" id="itmQty<?php echo $itms_id;?>" value="<?php echo $value; ?>" disabled>
<img onclick="ajaxAdd('<?php echo $itms_id;?>','<?php echo $itms_stock;?>')" class="carticons" src="images/logos/Add_r_50px.png"/>
</td>
<td><?php echo "Php. ".number_format($total,2);?></td>
<td>
<a href="cart_actionCart.php?del=<?php echo $itms_id;?>&maxval=<?php echo $itms_stock;?>">
<img class="carticons" src="images/logos/Remove_r_50px.png"/>
</a>
</td>
</tr>
<?php
}
}
$totalval+=$total;
}
}
?>
And I make my image tag clickable by adding
`onclick="ajaxMinus('<?php echo $itms_id;?>','<?php echo $itms_stock;?>')"`
And I add as well
`onclick="ajaxAdd('<?php echo $itms_id;?>','<?php echo $itms_stock;?>')"`
And here is my simple function for ajaxAdd
function ajaxAdd(id,maxqty){
if(window.XMLHttpRequest){
xhrAdd=new XMLHttpRequest();
}
else{
if(window.ActiveXObject){
try{
xhrAdd=new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){}
}
}
if(xhrAdd){
xhrAdd.onreadystatechange=minusQty;
xhrAdd.open("GET","cart_actionCart.php?add="+id+"&maxval="+maxqty,true);
xhrAdd.send();
}
else{
alert("Couldn't create an XMLHttpRequest");
}
}
function minusQty(){
if(xhrAdd.readyState == 4 && xhrMinus.staus == 200){
document.getElementById('itmQty'+id).value = xhrAdd.responseText;
}
}
This is working but my problem is the input value is not changing until I refresh the page, where is my problem? Any help is greatly appreciated.
Anything loaded in via AJAX will not have a listener unless the parent element was there in the first place.
As an example, I had a form HTML come through AJAX, and the datepicker would not work, until I changed the original code from this:
$('.datepicker').datepicker();
To this:
$("body").on("focusin", ".datepicker", function(){
$(this).datepicker();
});
Because body is there always, the listener now works. This will probably what your issue is too, so I suggest trying to adapt your listeners as above.

Add bulk discount options to cart in Opencart 1.5.6.4

Just wondering if there is a way that I can add discounts to the shopping cart in Opencart.
Basically im wanting customers to be able to select an option, i,e size, then add bulk amounts to the shopping cart. This would then add say 1000 of this item directly to the shopping cart. Im trying to get something like this:
Buy 1000 or more for only £5.00 per unit [Add to cart]
Buy 2000 or more for only £4.50 per unit [Add to cart]
Here is my code so far, unfortunatly Im not sure how to get this working correctly so any help would be great.
Thanks!
<?php if ($discounts) { ?>
<br />
<div class="discount">
<?php foreach ($discounts as $discount) { ?>
<div class="discount-item">
<h3>Buy <?php echo $discount['quantity']; ?> or more for only <?php echo $discount['price']; ?></h3>
<a onClick="addToCart('<?php echo $product['product_id']; ?>');" class="button"><?php echo $button_cart; ?></a>
</div>
<?php } ?>
</div>
<?php } ?>
It's unclear exactly what you're asking for. But if it's any help, the addToCart method that comes with the default template accepts an optional second field, which is the quantity. So you could do:
<a onClick="addToCart('<?php echo $product['product_id']; ?>', <?php echo $discount['quantity']; ?>);" class="button"><?php echo $button_cart; ?></a>
Edit for items with options:
I'll assume you're on the product page, as you need to select the options somehow... now we could clone the whole function that submits the data to the cart. But I think this is easier to manage:
First, change the link to look like this:
<a class="buy-bulk" data-quantity="<?php echo $discount['quantity']; ?>" class="button"><?php echo $button_cart; ?></a>
Then add the following near the bottom:
<script type="text/javascript">
$(document).ready(function() {
$('.buy-bulk').on('click', function() {
var quantity = $(this).data('quantity');
// change the quantity
$('input[name="quantity"]').val(quantity);
// simulate a click on the "add to cart button"
$('#button-cart').trigger('click');
})
});
</script>
NOTE: untested! :)

Autofill textfield with custom text after selected a stat from dropdown menu

I need some help for this script. It's a script I got from other place and I would like to add a function to it but I really got stuck. This is the script:
<select name="order_status_id">
<?php foreach ($order_statuses as $order_statuses) { ?>
<?php if ($order_statuses['order_status_id'] == $order_status_id) { ?>
<option value="<?php echo $order_statuses['order_status_id']; ?>" selected="selected"><?php echo $order_statuses['name']; ?></option>
<?php } else { ?>
<option value="<?php echo $order_statuses['order_status_id']; ?>"><?php echo $order_statuses['name']; ?></option>
<?php } ?>
<?php } ?>
</select>
</td>
</tr>
<tr>
<td><?php echo $entry_notify; ?></td>
<td><input name="notify" type="checkbox" value="1" checked="checked" /></td>
</tr>
<tr>
<td><?php echo $entry_comment; ?></td>
<td><textarea name="comment" cols="40" rows="8" style="width: 99%"></textarea>
I would like to fill the textfield automatically after I selected one name from the dropdown menu.
for example in the dropdown listbox I selected a status "Processing" then the textfield below will automatically fill with "We are now processing your request. Please wait for X days and we would inform you shortly."
So when I selected another status "canceled" , the textfield will change it's content with "We have cancelled your request." and so on..
Please tell me if my description wasn't clear enough. Thank you so much beforehand.
You can add a javascript function that will do what you need in the onchange event of your select box like this:
<select id="order_status_id" name="order_status_id" onchange="updateTextBox()">
<textarea id="comment" name="comment" cols="40" rows="8" style="width: 99%"></textarea>
Also I added id values for both elements so we can easily select them with getElementById
This event would fire each time you change the selection. You then would implement a simple javascript function updateTextBox() that will update the textbox for you:
function updateTextBox() {
var orderStatusSelect = document.getElementById("order_status_id");
var status = orderStatusSelect.options[orderStatusSelect.selectedIndex].text;
var myTextBox = document.getElementById("comment");
if(status == "Processing") {
//set the value of the text box
myTextBox.value = "We are now processing your request. Please wait for X days and we would inform you shortly." ;
} else if(status == "Cancelled") {
myTextBox.value = "We have cancelled your request!";
} else if(status == "OtherStatus") {
myTextBox.value = "another message goes here!";
}
//and so on for all different options
}
See this jsFiddle for a running example.

Javascript Dialogue box in ctp?

In test.ctp file
<?php echo $form->create(null, array('url' => 'test/'.$test['Test']['id'], 'onSubmit'=>'return status(this)')); ?>
<?php echo $form->hidden('id', array('value' => $test['Test']['id'])); ?>
<dl class="editForm">
<?php echo $form->create(null, array('url' => 'test/'.$test['Test']['id'])); ?>
<?php echo $form->hidden('id', array('value' => $test['Test']['id'])); ?>
<dt><label><?php __('Update Status');?>:</label></dt>
<dd><?php echo $form->select('status_id', $statuses, $selectedStatus, array(), false); ?></dd>
<dd><?php echo $form->end(__('Update Status', true)); ?></dd>
</dl>
In the same .ctp file I need a dialogue box to appear when update button is clicked for confirmation that if user wants to continue with "No Status" or not?
Following code is not working for me, where I am not getting status_id value.
<script type="text/javascript">
function status() {
var status_id = $("#status_id").val();
alert(status_id);
}
</script>
Required: If status_id value is 1 in posted data then dialogue box should appear for confirmation.
To create one button you don't need to keep create a new form. by using form->create...
what you want is just to check the dropdown if the value equals 1 confirmation_window appears.
To do so first you have to know the basic of HTML and JS. if you could implement that you will be able to do so. convert the code blow to cakephp tagging and form. it will get it work.
<form onsubmit="status();" >
<select name="data[User][field]" id="UserField">
<option value="0">1</option>
<option value="1">2</option>
<option value="2">3</option>
</select>
<input type="submit" />
</form>
<script>
function status() {
var status_id = $("#UserField").val();
//or
var status_id = document.getElemetById("UserField");
alert(status_id.options[status_id.selectedIndex].value);
}
</script>
you could even assign id_name to your dropdown. then you should be able to get the value from dropdown using js or jQuery.
$form->select('status_id', array('id'=>'status_id')); ?>

Categories

Resources