Passing php variable to jquery - php

for($v=0;$v<11;$v++) {
echo "<div class='unsubscribed'><a class='button'>Unsubscribe</a></div>";
echo "<div id='$v'></div>";
}
I want onclick of the class unsubscribed to remove the div below in the same iteration. So for this i have to pass $v to jquery.
This is what i started with jquery but i don't know how to get the variable $v. How do i accomplish this?
$.ready(
function() {
$('.unsubscribed').remove();
}
);

you do not need to pass anything to jquery :
$(document).ready(function(){
$('.unsubscribed').one('click',function(){
$(this).next().remove();
});
});
This works for your current html.
To be more safe, you should add a class to the elements you want to be removed:
for($v=0;$v<11;$v++) {
echo "<div class='unsubscribed'><a class='button'>Unsubscribe</a></div>";
echo "<div class='to_be_removed'></div>";
}
This way you can reference the div you want to remove withouth it being necessarily after the unsubscribed div :
$(document).ready(function(){
$('.unsubscribed').one('click',function(){
$(this).next('.to_be_removed').remove();
});
});

A better solution might be:
<?php for($v=0;$v<11;$v++) { ?>
<div class="unsubscribed" rel="<?php echo $v; ?>">
<a class='button'>Unsubscribe</a>
</div>
<div id="<?php echo $v; ?>"></div>
<?php } ?>
$(document).ready(function(){
$(".unsubscribed").click(function(){
var div_to_remove = $(this).attr("rel");
$('#'+div_to_remove).remove();
});
});
I prefer doing it this way, because working with .next can sometimes cause problems, when you add something in between. It can be very hard to find the problem then.
This way, you simply embed the needed information about the div you want to remove into an attribute of the div that triggers the event.
Note: in this example, the function is called on clicking the .unsubscribed div - not the .button.
You also have to make sure, the removable divs have different and unique ids. If $v isn't unique, you can do e.g. something like this:
...
<div id="<?php echo $v . $i; ?>"></div>
...

Related

$_GET not working when trying to view php image in a lightbox style

