AndrewFerrara
AndrewFerrara

Reputation: 2403

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';
}
?>

Upvotes: 0

Views: 4233

Answers (7)

enam
enam

Reputation: 1177

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>
<a href="javascript:showhide('abc');">Show/Hide Details</a>

<div id="abc" style="display:none;">
    your codes......
</div>
</body>
</html>

The real beauty of Javascript.....Hope this help you

Upvotes: 0

mario
mario

Reputation: 145482

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).

Upvotes: 0

Shamim Hafiz - MSFT
Shamim Hafiz - MSFT

Reputation: 22094

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.

Upvotes: 0

hnprashanth
hnprashanth

Reputation: 831

I think you must use javascript here. PHP would need a page refresh to process your echo statement.

Upvotes: 1

moe
moe

Reputation: 29704

<a id="anchor" onclick="document.getElementById('information').style.display='block';">Information</a>
<div id="information" style="display:none"><? echo 'INFORMATION' ?></div>

Upvotes: 1

Toby Allen
Toby Allen

Reputation: 11213

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.

Upvotes: 0

fabrik
fabrik

Reputation: 14365

Since PHP (your httpd, exactly) doesn't know anything about anchors, you'll need to handle this with javascript. Try jQuery.

Upvotes: 0

Related Questions