Href="" Automatically Adds to Current Page Url (In PHP). Can't Figure It Out

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

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

generating a HREF depending on the current URL using PHP

You're very close. I just split out the PHP from the HTML to make it easier to read. Basically, you were just missing the quotes around the destination URL. I also added a new variable $destination and set it to a default value. This way you can avoid the else which, again, just makes it easier to read.

<?php
$blog = '/blog/';
$currentpage = $_SERVER['REQUEST_URI'];
$destination = 'http://yahoo.com'; // This is a default

if ($blog == $currentpage) {
$destination = 'http://google.com';
}
?>

<li>
<a href="<?php echo $destination; ?>">About</a>
</li>

EDIT

Also another question - the links on my new blog to my main website
work but they link to my main website using
https://www.example.com/somepage.html, and I'd rather it be like
../../somepage.html -

is the full URL bad for my website? e.g. bad for SEO, or in analytics
will it show as a bounce off my website if it was the full link and
not if it was from ../ ?

What do you mean when you say,

../

<a href="../somepage.html">Test</a> VS. <a href="mysite.com/somepage.html">Test</a> ?

If so, then there is no difference. The browser will send the user to the same page and the HTTP request will look the same.

EDIT #2

thanks for that. By ../ I mean if you consider href='../somepage.html'
vs href='example.com/somepage.html'; - one linking locally and one
sending a http request and requiring the internet to work –
user8758206

I think there is some confusion about what is happening when you use relative links Vs. absolute links. These are instructions for the browser at runtime. It does not generate an extra HTTP request (redirect) when someone clicks on the link. All else being equal, both links will produce a 200 HTTP response code from the server.

Here's a discussion - Absolute vs relative URLs

Can we get the link in POST?

after long discussion, here is i get what he want to

  1. He want to send variable to controller by click (I use Jquery)

so here is my demo based on your issue

note in that demo you need latest jquery, please see external js

so i change your html to make it easy

<ul class="dropdown-menu">
<a href="#" class="specialLink" id="54th-65hy">
<li>54th-65hy</li>
</a>

<a href="#" class="specialLink" id="HT45-YT6T">
<li>HT45-YT6T</li>
</a>
</ul>

Please see i put your value as id

and this is my js

$( ".specialLink" ).click(function() {
var value = this.id; //get value for throw to controller
alert(value);

$.ajax({
type: "POST", //send with post
url: "<?php echo site_url('welcome/post) ?>", //for example of mine , using your controller
data: "value=" + value, //assign the var here
success: function(msg){
alert(msg);
}
});
});

see the id i save into variable with name value to send to controller welcome/post

then this is the controller

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {

public function index()
{

}

public function post()
{
$value=$this->input->post('value');
echo $value;
}

}

to make you know ajax, just comment my js in demo will be like this

   $( ".specialLink" ).click(function() {
var value = this.id; //get value for throw to controller
alert(value);

$.ajax({
type: "POST", //send with post
url: "<?php echo site_url('welcome/post) ?>", //for example of mine , using your controller
data: "value=" + value, //assign the var here
success: function(msg){
alert(msg);
}
});
});

Edit :
1. make the demo become show the value first after you know it just copy and paste js in answer into codepen
2. in the demo i pust controller in js part
3. please see in js file demo data: "value=" + value,

Adding a href to an i class

<a href="www.google.com"><i class="fa fa-facebook"></i></a>

How to prevent canonical URL's

Everything behind # is interpreted as the hash fragment and not send to the server. Instead your browser change the hash fragment for the current page.

RFC 2396: Uniform Resource Identifiers (URI): Generic Syntax

The character "#" is excluded because it is used to delimit a URI
from a fragment identifier in URI references (Section 4).

https://www.rfc-editor.org/rfc/rfc2396

PHP: Getting current full url and href to each word in a variable

This is a simple application of explode, which splits a string into multiple substrings based on a delimiter, in this case ", ".

$test = "Ontario, Montreal, Quebec";
$words = explode(", ", $test);
foreach ($words as $word) {
echo "<a href='http://www.domain.com/" . $_SERVER["REQUEST_URI"] . "'>$word</a>";
}

a tag with php adds %20 even without spaces

you can try something like this to remove white spaces:

<a class="links" href="buying.php?link=<?php echo preg_replace('/\s+/', '', $urlname) ?>">Gekauft</a>


Related Topics



Leave a reply



Submit