Trying to use window.location and replaceWith() in the same time - php

I have a link that when clicked should generate a PDF document and change the text of himself.
This is my JS function:
function clander(){
window.location="orders/issueBill/order_id/"+order_id;
$('#jander').replaceWith('pdf just generated');
}
The link:
<a href="#" id="jander" onClick='clander()'>click here</a>
The action:
public function executeIssueBill(sfWebRequest $request) {
//here is the code that generates the pdf.
}
The problem: when I click the link, it generates the pdf but the text of the link is not changed. It only changes the text if I remove the window.location... line.
The reason of my code is here.
Any idea?
Regards
Javi

The problem is that it never gets to this part:
$('#jander').replaceWith('pdf just generated');
It redirects the user to "orders/issueBill/order_id/"+order_id, which is a different page, so it doesn't remember state.
I guess your best bet is to pass a parameter to the new page (ie. "orders/issueBill/order_id/"+order_id+"?pdf_generated=1") - this way it'll know that the pdf was generated, and you can change the link accordingly.

I think it's the order of your statements in the js function. If you change the order the other way around, should work fine like so:
<script>
function clander(){
document.getElementById('jander').innerHTML = 'pdf just generated';
window.location="orders/issueBill/order_id/"+order_id;
}
</script>
<a href="#" id="jander" onClick='clander()'>click here</a>
Best,
Kamran

Related

Change Location of Outgoing Link On Click

