Jump to content

PHP function calling in <a>


ruddie

Recommended Posts

Hi, I got a little problem with calling a PHP function from a href.

I got my site all set up, and am calling the function like this:

 

<?php echo '<a href="'.changeRecentPosts().'" class="image">' ?>

 

This does indeed work, but it calls the function every time I refresh my page.. Which isn't the effect I was trying to achieve.

 

I tried to change it to :

<?php echo '<a href="#" onClick="'.changeRecentPosts().'" class="image">' ?>

But that didn't work either.

 

It also seems it only calls it when I refresh my page, but never when I click it (I guess because PHP is handeled before HTML or something similar?).

 

If you can think of any way (even in AJAX, or whatever other language...) feel free to, I think I can figure it all out.

 

Thank you!  :D

Link to comment
https://forums.phpfreaks.com/topic/224815-php-function-calling-in/
Share on other sites

You could use some Javascript and CSS to hide the <div>.

 

Add some CSS to the page:

<style type="text/css">
.active   { display:block; }
.inactive { display:none; }
</style>

 

If you're <div> doesn't already have an ID, create one. You'll also need to default the class to "active" if you want it to be shown by default. For example:

<div id="contentToBeHidden" class="active">...</div>

 

Now you just need to create a JavaScript funtion to deal with the onclick (Example: <a href="#" onclick="changeRecentPosts()">Test</a>):

<script type="text/javascript">
function changeRecentPosts() {
//IF THE DIV IS CURRENTLY SHOWING, HIDE IT
if(document.getElementById('contentToBeHidden').className == 'active') {
	document.getElementById('contentToBeHidden').className='inactive';
//ELSE...THE DIV IS HIDDEN, SHOW IT
} else {
	document.getElementById('contentToBeHidden').className='active';
}
}
</script>

 

 

Here my entire test script:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
function changeRecentPosts() {
//IF THE DIV IS CURRENTLY SHOWING, HIDE IT
if(document.getElementById('contentToBeHidden').className == 'active') {
	document.getElementById('contentToBeHidden').className='inactive';
//ELSE...THE DIV IS HIDDEN, SHOW IT
} else {
	document.getElementById('contentToBeHidden').className='active';
}
}
</script>
<style type="text/css">
.active   { display:block; }
.inactive { display:none; }
</style>
</head>

<body>
<p><a href="#" onclick="changeRecentPosts()">Test</a></p>
<div id="contentToBeHidden" class="active">Content to be hidden</div>
</body>
</html>

 

Let me know if you have any questions.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.