selectbox the option I chose goes back to the top - javascript

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>

Related

Button gets an incorrect PHP value by default via javascript

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>

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

JavaScript on change function working not well

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.

How to be a div id is undefined in javaScript?

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);
};

Categories