I'm trying to link to another page without displaying the real URL on hover or in the source code. I want <a> to redirect to $newUrl; even if there is another url defined in the href="".
<?php $newUrl = "http://therealurl.com/folder/private.php" ?>
Link
So using the code above I want to replace http://domain.com with <?php echo $newUrl; ?> when the user clicks on the <a> because i never want it to be displayed to the user.
Is there a solution to target a specific anchor to do this? If not then every link on the page is fine I just want to do this in a way that the real URL is not displayed for privacy reasons.
Here is solution.
<?php $newUrl = "http://therealurl.com/folder/private.php"; ?>
<script>
$( document ).ready(function(){
$( '#link' ).click(function(){
var newLink = <?php echo json_encode($newUrl);?>;
$(this).attr("href", newLink);
});
});
</script>
<a id="link" href="http://domain.com">Link</a>
Don't forget to include jquery library.
If you have full access to the original link, you could use it as a redirect by setting a header in the destination script.
header("Location: yoururl.php);
This is the only quick easy solution that I can think of, as this one will not show the actual destination in the source code.
If you want to hide the link from the source code then any javascript methods won't work, to my knowledge.
You could also use ?page=get stuff to change where it sends the user, in order to make this script usable in multiple ways, just don't forget to manage your GET data safely.

how to inactive a link using jquery

I have jQuery site here. I have 4 links with in ul tag. Within this link, I need to inactive a link but have to show. See below:
<div class="over hide" id="waterSecondOver">
Overview
Local Area
Floor Plans
<a href="javascript:void(0);" id="chancingallery" >Gallery</a>
</div>
I have a click function on the above links (except the 4th one i.e gallery). See the code
JQR('#waterSecondOver a').click(function() {
// my code
});
Here I need to show the gallery link, but nothing happens when click on the gallery (i.e must be inactive link or what I say it's functionality is only show the link as "gallery ")
I think it's clear . Please help me
JQR('#waterSecondOver a').click(function(event) {
event.preventDefault();
});
Don't set hrefs in the anchor tags to javascript:void(0); - this is messy because it's needless repetition. With jQuery, it's possible to use return false; in your event callback to avoid the default click event taking you to a new page or the current one. While this is the easiest method, it's not always the best. See this article for more info:
http://fuelyourcoding.com/jquery-events-stop-misusing-return-false/
But you definitely don't want to be setting href manually.
I would simply change the class of the anchor(lets say class="active") and everytime onclick check for that "active" class. If the class is "active" do nothing else DO SOMETHING.
You can do this way for simplicity...
JQR('#waterSecondOver a').click(function(event) {
if(JQR(this).attr('id') == "chancingallery"){
// inactive all links except the one you dont want
}else{
// whatever you need....
}
});

PHP, javascript around a link

I have a small piece of javascript to get a user to confirm things on my website. I have a link that I also ask them to confirm before proceeding. I've used the 'onclick' function with buttons no problem, but how do I use it on a link?
Heres my link:
echo"Delete";
And heres my 'onclick':
onclick="return confirm('Please confirm you wish to edit these details')"/>
Any help would be great!
Ummm...
<?php ?>
Delete
You can use onclick on an anchor tag the same way you'd use it on a button:
Delete
However you'd be better off using an event handler:
<script type="text/javascript">
function myClickHandler() {
return confirm("Really?");
}
document.getElementById("my_link").onclick = myClickHandler;
</script>
<a id="my_link" href="<?php echo $urlHTML; ?>">Delete</a>
I recommend you also read up on jQuery. It'll save you a lot of time.
You might want to consider using addEventListener and attachEvent (IE) instead of using inline JavaScript.

Use Ajax to send data to external page, and then load said external page

I have a thumbnail gallery populated from a database using php. Each thumbnail is also a link. What I would like is to be able to load an external php page in each case, with the large version of each thumbnail on it, flanked with related images.
The database tables are all set up properly in a relational sense, but I'm not sure about the functionality of being able to send data, in this case imgId, from the thumbnail gallery page, to the external page, and load the external page at the same time.
I thought it would be possible to do with a form submit, but since I need this functionality on every single thumbnail link, I thought that Ajax would work using jQuery. But alas it doesn't seem to be sending the data when I click on the link.
Hopefully someone can give me some advice. Thanks in advance.
The HTML:
<a target="_blank" href="secondary_imgs.php" class="gallery" value="16">
<img src="new_arrivals_img/thumbnails/boss-skaz1_black_front.jpg">
</a>
The jQuery:
$('.gallery').click(function(){
$.get("secondary_imgs.php", { imgId: $('.gallery').attr('value') });
});
Use $(this) inside the function to reference the clicked gallery
$('.gallery').click(function(){
$.get("secondary_imgs.php", { imgId: $(this).attr('value') });
});
Try this:
$('.gallery').click(function(){
$.get("secondary_imgs.php", { imgId: $(this).attr('value') }, function(data) {
$('body').append(data);
});
});
If that does nothing when you click on it, browse to "/secondary_imgs.php?imgID=xxx" and see if it is returning the data correctly at all.
Ok, I feel kinda stupid, but I figured out that all I needed to do was pass the data through the URL. The PHP code below shows what I mean, and I do thank those who helped me with this problem. My fault for making it faaaar more complicated than needed.
My question is however, is it safe to place data in the href as I'm showing below? I remember someone mentioning a potent ion problem concerning google spiders and the like.
The PHP:
while($row = mysql_fetch_assoc($result_pag_data)) {
echo "<a target='_blank' href='secondary_imgs.php?imgId=".$row['imgId']."'></a>";
}

jQuery - click() not working when attached to anchor created in PHP

I have this PHP code which outputs HTML anchor elements on a page:
if(!$isOnOwnPage)
echo '<a id="message-button" class="button">Message</a>';
if($isOnOwnPage)
echo '<a id="add-img-button" class="button">Add Image';
else if(!$isFollowing)
echo '<a id="follow-button" class="button">Follow';
else
echo '<a id="follow-button" class="button">Unfollow';
echo "</a>";
When I load the web page, I get this, as expected:
...
<a id="message-button" class="button">Message</a><a id="follow-button" class="button">Unfollow</a>
...
But when I try to attach a click() function to it, the clicking doesn't work. This is the jQuery code. (It's weird because all of my other JS on the page works flawlessly."
$('.button').click(function() { alert("HEY"); }); // It doesn't work grabbing by #follow-button or #message-button either.
What did I do wrong here? I've spent an hour looking at these snippets of code to no avail.
Try this:
$('.button').live('click',function() { alert("HEY"); });
Put it in your $(document).ready function.
Gonna go out on a limb and guess that you've forgotten to wait for document.ready and that the a.button element doesn't exist when the click event is bound.
Is ajax included somehow? Because then you need to either use the live, or re-apply the function.
It should work regardless if it's PHP generated. In the end it's still HTML that gets output to the client.
I think you should make sure that the JS code gets to the part where you assign clicks to the buttons. Maybe there's not document ready or a correct one. Maybe there's a boolean IF that doesn't get passed.
All in all, if you can see the code in the View Source page, then it's a HTML/JS problem, not a PHP one.
This is an old question, but to all there who want to add listeners to dynamic content, instead of using $('a').(click(function(){}) or $('a').on('click',function(){}), you need to use instead the document as the object you want to attach the listeners, because the tag you are inserting wasn't in the DOM when the listener was assigned.
So instead you should use:
$(document).on('click','a',function(){ ... });

Categories