I have something that im currently working on, however it appears that the $_GET doesn't completely work.
I have a JavaScript light box that brings up an image in a little window, this works however i can only guess that it is using the same URL over and over again.
However when i view the source for the page (and even click one of the links in the source) it will display the correct data.
But the lightbox only seems to display the first image.
This is the JavaScript
<script>
//Checkes if any key pressed. If ESC key pressed it calls the lightbox_close() function.
window.document.onkeydown = function (e)
{
if (!e){
e = event;
}
if (e.keyCode == 27){
lightbox_close();
}
}
</script>
<script>
//This script makes light and fade divs visible by setting their display properties to block.
//Also it scrolls the browser to top of the page to make sure, the popup will be on middle of the screen.
function lightbox_open(){
window.scrollTo(0,0);
document.getElementById('light').style.display='block';
document.getElementById('fade').style.display='block';
}
</script>
<script>
//This makes light and fade divs invisible by setting their display properties to none.
function lightbox_close(){
document.getElementById('light').style.display='none';
document.getElementById('fade').style.display='none';
}
</script>
I wont show the CSS i dont think thats relivant (If someone wants it then ask away)
The relevant part that creates the links is this, its part of a ForEach statement all PHP
$i = 0;
foreach ($nrows as $nrow)
{
$id = $nrow['id'];
$rid = $nrow['RaidID'];
$bid = $nrow['BossID'];
$normal = $nrow['NormalKills'];
$heroic = $nrow['HeroicKills'];
$boss = substr($nrow['BossName'], 0, 3);
$p1 = $id + $bid.".php";
$image = $boss . $p1;
#echo $image;
echo $bid;
if ($oid != $rid)
{
$i = 0;
}
if ($i == 0) {
?><td style="width: 176px;"><center><b><?php echo $nrow['raid']; ?> </b></center></td> </tr><?php
$i++;
}
?><tr><td style="width: 176px;"><div align="left"><?php echo $nrow['BossName']; ?><div id="light"><img src="bossdata/template.php?boss=<?php echo $bid;?>"></a></div><div id="fade" onClick="lightbox_close();"></div>
</div>
<?php
if ($heroic == 0)
{
if ($normal > 0)
{
echo '<img src="images/whiteskull.png" align="right" alt="Normal Kill">';
}
else
{
echo '<img src="images/redx.png" align="right" alt="Not Killed">';
}
}
else
{
echo '<img src="images/redskull.png" align="right" alt="Normal Kill">';
}
?>
</td></tr><?php
$oid = $id;
}
Now this all works, and it actually displays an image with data, however no matter what link i click the boss data is always from the first one on the list.
To me this means that the data is getting through, and reaching the the right parts on image so its "Working", but all the links do the same thing and show the same data :(
*Removed last code Bulk
You have multiple div with the same ID "light" since you create them in a foreach loop.
<div id="light">
Your function lightbox_open() opens all the divs that have id "light".
document.getElementById('light').style.display='block';
That's why you always see the first lightbox. Because the others are behind the first one.
you should try something like this :
function lightbox_open(elem){
window.scrollTo(0,0);
elem.getElementByClass('light').style.display='block';
elem.getElementByClass('fade').style.display='block';
}
And change this :
<a href="#" onclick="lightbox_open();">
By this :
<a href="#" onclick="lightbox_open(this);">
And replace id by class in your div definition :
<div class="light">
$_GET is working correctly in your code.
The issue is in the way you are combining JavaScript and PHP in the second code box. First, all of your divs have the same ID: "light" which is wrong because they all IDs are meant to be unique within the HTML document. You need to identify them uniquely, for example appending the BossID to them.
After identifying each div uniquely you'll have to edit lightbox_open and lightbox_close so they can receive the BossID of the divs that you want to show and hide.

Send multiple dynamic arguments to jquery event functions

I want to send multiple arguments to JQUERY event functions.For sending one data i put it in value attr in html code.
<li id="<?php echo 'v'.$value['vehicleid'] ?>" value="<?php echo $value['vehicleid']; ?>" >
And get data in JQuery event function with $(this).val() But i don't know how can i pass multiple php data to JQuery event function!
these data are dynamic and produced by php code.
I came across something similar the other day.
Try using the html5 custom data attribute.
John Resig (the king of jquery and general javascript ninja) first posted about it here.
so:
<li id="vehicle<?php echo 'v'.$value['vehicleid'] ?>" data-vehicle="<?php echo $value['vehicleid']; ?>" >
Then access it using the jquery .data function. see http://api.jquery.com/data/.
If you have a lot of data you want to add I recommend using an object literal
{ vehicleId: <?php echo 'v'.$value['vehicleid'] ?>,
vehicleName: <?php echo 'v'.$value['vehicleName'] ?>}
as in the example on the jquery website.
You cannot use val() on an li it is supposed to be used on form elements like input for which there is a valid attribute called value
For an li you can use data attributes
<li id="<?php echo 'v'.$value['vehicleid'] ?>" data-vehicleid="<?php echo $value['vehicleid']; ?>" >
and use:
var vehicleId = $(this).data('vehicleid');
For passing multiple data you can seperate each id with some char like "-":
<li id="<?php echo 'v'.$value['vehicleid'] ?>" data-vehicleids="<?php echo $value['vehicleid1']. '-' .$value['vehicleid2']; ?>" >
which should produce something like:
<li id="1234" data-vehicleids="1234-3456-5678" >
then do
var vehicleIdsArray = $(this).data('vehicleids').split('-');
just make sure charachter used for seperation is not used for vehicle ids
This should help you:
<ul id="list">
<li data-vehicleid="1234" data-vehiclename="renault" data-vehicle-color="silver">Renault</li>
</ul>
$(document).ready(function(){
$('ul#list li').click(function(e){
var vehicleId = $(this).attr('vehicleid');
var vehicleName = $(this).attr('vehiclename');
var vehicleColor = $(this).attr('vehiclecolor');
return false;
});
});
Quote from Resig:
I think what is most enticing about this whole specification is that
you don't have to wait for any browser to implement anything in order
to begin using it. By starting to use data- prefixes on your HTML
metadata today you'll be safe in knowing that it'll continue to work
well into the future. The time at which the HTML 5 validator is
integrated into the full W3C validator your site will already be
compliant (assuming, of course, you're already valid HTML 5 and using
the HTML 5 Doctype).
I am not one hundred percent sure which kind of event are you pertaining to. But you can pass multiple arguments inside the function(..HERE..). It should be separated by commas. Its like this.
$('selector').change(function(arg1,arg2,arg3){
alert(arg1+arg2+arg3);
});
In this example i am using change event for the selector 'selector'.
I think you have to create a function to handle the event
function handleEvent(data-vehicleid,data-vehiclename,data-vehiclename)
{
alert(data-vehicleid + data-vehiclename + data-vehiclename);
}
Then on your li,
<li onmouseover="handleEvent(<?php echo $variable1;?>,<?php echo $variable2;?>,<?php echo $variable3;?>)">Renault</li>

php array and jquery add class

I am trying to get desks highlighted that are available based off of a form that asks for the the day, time start and time end. I am able to echo out all the desks that are available, but I cant get the jquery to work with it.
foreach ($allData as $desk => $id){
foreach ($id as $computer){?>
<div id="<?php echo $desk?>"></div><?php
}
}
<style>
.availableDesk{
background: #000000
}
</style>
<script>
$(document).ready(function() {
jQuery( " <?php echo $desk ?> " ), addClass('availableDesk') ;
})
</script>
DESKS:
<ul class="tabs">
<li id="1A"><div id="ddesk"></div></li>
<li id="1B"><div id="ddesk"></div></li>
<li id="1C"><div id="ddesk"></div></li>
</ul>
You're missing the ?> at the end of your PHP block.
<?php
foreach ($allData as $desk => $id){
foreach ($id as $computer){?>
<div id="<?php echo $desk?>"></div><?php
}
} // You need a "?>" here
?>
There are a few things wrong with your jQuery code. First off, if $desk is an ID, you need to do $('#ID'). Second, you have a comma before addClass instead of a period.
jQuery("#<?php echo $desk ?>").addClass('availableDesk');
P.S. HTML ID's aren't supposed to start with a number. Also, you cannot have multiple elements with the same ID, I suggest you use classes instead.
I can see a couple of potential issues here...
jQuery( "<?php echo $desk ?>" ), addClass('availableDesk') ;
Syntactically, I think this should be:
jQuery("#<?php echo $desk ?>").addClass('availableDesk');
Note the # in the selector, this tells jQuery that you are looking for an id. The addClass method is available on the returned items, so you need a stop (.) not a comma (,)
The other gotcha is that you are writing the id in a foreach loop in PHP - I presume that each desk has a unique id - when you write jQuery("#<?php echo $desk ?>") the statement is outside of the foreach loop, so it won't match the ids you are targeting.
If you know that the desk is available in PHP, the best option would be to set the class as you write the desk...
<div id="<?php echo $desk?>" class="availableDesk"></div>

scrollTO with strings

I'm trying to do a scrollTO function with my code but I keeps just popping up to the top like its not connecting to the id im telling it to go to is there something im doing wrong?.
<div class="a-z">
<? $a1=range("A","Z");
foreach($a1 as $char2){
echo "<a href='#$char2' onclick='$.scrollTo( '#$char2', 800, {easing:'elasout'} ); title='$char2'>$char2</a>";
}?>
<script type="text/javascript">
$(document).ready('#<?php echo $char2 ?>').localScroll({
target:'<?php echo $char2 ?>'
});
</script>
</div>
code updated i dont get any errors
<? $a1=range("A","Z");
foreach($a1 as $char2){
echo "<a href='#' onclick='$.scrollTo( \'#{$char2}\', 800, {easing:\'elasout\'} );' title='{$char2}'>{$char2}</a>";
}?>
Try this one as the php variable "$char2" in single quotes is not getting its value it should be enclosed in {} :)
rahul was almost right, but you need to use escaped double quotes inside of the onclick's single quotes. Escape them so the php parser doesn't think you are ending the 'echo' argument.
<? $a1=range("A","Z");
foreach($a1 as $char2){
echo "<a href='#' onclick='$.scrollTo( \"#{$char2}\", 800, {easing:\"elasout\"} );' title='{$char2}'>{$char2}</a>";
}?>
A better solution is giving all of those links a class and a custom data attribute, then using jQuery to assign a click handler:
<? $a1=range("A","Z");
foreach($a1 as $char2){
echo "<a href='#' class='linkClass' data-destination='#{$char2}' title='{$char2}'>{$char2}</a>";
}?>
In a script tag:
$(document).ready( function() {
$(".linkClass").click( function() {
//On a click event, get the destination data attribute, then scroll to it
$.scrollTo( $(this).data("destination"), 800, {easing: "elasout"});
});
});

