I have a JavaScript code which works with HTML select tag.
$(function() {
$('#to_name').on('change',function() {
if( $(this).val()=="<?php echo $ceo_chunks[0];?> <?php echo $ceo_chunks[1];?> <?php echo $ceo_chunks[2]?>") {
$('#to_designation').val('<?php echo $ceo_chunks[3];?>');
$('#dear_sir').val('<?php echo $ceo_chunks[0]?> <?php echo $ceo_last_name;?>') ;
if( $(this).val()=="<?php echo $liaison_one_chunks[0];?> <?php echo $liaison_one_chunks[1];?> <?php echo $liaison_one_chunks[2]?>") {
$('#to_designation').val('<?php echo $liaison_one_chunks[3]?>')
$('#dear_sir').val('<?php echo $liaison_one_chunks[0]?> <?php echo $liaison_one_last_name;?>')
$("#liaison_one").show()
$("#dear_liaison_one").show()
} else {
$("#ceo").hide()
$("#liaison_two").hide()
$("#dear_ceo").hide()
$("#dear_liaison_two").hide()
}
if( $(this).val()=="<?php echo $liaison_two_chunks[0];?> <?php echo $liaison_two_chunks[1];?> <?php echo $liaison_two_chunks[2]?>") {
$('#to_designation').val('<?php echo $liaison_two_chunks[3]?>')
$('#dear_sir').val('<?php echo $liaison_two_chunks[0]?> <?php echo $liaison_two_last_name;?>')
$("#liaison_two").show()
$("#dear_liaison_two").show()
} else {
$("#ceo").hide()
$("#liaison_one").hide()
$("#dear_ceo").hide()
$("#dear_liaison_one").hide()
}
};
});
});
and here is html code
<select name="to_name" id="to_name">
<option value="<?php echo $ceo_chunks[0];?> <?php echo $ceo_chunks[1];?> <?php echo $ceo_chunks[2]?>"><?php echo $ceo_chunks[0];?> <?php echo $ceo_chunks[1];?> <?php echo $ceo_chunks[2]?></option>
<option value="<?php echo $liaison_one_chunks[0];?> <?php echo $liaison_one_chunks[1];?> <?php echo $liaison_one_chunks[2]?>"><?php echo $liaison_one_chunks[0];?> <?php echo $liaison_one_chunks[1];?> <?php echo $liaison_one_chunks[2]?></option>
<option value="<?php echo $liaison_two_chunks[0];?> <?php echo $liaison_two_chunks[1];?> <?php echo $liaison_two_chunks[2]?>"><?php echo $liaison_two_chunks[0];?> <?php echo $liaison_two_chunks[1];?> <?php echo $liaison_two_chunks[2]?></option>
</select>
<br>
<select name="to_designation" id="to_designation">
<option value="<?php echo $ceo_chunks[3]?>" id="ceo"><?php echo $ceo_chunks[3]?></option>
<option value="<?php echo $liaison_one_chunks[3]?>" id="liaison_one"><?php echo $liaison_one_chunks[3]?></option>
<option value="<?php echo $liaison_two_chunks[3]?>"id="liaison_two"><?php echo $liaison_two_chunks[3]?></option>
</select>
<div id="dear_ceo" style="margin-top:10px; width:auto">
<input type="text" name="dear_sir" id="dear_ceo" value="Dear <?php echo $ceo_chunks[0]?> <?php echo $ceo_last_name;
?>"/>
</div>
<div id="dear_liaison_one" style="margin-top:10px; width:auto; display:none">
<input type="text" name="dear_sir" id="dear_liaison_one" value="Dear <?php echo $liaison_one_chunks[0]?> <?php echo $liaison_one_last_name;?>"/>
</div>
<div id="dear_liaison_two" style="margin-top:10px; width:auto; display:none">
<input type="text" name="dear_sir" id="dear_liaison_two" value="Dear <?php echo $liaison_two_chunks[0]?> <?php echo $liaison_two_last_name;?>"/>
</div>
My function works very well for select tag but it's not working with input type text.
How can I fix that?
There is a closing bracket clearly missing for your first if statement.
Please close the bracket. There is no logical error in your code.
$(function() {
$('#to_name').on('change',function(){
if( $(this).val()=="<?php echo $ceo_chunks[0];?> <?php echo $ceo_chunks[1];?> <?php echo $ceo_chunks[2]?>"){
$('#to_designation').val('<?php echo $ceo_chunks[3];?>')
$('#dear_sir').val('<?php echo $ceo_chunks[0]?> <?php echo $ceo_last_name;?>')
/*$("#ceo").show()
$("#dear_ceo").show()*/
//} ---uncomment this ---
also include a terminating semi-colon everytime you are calling a function. For example
if( $(this).val()=="<?php echo $ceo_chunks[0];?> <?php echo $ceo_chunks[1];?> <?php echo $ceo_chunks[2]?>"){
$('#to_designation').val('<?php echo $ceo_chunks[3];?>');
$('#dear_sir').val('<?php echo $ceo_chunks[0]?> <?php echo $ceo_last_name;?>');
}
I suggest you use semicolons at the end of line (when appropriate). I know they are not mandatory but consider them as a good practice.
You also have a semicolon at the end of if-clause at the end. It's probably not braking anything but just in case...
Try replacing spaces with underscores (_).
For example:
<?php echo $ceo_chunks[0];?> <?php echo $ceo_chunks[1];?> <?php echo $ceo_chunks[2]?>
would become
<?php echo $ceo_chunks[0];?>_<?php echo $ceo_chunks[1];?>_<?php echo $ceo_chunks[2]?>
And check if you can alert the value of the changed input correctly.
Related
When the page is redirected, the language I chose in the box does not
appear. At first, whatever is available is chosen.
here is what I want to do: change option data
<select id='lang' class='btn btn-xs btn-primary icons-select2'>
<?php
$query_lang=mysqli_query($con, "SELECT * FROM tb_lang");
while($lang = mysqli_fetch_assoc($query_lang)){
if ($lang['durum']==1)}
?>
<option value = "<?php echo $lang['lang_fix']; ?>" data-min="_<?php echo $lang['lang_fix']; ?>"
data-icon="<?php echo $lang['lang_flag']; ?>">
<?php
echo strtoupper($lang['lang_fix']);
?>
</option>
<?php }} ?>
</select>
<script>
$(document).ready(function () {
$('body').on('change','#lang', function() {
var lang_id= $(this).val();
window.location.replace("index.php?lang="+lang_id);
});
});
</script>
First retrieve the lang sent over GET by
$lang_id = $_GET['lang'];
then check it against the data you are looping through
<option value = "<?php echo $lang['lang_fix']; ?>" data-min="_<?php echo $lang['lang_fix']; ?>" data-icon="<?php echo $lang['lang_flag']; ?>" <?php if ($lang_id == $lang['lang_fix']) echo "selected"; ?>>
making your code look like below, so the option you want to be selected will have the necessary selected attribute.
$lang_id = $_GET['lang'];
<select id='lang' class='btn btn-xs btn-primary icons-select2'>
<?php
$query_lang=mysqli_query($con, "SELECT * FROM tb_lang");
while($lang = mysqli_fetch_assoc($query_lang)){
if ($lang['durum']==1)}
?>
<option value = "<?php echo $lang['lang_fix']; ?>" data-min="_<?php echo $lang['lang_fix']; ?>" data-icon="<?php echo $lang['lang_flag']; ?>" <?php if ($lang_id == $lang['lang_fix']) echo "selected"; ?>>
<?php
echo strtoupper($lang['lang_fix']);
?>
</option>
<?php }} ?>
</select>
<script>
$(document).ready(function () {
$('body').on('change','#lang', function() {
var lang_id= $(this).val();
window.location.replace("index.php?lang="+lang_id);
});
});
</script>
I have the following selector with options:
<select name="" id="product-selector" class="list_item">
<?php
if( have_rows('section_a') ):
while( have_rows('section_a') ): the_row();
if( have_rows('product_item') ):
while( have_rows('product_item') ): the_row();
$product_item_quantity = get_sub_field('product_item_quantity');
$product_item_price = get_sub_field('product_item_price');
$product_item_id = get_sub_field('product_item_id');
$product_item_variation_id = get_sub_field('product_item_variation_id');
?>
<option value="<?php echo $product_item_variation_id; ?>" data-id="<?php echo $product_item_variation_id; ?>" data-content="<?php echo $product_item_quantity; ?> <span class='price' data-count='<?php echo $product_item_quantity; ?>' data-price='<?php echo $product_item_price; ?>'><?php echo $product_item_price; ?>"> </option>
<?php endwhile; endif; endwhile; endif; ?>
</select>
And I have the following button:
<div class="submit">
<a class="btn btn-warning" id="purchase" href="?add-to-cart=<?php echo $product_item_id; ?>&variation_id=<?php echo $product_item_variation_id; ?>">Order Now</a>
</div>
My javascript is:
<script>
document.getElementById("product-selector").onchange = function() {
document.getElementById("purchase").href = "?add-to-cart="+"<?php echo $product_item_id; ?>"+"&"+"variation_id="+this.value+"/";
}
</script>
When user click by the Order button the link changes dynamically - it gets the value of and should get the <?php echo $product_item_variation_id; ?>" via the "variation_id="+this.value+" and it works fine, but when the page loads, the <?php echo $product_item_variation_id; ?> value of the button get the LAST option value, but not a first (default) one.
Much appreciate any help.
I think that you should make a variable to save the first value. Because you already loop. so default value is last value of the select.
<?php $product_item_variation_id_first = ""; ?>
<select name="" id="product-selector" class="list_item">
<?php
if( have_rows('section_a') ):
while( have_rows('section_a') ): the_row();
if( have_rows('product_item') ):
while( have_rows('product_item') ): the_row();
$product_item_quantity = get_sub_field('product_item_quantity');
$product_item_price = get_sub_field('product_item_price');
$product_item_id = get_sub_field('product_item_id');
$product_item_variation_id = get_sub_field('product_item_variation_id');
if ($product_item_variation_id_first == "")
$product_item_variation_id_first = $product_item_variation_id;
?>
<option value="<?php echo $product_item_variation_id; ?>" data-id="<?php echo $product_item_variation_id; ?>" data-content="<?php echo $product_item_quantity; ?> <span class='price' data-count='<?php echo $product_item_quantity; ?>' data-price='<?php echo $product_item_price; ?>'><?php echo $product_item_price; ?>"> </option>
<?php endwhile; endif; endwhile; endif; ?>
</select>
<div class="submit">
<a class="btn btn-warning" id="purchase" href="?add-to-cart=<?php echo $product_item_id; ?>&variation_id=<?php echo $product_item_variation_id_first; ?>">Order Now</a>
</div>
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
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!
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Hi i have read this thread about using php variable in jquery but somehow it doesnt work on my site:
LINK
Javascript:
<script>
var row_id="<?php echo $r['id']; ?>";
$(document).ready(function(){
$("#que"+row_id).click(function(){
$("#ans"+row_id).slideToggle();
});
});
</script>
<?php foreach($res as $r) : ?>
<li id="que<?php echo $r['id']; ?>">Q<?php echo $r['id']; ?>: <?php echo $r['question']; ?> </li>
<div id="ans<?php echo $r['id']; ?>" style="padding:5px;">A: <?php echo $r['answer']; ?></div>
<?php endforeach; ?>
Just use classes. Otherwise you will end up having a jquery piece of code for each question.
<script>
$(document).ready(function(){
$(".que").click(function(){
$(this).next().slideToggle();
});
});
</script>
<?php foreach($res as $r) : ?>
<li class="que">Q<?php echo $r['id']; ?>: <?php echo $r['question']; ?></li>
<div style="padding:5px;">A: <?php echo $r['answer']; ?></div>
<?php endforeach; ?>
FIDDLE
Script tag should be inside the forach loop.
Please try this and explain exact what error you got?
you wouldn't get this value var row_id="<?php echo $r['id']; ?>"; because $r['id'] is in the foreach.
I assume that you want to take the $r['id'], so try something like this
<script>
$(document).ready(function(){
$(".row").click(function(){
var row_id = $(this).val();
$("#ans"+row_id).slideToggle();
});
});
</script>
<?php foreach($res as $r) : ?>
<li id="que<?php echo $r['id']; ?>">Q<?php echo $r['id']; ?>: <?php echo $r['question']; ?></li>
<input type="hidden" value="<?php echo $r['id']; ?>" class="row" />
<div id="ans<?php echo $r['id']; ?>" style="padding:5px;">A: <?php echo $r['answer']; ?></div>
<?php endforeach; ?>
Use classes and .next() to solve this easily:
<script>
$(document).ready(function(){
$('.que').click(function(){
$(this).next('.ans').slideToggle();
});
});
</script>
<?php foreach($res as $r) : ?>
<li id="que<?php echo $r['id']; ?>" class="que">Q<?php echo $r['id']; ?>: <?php echo $r['question']; ?> </li>
<div id="ans<?php echo $r['id']; ?>" class="ans" style="padding:5px;">A: <?php echo $r['answer']; ?></div>
<?php endforeach; ?>
I think you have used the undefined variable $r in javascript.
Which is only defined in the foreach loop. get the javascript into the loop it will work.
<script>
function q_click(row_id){
$("#ans"+row_id).slideToggle();
}
</script>
<?php foreach($res as $r) : ?>
<li id="que<?php echo $r['id']; ?>">Q<?php echo $r['id']; ?>: <?php echo $r['question']; ? onclick="q_click(<?php echo $r['id']; ?>)"> </li>
<div id="ans<?php echo $r['id']; ?>" style="padding:5px;">A: <?php echo $r['answer']; ?></div>
<?php endforeach; ?>