htillberg
asked on
Adding a variable into a <a href> tag
I am trying to modify a template. I want to add a link to an image that is generated by PHP. Currently the code is:
<dl class="gallery"><dd><a href="http://www.sky2five.com/jpegged/index.php<?=$post_ID?>">
But it defaults to index.php...
Is the syntax the problem?
Thanks,
HT
<dl class="gallery"><dd><a href="http://www.sky2five.com/jpegged/index.php<?=$post_ID?>">
But it defaults to index.php...
Is the syntax the problem?
Thanks,
HT
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Good day:
The problem is indeed in your syntax. First, the script doesnt know that you are passing any data to it because you haven't defined a variable in the URL. Without any html code, your url should look something like this:
http://www.sky2five.com/jpegged/index.php?post_id=5
You'll notice there that I added the question mark (which means that a single variable is getting ready to follow), then I added post_id=5. post_id is just a variable name that I made up. You can name it anything you like. Then you see = and then the number 5. The number 5 in this case, obviously represents your variable $post_ID in your script. I just gave it a value in this case so you can see how it all fits together.
So in order to fix your script, you have to add a variable name to the url, and then pass the value. I chose to use a full <?php ?> block here, for clarity, but it should work the same either way.
<?php
echo "<dl class=\"gallery\"><dd><a href=\"http://www.sky2five.com/jpegged/index.php?post_id=\"".$post_ID."\">Cli ck here</a>";
?>
Now in your script (index.php) you will need to retrieve this value like this--
<?php
// index.php--
// Your code should already be here someplace--
$postID=$_GET['post_id'];
if(!isset($postID) || empty($postID)) {
// Handle the error here--
} else {
// Script continues--
}
?>
Please let me know if there is any other way that I could clarify this for you. Hope it helps! Have a wonderful day!
The problem is indeed in your syntax. First, the script doesnt know that you are passing any data to it because you haven't defined a variable in the URL. Without any html code, your url should look something like this:
http://www.sky2five.com/jpegged/index.php?post_id=5
You'll notice there that I added the question mark (which means that a single variable is getting ready to follow), then I added post_id=5. post_id is just a variable name that I made up. You can name it anything you like. Then you see = and then the number 5. The number 5 in this case, obviously represents your variable $post_ID in your script. I just gave it a value in this case so you can see how it all fits together.
So in order to fix your script, you have to add a variable name to the url, and then pass the value. I chose to use a full <?php ?> block here, for clarity, but it should work the same either way.
<?php
echo "<dl class=\"gallery\"><dd><a href=\"http://www.sky2five.com/jpegged/index.php?post_id=\"".$post_ID."\">Cli
?>
Now in your script (index.php) you will need to retrieve this value like this--
<?php
// index.php--
// Your code should already be here someplace--
$postID=$_GET['post_id'];
if(!isset($postID) || empty($postID)) {
// Handle the error here--
} else {
// Script continues--
}
?>
Please let me know if there is any other way that I could clarify this for you. Hope it helps! Have a wonderful day!
Inorrect: <dl class="gallery"><dd><a href="http://www.sky2five.com/jpegged/index.php<?=$post_ID?>">
Correct: <dl class="gallery"><dd><a href="http://www.sky2five.com/jpegged/index.php?post_id=<? echo $post_ID; ?>">
Correct: <dl class="gallery"><dd><a href="http://www.sky2five.com/jpegged/index.php?post_id=<? echo $post_ID; ?>">
If your $post_ID = 1, then the URL looks like the one below that I posted. I actually went to your site, you pass a parameter to index.php and assign a number to it. So far, I noticed two parameters - cat and p. Try to modify the URL as I suggested. It worked on my server.
<?php
//just an example
$post_ID=1;
?>
<dl class="gallery">
<dd>
<a href="http://www.sky2five.com/jpegged/index.php?cat=<?php print $post_ID?>">Click here</a>
<?php
//just an example
$post_ID=1;
?>
<dl class="gallery">
<dd>
<a href="http://www.sky2five.com/jpegged/index.php?cat=<?php print $post_ID?>">Click here</a>
Here is "proper" way to do this. First we check whether variable is set or not, then if true we echo out ?post_ID=value, otherwise we don't do anything.
<dl class="gallery"><dd><a href="http://www.sky2five.com/jpegged/index.php<?php echo (isset($post_ID) ? '?post_ID=' . $post_ID : ''); ?>">
You must be careful with variable names because $post_ID is not the same as $post_id!!
<dl class="gallery"><dd><a href="http://www.sky2five.com/jpegged/index.php<?php echo (isset($post_ID) ? '?post_ID=' . $post_ID : ''); ?>">
You must be careful with variable names because $post_ID is not the same as $post_id!!
try this:
<a href="http://www.sky2five.com/jpegged/index.php?PostID=<?=$post_ID?>">
that will add a QueryString Variable "PostID" with the value of post_ID.
Not sure if that's what you want.. maybe some more clarification will help,
CHeers.