Jquery, codeigniter working with load and replaceWith - javascript

This is code for generating page:
<div id="mali_oglasi">
<?php foreach ($mgs as $key) : ?>
<p class="mali_oglas">
<span class="name"><?php echo $key['name'] ?></span>
<span class="body"><?php echo $key['body'] ?></span>
<span class="contact_author"><?php echo $key['contact_author'] ?></span>
<span class="date_created"><?php echo $key['date_created'] ?></span>
<span class="date_expires"><?php echo $key['date_expires'] ?></span>
<span class="email"><?php echo $key['email'] ?></span>
<?php foreach ($lokacija as $lok) : ?>
<?php if($lok['id_location'] == $key['location_id']) : ?>
<span class="lokacija" id="<?php echo $lok['id_location'] ?>"><?php echo $lok['name'] ?></span>
<?php endif ?>
<?php endforeach; ?>
<?php foreach ($kategorija as $kat) : ?>
<?php if($kat['id_category'] == $key['category_id']) : ?>
<span class="kategorija" id="<?php echo $kat['id_category'] ?>"><?php echo $kat['name'] ?></span>
<?php endif ?>
<?php endforeach; ?>
<a class="obrisi" id="<?php echo $key['id_global_info'] ?>">Obrisi</a>
<a class="izmeni" id="<?php echo $key['id_global_info'] ?>">Izmeni</a>
</p>
<?php endforeach; ?>
</div>
which will give this result:
<div id="mali_oglasi">
<p class="mali_oglas">
<span class="name">fbsd</span>
<span class="body">sdhdsf</span>
<span class="contact_author">mirko</span>
<span class="date_created">2012-03-15 11:24:19</span>
<span class="date_expires">2012-04-14 11:24:19</span>
<span class="email">a#a.com</span>
<span class="lokacija" id="1">Pirot</span>
<span class="kategorija" id="1">Auto i Moto</span>
<a class="obrisi" id="19">Obrisi</a>
<a class="izmeni" id="19">Izmeni</a>
</p>
<p class="mali_oglas">
<span class="name">dsh</span>
<span class="body">dshg</span>
<span class="contact_author">mirko</span>
<span class="date_created">2012-03-15 11:17:52</span>
<span class="date_expires">2012-04-14 11:17:52</span>
<span class="email">a#a.com</span>
<span class="lokacija" id="1">Pirot</span>
<span class="kategorija" id="1">Auto i Moto</span>
<a class="obrisi" id="18">Obrisi</a>
<a class="izmeni" id="18">Izmeni</a>
</p>....
</div>
and JS for replaceWith and load:
$(parent).on('click', '.izmeni', function() {
var name = $(this).siblings('.name').text();
var body = $(this).siblings('.body').text();
var email = $(this).siblings('.email').text();
var contact_author = $(this).siblings('.contact_author').text();
var id_lok = $(this).siblings('.lokacija').attr("id");
var id_kat = $(this).siblings('.kategorija').attr("id");
$(this).siblings('.name').replaceWith('<input value="' + name + '" />');
$(this).siblings('.body').replaceWith('<textarea>' + body + '</textarea>');
$(this).siblings('.date_created').hide();
$(this).siblings('.date_expires').hide();
$(this).siblings('.email').replaceWith('<input value="' + contact_author + '" />');
$(this).siblings('.lokacija').replaceWith(function(){
$(this).load("<?php echo base_url() ?>form/location", {'id' : id_lok})
});
$(this).siblings('.kategorija').replaceWith(function(){
$(this).load("<?php echo base_url() ?>form/category", {'id' : id_kat})
});
});
load is calling following page (for lokacija):
<select>
<?php foreach ($lokacija as $lok) : ?>
<option
<?php if($lok['id_location'] == $id) : ?>
selected="selected"
<?php endif ?>>
<?php echo $lok['name'] ?>
</option>
<?php endforeach; ?>
</select>
Everything except lokacija and kategorija is replaced, but in the firebug I am getting positive response (page requested page is loaded without the errors but it is not displayed). What am I doing wrong?

Dosent LOAD and REPLACEWITH both replace the content, so you are replacing the replaced the content.
So can't you just either add the ID to the load method, or make a normal ajax GET and the use the replacewith function?

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>

Building Filtering sidebar and searching box

