The php and html are pretty simple:
<?php
session_start();
$_SESSION['liked_or_not']=0;
?>
<button class="photo_like" value="<?php if($_SESSION['liked_or_not']==0){ echo 'Like';} else{ echo 'Unlike';} ?>" ><?php if($_SESSION['liked_or_not']==0){ echo 'Like';} else{ echo 'Unlike';} ?>
jQuery snippet:
<script type="text/javascript">
$(document).ready(function(){
alert("alert one ="+ $('.photo_like').text());
if($('.photo_like').text()=='Like'){
alert("alert two ="+ like_text);
}
});
</script>
The above code gives the ouput as 'alert one =Like' and then after a few invisible line breaks on the alert box a square. How does that square appear there?
And the second alert box does not appear, as the square box is not included in the if($('.photo_like').text()=='Like'){ comparison.
Also Chrome console says :Uncaught TypeError: Cannot read property 'hasPort' of undefined
Explanation?
You should close your button.
<button class="photo_like" value="<?php if($_SESSION['liked_or_not']==0){ echo 'Like';} else{ echo 'Unlike';} ?>" ><?php if($_SESSION['liked_or_not']==0){ echo 'Like';} else{ echo 'Unlike';} ?></button>
And use ternary operator can make your code more clear.
<?php $like_or_not = $_SESSION['liked_or_not']==0 ? 'Like' : 'Unlike'; ?>
<button class="photo_like" value="<?php echo $like_or_not; ?>"><?php echo $like_or_not; ?></button>
if you want the value Like and Unlike the set in into the hidden input box like:
<input type="hidden" name="getTxt" value="<?php if($_SESSION['liked_or_not']==0){ echo 'Like';} else{ echo 'Unlike';} ?>" >
and use something like this:
var getHiddenVal = $('input[name="getTxt"]').val();
// Return the value
its far easy with messing the codes.
$('.photo_like') can return multiple objects. To be sure it returns only one object use $('.photo_like:first')
To get the value of a button i always use val() instead of text().
So your if should be like this: if($('.photo_like:first').val()=='Like'){}
Related
I want to use PHP variable in jQuery.
I am having a 50 students data in table which having textbox and radio button.
By default textbox is hidden but when I clicked on radio button textbox show in table.
$("input[type='radio']").on('change', function() {
if ($(this).val() == "2")
var id = '<?php echo json_encode($absent); ?>';
//$("input[type='text']").show();
else
$("input[type='text']").hide();
});
PHP code:
<input name="nxtmark<?php echo $row[0]; ?>" type="text" id="nxtmark<?php echo $row['id']; ?>" onblur="onleave(this.name);" maxlength="2" placeholder="Enter Mark" hidden="" />
<div class="noerrordiv" id="dnxtmark<?php echo $row[0]; ?>">Mark must not be blank</div>
</td>
<td>
<div align="justify">
<input type="radio" id="rdopresent" name="rdopresent<?php echo $row['id']; ?>" checked="checked" value="1" />Present
<input type="radio" id="rdoabsent" name="rdopresent<?php echo $row['id']; ?>" value="2" />Absent<br />
</div>
</td>
</tr>
You can show / hide text box without using PHP variable. See below code -
$("input[type='radio']").on('change',function() {
//find input text from the previous 'td' for which name starts with 'nxtmark'
var $textBox = $(this).closest('td').prev('td').find('input[name^=nxtmark]');
//show hide text box as per radio button value
if($(this).val() == "2")
$textBox.show();
else
$textBox.hide();
});
If your Problem is just to use PHP variable in jquery then following examples will help you.
if you have to user a php array in jquery then use json_encode in PHP not in SCRIPT.
<?php
$a = array(1,2,3,4);
$a = json_encode($a);
$b = "hello";
?>
<script>
$(document).ready(function(){
alert("<?php echo $a; ?>");
alert("<?php echo $b; ?>");
});
</script>
I have this code php
............................
..............................
.................
<input type="radio" name="option[<?php echo $option['product_option_id']; ?>]" stock="
<?php if ($option_value['subtract']) { ?>
<?php if ($option_value['quantity'] >= 3) { ?>
<?php echo $text_in_stock; ?>
<?php } ?>
<?php if ($option_value['quantity'] < 3 && $option_value['quantity'] > 0) { ?>
<?php echo $option_value['quantity']; ?> <?php echo $text_pcs_only; ?>
<?php } ?>
<?php if ($option_value['quantity'] <= 0) { ?>
<?php echo $text_out_of_stock; ?>
<?php } ?>
<?php } ?>
" value="<?php echo $option_value['product_option_value_id']; ?>" id="option-value-<?php echo $option_value['product_option_value_id']; ?>" onchange="recalculateprice();" />
<div class="pippo">
.................
.......
</div>
then I have this script
<script type="text/javascript">
function recalculateprice() {
var stock = $('#style_stock').attr('stock');
$('input[type=radio]:checked').each(function() {
$('.pippo').hide();
if ($(this).attr('stock') != '<?php echo $text_out_of_stock; ?>') {
$('.pippo').show()
stock = $(this).attr('stock');
}
});
I need hide div pippo when check an option with stock = . But the attr stock in option radio has some if ....
What I have insert in this row ?
if ($(this).attr('stock') != '<?php echo $text_out_of_stock; ?>') {
i can't tell you exactly what is the problem
but here are few important notes:-
first in PHP
try to calculate the stock value outside the input tag stock attribute & echo the final value and remove the extra spaces.
try to use "else if" instead of stacking "if" commands and making ambiguity in the value
(its also easier)
second in Javascript
the code you have written will always show the ".pippo" div in case if there at least on radio button with attribute stock != out_of_stock so you need to make sure that you are testing the only changed radio.
the value of the $(this).attr('stock') in the case you use the line directly will be undefined as u didn't define it .
finally the calling for onChange attribute will make some problems in page loading and may give you error of undefined function
so instead i prefer as long as you are using jQuery attach the events through jQuery
here is a full sample of may what you can use
HTML
<div class="pippo">
.................
.......
</div>
<input type="radio" stock="...." name="opt">
<input type="radio" checked="checked" stock="10" name="opt">
<div id="style_stock" stock="10">stock = 10</div>
JAVASCRIPT
function recalculateprice(x) {
if ($(x).attr('stock') != '....') {
$('.pippo').show();
//put the code you want
}else{
$('.pippo').hide();
//put the code you want
}
}
$("input[type=radio]").change(function(){
recalculateprice(this);
});
i have a list of records from a recordset :
<?php while($row = mysqli_fetch_array($result_imprint))
{ ?>
<tr><td><input type="checkbox" id="location_<?php echo $row['location_id']; ?>" name="location_[<?php echo $row['location_id']; ?>]" value="location_<?php echo $row['imprint_location']; ?>"/> </td>
<td><?php echo $row['imprint_location']; ?></td>
<td><?php echo $row['max_size']; ?></td>
<td><?php echo $row['imprint_type']; ?> </td>
<td> <?php echo $row['max_colours']; ?></td>
<td><?php echo $row['setup_fee']; ?></td></tr>
<?php } ?>
I can't figure out how to show a div if someone checks a box from one of these records. there are a max of six records, I have the div's already done out :
<div class="location_1" id="location_1" style="display: none;">
<br /><hr class="style-seven">
Content Here
</div>
This is the code i used but it doesn't seem to work :
$(document).ready(function(){
$('#location').change(function(){
if(this.checked)
$('#location_[<?php echo $row['location_id']; ?>]').fadeIn('slow');
else
$('#location_[<?php echo $row['location_id']; ?>]').fadeOut('slow');
});
});
any help much appreciated.
thanks
For starters... IDs have to be unique on your page. So you cannot have a checkbox with id="location_1" and then have a div with also an id of "location_1"
So have checkboxes with id "location_X" and your divs with "div_location_X"
I would also stop using php on javascript, unless you do a ajax call to a php script or you do a loop to write all the javascript for each of the checkboxes, but it would be simpler to:
when checked (use a class to group all the checkboxes together), you can get javascipt to get the id attribute of the checkbox
<input type="checkbox" id="location_1" name="location_1" class="check"/> </td>
$(document).ready(function(){
$('.check').change(function(){
if((.check).is(":checked"))
var div_name = $(this).attr('id'); //this should come back with location_1 for the first checkbox
$('#div_'+div_name+'').fadeIn('slow'); // prepend div_ for the corrent unique id name
else
$('#div_'+div_name+'').fadeOut('slow');
});
});
Use chrome tools console for debugging
currently your php code will get interpreted as a string.
try changing your code to:
$('#location_['+<?php echo $row["location_id"]; ?>+']').fadein('slow')
and respectively for fade out.
<tr><td>
<input type="checkbox" id="location_<?php echo $row['location_id']; ?>" name="location_[<?php echo $row['location_id']; ?>]" class="check"/> </td>
$(document).ready(function(){
$('#location').change(function(){
if((.check).is(":checked"))
$('#location_[<?php echo $row['location_id']; ?>]').fadeIn('slow');
else
$('#location_[<?php echo $row['location_id']; ?>]').fadeOut('slow');
});
});
1.You can't have same id for div and check box,so i am changing div id to location_div
$(document).ready(function(){
$(":checkbox").click(function () {
if($("input[id^='location_']:checkbox:checked").length>0)
{
$("#location_div").removeAttr('style');
}
else
{
$("#location_div").css({"display":"none"});
}
});
});
What I want to do is that only when I click on a div with id="$project['slug']", it will load the iframe inside that div.
So, i removed the src attribute from the iframe, and add it on onclick event.
I have this in my HTML/PHP:
<div class="box" id="<?=$project['slug']?>" onclick="load_frame();">
<iframe id="frame_<?=$project['slug']?>" frameborder="0"></iframe>
</div>
Js:
function load_frame() {
id = location.hash.replace('#', '');
var source = "<?php echo $project['video']; ?>";
$('#frame_'+id).attr("src", source);
}
Unfortunately, when I inspect the iframe it shows: src="<?=$project['video']?>" instead of the value that the variable holds.
Do you have any idea what i am doing wrong?
thank you!
jQuery is a client side language and have access only to the DOM elements once they have been rendered. So what you need to do is store $project['video'] variable in a hidden field and then using the id of that field get access to the rendered data.
Also, i noticed that you should use <?php instead of <?
You may try something like this.
<div class="box" id="<?php echo $project['slug']; ?>">
<iframe id="frame_<?php echo $project['slug']; ?>" frameborder="0"></iframe>
<input type="hidden" id="<?php echo $project['slug']; ?>" value="<?php echo $project['video']" />
</div>
Then in jQuery do:
$(document).ready(function(){
$('.box').click(function(){
var slug = $(this).attr('id');
var source = $('input#' + slug).val();
$('iframe#' + slug).attr("src", source);
});
});
add hidden input on html page
<input type="hidden" id="hidsource" value="<?php echo $project['video']" />
edit your function in js like this
function load_frame() {
id = location.hash.replace('#', '');
$('#frame_'+id).attr("src", $('input#hidsource').val());
}
hope this will work
You might not have the shorthand syntax enabled on the server. Try standard PHP instead:
<?php echo $project['slug']; ?>
<script language="javascript">
function customFunction(url){
alert(url);
//window.location = "url";
//document.all.url.checked = true;
document.getElementById('url').checked = true;
document.downloadable_prod.submit();
}
</script>
<!-- Note : this below radio buttons coming from loop --->
<input type="radio" name="links" id="links_<?php echo $_link->getId() ?>" value="<?php echo $_link->getId() ?>" />
<a href="javascript:customFunction(<?php echo $_link->getId() ?>);" class="buy_stan">
<span style="margin-left:160px;">
<?php echo $this->getFormattedLinkPrice($_link); ?>
your radio buttons have a link of "links_xxxx", xxx being whatever your php is returning. in your anchor tag you need to add links to the custom function call. Try the below
Note I added 'links_ after customFunction( and closed it off using ' at the end of your php call.
<a href="javascript:customFunction('links_<?php echo $_link->getId() ?>');" class="buy_stan">