Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HREF="" automatically adds to current page URL (in PHP). Can't figure it out

Tags:

html

php

Longtime reader of stackoverflow but first question.

I'm working with Wordpress (specifically thesis theme) in the custom_functions.php file and am finding for some reason is automatically adding the current page url. For example this code is to query the database and then loop through outputting each product in it's own div:

$result = db_connection($query); while ($row = mysql_fetch_array($result)) { ?>     <div class="box"><a href="">         <img src="http://www.electricbikehub.co.nz<?php echo $row['product_root_directory']         . $row['mid_size_image'] ?>">         <h2><?php echo $row['name']?></h2>         <p><?php echo $row['description_brief'];?></p>         <p><span class="multiple_product_red"><span class="multiple_product_linethrough">RRP: <?php echo $row['list_price']; ?>.</span> Our discounted price: <?php echo $row['our_price']; ?>. Includes delivery and GST.</span></p>         </a>     </div> <?php } ?> 

As you can see 3rd line says href="" but the actual link being generated is the current page (in this case 'http://www.electricbikehub.co.nz/?page_id=1192'). If I add anything in the href, such as href="something" it will just add it to the end, ie http://www.electricbikehub.co.nz/?page_id=1192something.

Any help would be greatly appreciated!

like image 513
Evan Hobbs Avatar asked Jan 06 '12 20:01

Evan Hobbs


People also ask

How do I find my current HREF URL?

Answer: Use the window. location. href Property location. href property to get the entire URL of the current page which includes host name, query string, fragment identifier, etc.

What does href =# do?

Definition and Usage The href attribute specifies the URL of the page the link goes to. If the href attribute is not present, the <a> tag will not be a hyperlink. Tip: You can use href="#top" or href="#" to link to the top of the current page!

Can we use anchor tag in PHP?

Of course it can. The PHP part is just the language that the server is instructed to interpet the page in. Then it sends regular HTML back to the client browser, who uses the anchors to jump around the HTML document. IDs are the correct way to do it, on any visible element.


2 Answers

This is how a browser interprets and empty href. It assumes you want to link back to the page that you are on. This is the same as if you dont assign an action to a <form> element.

If you add any word in the href it will append it to the current page unless you:

  • Add a slash / to the front of it telling it to append it to your base url e.g. http://www.whatever.com/something
  • add a # sign in which case it is an in-page anchor
  • or a valid URL

EDIT: It was suggested that I add a link to help clarify the situation. I found the following site that I think does a really good job explaining the href attribute of anchor tags and how it interprets URL paths. It is not incredibly technical and very human-readable. It uses lots of examples to illustrate the differences between the path types: http://www.mediacollege.com/internet/html/hyperlinks.html

like image 143
Wes Crow Avatar answered Oct 21 '22 06:10

Wes Crow


Add http:// in front of url

Incorrect

<a href="www.example.com">www.example.com</span></p> 

Correct

<a href="http://www.example.com">www.example.com</span></p> 
like image 34
Mukesh Avatar answered Oct 21 '22 07:10

Mukesh