PHP, javascript around a link - php

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.

Related

onclick on image is not working

I am trying to display some info when a user clicks on an image. Here is the image tag
<img src="<? echo $list[image]; ?>" onclick="showUser(<?php echo $list['id']; ?>)">
I am directly using the example from w3schools. Here is the link
Is there something i am doing wrong? When I click the image nothing happens.
I don't recommend using that GET method, it would be better to use a load method instead because you would have less code to work with and in my opinion it would make things easier. Now here's how I would do it.
Assuming you got that code from there, this is what you can do:
<script type="text/javascript">
ShowUser(str) {$(function) {
$('#loadhere').load("thepagetoloadfrom.php?q="+str);
}};
</script>
#loadhere is where you want to load the output from the load().
This is much shorter and easier to understand I believe.
I figured out the problem myself. In my showUsers function when I call xmlHttp.open I had the path to my file wrong. Thanks anyway

If anchor is clicked php

I'm trying to echo information on a page after and anchor has been clicked
<a id="anchor">Information</a>
<?php
if(?){
echo 'INFORMATION';
}
?>
<a id="anchor" onclick="document.getElementById('information').style.display='block';">Information</a>
<div id="information" style="display:none"><? echo 'INFORMATION' ?></div>
I think you must use javascript here. PHP would need a page refresh to process your echo statement.
That's where you might want to utilize AJAX. You can often approach it like this:
<a onClick=" $('#output').load('output.php') ">click here</a>
<div id="output"><!-- This is where the content goes --></div>
Then define the according PHP script output.php like this:
<?php
echo $whatever;
?>
jQuery will then issue another HTTP request, invoking a PHP script, and finally injects it where you told it to (#output div).
Since PHP (your httpd, exactly) doesn't know anything about anchors, you'll need to handle this with javascript. Try jQuery.
You cant do this in PHP I'm afraid. PHP lives on the server and the page is on your client (browser). In order to do something (other than go to another page) when some clicks, you'll need to use javascript. Look at jquery.
You can't do it like this.
PHP works in the server side, therefore once the anchor information has come to the client browser, you won't be able execute any PHP code there.
There are several workarounds if you really want to achieve that.
i) Use client side JavaScript.
ii) Use Ajax to maker requests to server side and update page accordingly.
If you really want to show the information after clicking into a link can use the following code:
<html>
<title>lasdfjkad</title>
<head>
<script type="text/javascript">
function showhide(id){
if (document.getElementById){
obj = document.getElementById(id);
if (obj.style.display == "none"){
obj.style.display = "";
} else {
obj.style.display = "none";
}
}
}
</script>
</head>
<body>
Show/Hide Details
<div id="abc" style="display:none;">
your codes......
</div>
</body>
</html>
The real beauty of Javascript.....Hope this help you

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(){ ... });

Open link in iframe and then open another link in same window

I have this code:
echo 'Send map to 2nd screen';
Id like to be able to press this link, which opens the case_util.php in another iframe and then goes to another link in its own iframe.
Sort of like a refresh/going back. However, it is a form, and pressing back retains the data in the form. I dont want this data in the form, hence I'd like it to navigate to the page.
Thanks :)
Syntax issues. document.location is not a function and is deprecated anyway.
Here is a better version
echo 'Send map to 2nd screen';
or easier to read since you want the href in the iframe named second .
...?> <a href="case_util.php?status=green&adr=<?php echo $adr; ?>" rel="nofollow" target="second"
onClick="window.location='create.php'" >Send map to 2nd screen</a>
Best answer:
Don't use iframes.
However if you are determined to use something as silly as an iframe, use something as silly as a meta redirect.
<meta http-equiv="refresh" content="0;url=http://example.com/"/>
;) seriously, just read about AJAX+jQuery instead:
http://net.tutsplus.com/tutorials/javascript-ajax/5-ways-to-make-ajax-calls-with-jquery/

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

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

Categories