Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
589 views
in Technique[技术] by (71.8m points)

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

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...