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
Share on other sites

What is "changeRecentPosts()" supposed to do?

 

It is supposed to minimalize/maximalize (show or not show) a div that uses PHP to collect a bit of data from my database. So basically, it just adds a cookie that says whether it is active or not.

Link to comment
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.