I want to build filtering sidebar and search box . My problem is, I can't make it run searching box and filtering items together at the same time. I hope any of u can help me?
I think,the main problem is in my script "var s" php parameter where I'm getting cities from
Let's see what I've done so for,
Here's my script;
<script>
$(function(){
$('.item_filter').change(function(){
var p = multiple_values('p');
var s ='<?php echo $_POST["search"]; ?>';
var url ="product.php?product=<?php echo
htmlentities($_GET['product']) ?>&s="+s+"&b="+b+"&p="+p;
window.location=url;
});
});
function multiple_values(inputclass){
var val = new Array();
$("."+inputclass+":checked").each(function() {
val.push($(this).val());
});
return val.join('-');
}
</script>
Search Box
<form class="search" name="search" method="POST"
action="product.php?product=<?php echo
''.htmlentities($_GET['product']).''; ?>" >
<a href="javascript:void(0);"><input id="small_search"
name="searchTerm" class="item_filter s" <?php { echo "checked"; } ?>
list="local" /></a>
<?php
while($row = mysqli_fetch_assoc($result)){
$local = ($row['local']); ?>
<a href="javascript:void(0);" target="_blank"><input type="submit"
id="small_search_btn" value="Search"></a >
<datalist id="local">
<option value="<?php echo ''.htmlentities($row['local']).''; ?
>"></option>
<?php } ?>
</form >
**Filtering **
<div class="list-group" style="font-size:14px;">
<h4 style="font-size:16px; font-weight: bold; color:black;">
</h4>
<?php
some sql statement..
?>
<a href="javascript:void(0);" class="list-group-item"
id="left_filtering" >
<label >
<input type="checkbox" class="item_filter b" value="<?php echo
htmlentities($row['brand']); ?>" <?php if(in_array($row['brand'],$b
{echo "checked"; } ?> > <?php echo ucfirst($row['brand']);
?></a>
</label>
<?php } ?>
</div>

Giving unique ID's in a loop doesnt work

I'm trying to give each ID in the loop a unique ID.
Like: check-1, check-2 etcera.
But it is not working .
Here is my code:
<?php
if( have_rows('activiteiten_knoppen') ): $count=0;?>
<?php while(have_rows('activiteiten_knoppen') ): the_row();
$meer_info = get_sub_field('meer_info');
$tijd = get_sub_field('reserveren');
?>
<p class="activiteitInfo"><a class="fancybox-inline" style="color: #f3f3f3 !important;">Meer info</a></p>
<p class="activiteitReserveer"><a style="color: #f3f3f3 !important;" href="<?php echo $tijd; ?>">Reserveren</a></p>
<div id="check-<?php echo $count ?>" style="display: none;"><?php echo $meer_info; ?></div>
<script>
$(document).ready(function () {
$(".activiteitInfo").click(function () {
$(".hoi").css("display", "block");
});
});
</script>
<?php $count++; ?>
<?php endwhile; ?>
<?php endif; ?>

Multiple ID's on single button click

HTML + PHP
<?php
for($i=0;$i<5;$i++){
?>
<input readonly class="copyTarget" id="copyTarget<?php echo $i; ?>" value="val<?php echo $i; ?>">
<span>
val<?php echo $i; ?>
</span>
<button class="btn btn-primary" id="copyButton<?php echo $i; ?>" onClick="reply_click(this.id, $('.copyTarget').attr('id'));">Copy</button>
<?php
}
?>
JS
<script>
function reply_click(clicked_id, target_id) {
alert(clicked_id);
alert(target_id);
}
</script>
What i want
I want to get the both values for copyTarget and copyButton as per loop cycle. It means
If current value of $i = 3
then I want alert values like,
clicked_id = copyTarget3
target_id = copyButton3
What i am currently getting is,
If current value of
$i = 3
then I want alert values like,
clicked_id = copyTarget0
target_id = copyButton3
Its taking first value of ID(copyTarget) stored initially. I want current loop cycle value.
Any help would do
Thanks
Why use JS in handler?
Try:
onClick="reply_click('copyButton<?php echo $i; ?>', 'copyTarget<?php echo $i; ?>')"
Also you should store id names (copyButton and copyTarget) in php variable, so you can change them in one place.
You could try something like below. However, I would go by Maxx's answer .. It really depends on what you plan to do with the rest of code etc.
<?php
for($i=0;$i<5;$i++){
?>
<div>
<input readonly class="copyTarget" id="copyTarget<?php echo $i; ?>" value="val<?php echo $i; ?>">
<span>
val<?php echo $i; ?>
</span>
<button class="btn btn-primary" id="copyButton<?php echo $i; ?>" onClick="reply_click(this)">Copy</button>
</div>
<?php
}
?>
<script>
function reply_click(btn) {
var clicked_id = $(btn).attr('id');
var target_id=$(btn).parent().find('span').html();
alert(clicked_id);
alert(target_id);
}
</script>
Try This
<?php
for($i=0;$i<5;$i++){
?>
<input readonly class="copyTarget" id="copyTarget<?php echo $i; ?>" value="val<?php echo $i; ?>">
<span>
val<?php echo $i; ?>
</span>
<button class="btn btn-primary" id="copyButton<?php echo $i; ?>" onClick="reply_click(this.id);">Copy</button>
<?php
}
?>
JS
<script>
function reply_click(clicked_id) {
alert(clicked_id); //clicked id
alert(clicked_id.split('copyButton').join('copyTarget')); // target id
}
</script>

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