Solved

Adding a variable into a <a href> tag

Posted on 2004-08-18
9
7,894 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:htillberg
9 Comments
 
LVL 27

Accepted Solution

by:
Diablo84 earned 250 total points
ID: 11835406
i would avoid using short tags personally in case you are ever in an environment where they are disabled, so try:

<dl class="gallery"><dd><a href="http://www.sky2five.com/jpegged/index.php<?php echo $post_ID; ?>">

and failing that do some checking to make sure the variable is set and its not empty, somewhere in your script:

<?php
if (!isset($post_ID)) {
 echo "post_ID variable is not set";
}
elseif (empty($post_ID)) {
 echo "post_ID variable is set but is an empty";
}
0
 
LVL 5

Expert Comment

by:darksinclair
ID: 11835422
What are you trying to have it default too?

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.
0
 
LVL 2

Expert Comment

by:nowaydown1
ID: 11836018
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."\">Click 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!

0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 1

Expert Comment

by:PatrickLucas
ID: 11870936
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; ?>">
0
 
LVL 1

Expert Comment

by:njava
ID: 11898726
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>
0
 
LVL 9

Expert Comment

by:gruntar
ID: 12643871
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!!
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question