Go Link thru iFrame

I have problem and I tried click link then it doesn't work to open link using target: name of iFrame. i dont want use href because im going make show/hide div.
Javascript:
<script type="text/javascript">
<!--//
function godirect(url, targetname)
{
document.getElementById(targetname).src = url;
//frame[targetname].location.href = url;
}
//-->
</script>
in HTML and PHP:
$a=0;
echo 'Click Me!';
echo '<iframe class="iframe_url" id="iframe_url'.$a.'"></iframe>';
How about
<script type="text/javascript">
function godirect(url, targetname) {
window.frames[targetname].location = url;
//OR
//window.open(url,targetname);
return false;
}
</script>
<?PHP
$a=0;
?>
Click Me!
<iframe class="iframe_url" name="iframe_url<? echo $a; ?>" id="iframe_url<? echo $a; ?>"></iframe>
You have to quote strings in JavaScript. You are trying to get the id of the element by passing in a variable which you haven't defined.
You are also using the same quote characters to delimit your HTML attribute value as you are using to delimit your JS strings.
To use the approach you are using, while making the minimum number of fixes to make it work:
echo 'Click Me!';
Using JS for this is a very silly idea in the first place though, and your implementation fails to have any kind of fallback for when JS is not available (which is odd, since you are taking steps to stop browsers which don't recognise the script element from rendering the JS as content text).
You can do this with plain HTML:
<a href="http://www.google.com"
target="iframe_url<?php echo htmlspecialchars($a); ?>">
Click Me!
</a>
i dont want use href because im going make show/hide div.
You can do that as well as having a normal, functioning link. Build on things that work.
Try this:
echo 'Click Me!';
echo '<iframe class=\"iframe_url\" id=\"iframe_url'.$a.'\"></iframe>';

Categories