Good Day! I've got some trouble on PHP. If I click an href which is this <a href = 'members/loans/loan-application-form.php'>
it goes to the loan-applicaton-form.php, but if i click it again inside the loan-application-form.php (cuz it is located at the navbar that exist in all pages) the href of the anchor concatenates on the existing url and it creates an error telling that address is not existing. I already knew that the problem is it searches the address on the current directory and probably it will return an error because there is no existing address on the directory because it is the origin or the parent. What is the best way to avoid this?

- 291
- 2
- 6
- 15
-
The best solution would be to either remove that link when you are already on the loan-application form or disable it. If its not there or user cannot use it then you cannot get an error. If you leave it there the next problem will be when they click it, they loose all the information they have already entered – RiggsFolly Jul 06 '15 at 08:57
3 Answers
Add a base
tag to your header. This will prepend all HTML URL's with the given base URL. (Including images, CSS and JS calls)
<head>
<base href="http://www.yourdefaulturl.com/">
</head>
Your URL will then be http://www.yourdefaulturl.com/members/loans/loan-application-form.php

- 1,384
- 17
- 32
-
-
As far as I can understand it correctly, this is the most elegant solution for the problem. This way you don't have to include the base URL in every URL. – vonUbisch Jul 06 '15 at 09:07
-
Even though I have many folders inside the url? Or many folders inside the folders inside the URL? – Niel Sinel Jul 06 '15 at 09:12
-
1
You can use an absolute path
<a href = 'http//your.site.cpm/members/loans/loan-application-form.php'>
Advice : you can read this article
or this stack question
Or you can use ../
to navigate inside your relative path
Lets imagine your nav bar is located at /navigation/navbar.html
Then you can have a relative path like
<a href = '../members/loans/loan-application-form.php'>

- 1
- 1

- 3,699
- 1
- 29
- 44
-
How if the application is still on the localhost and If i use the absolute path of the localhost, it would be a problem If I put it on the web server because of C://........... what is the best way or is there any function that can solve this problem? – Niel Sinel Jul 06 '15 at 09:01
-
See my edit, you probably need to navigate through your directories instead of just adding /members... to the current directory – Ronan Quillevere Jul 06 '15 at 09:09
By specifying relative Urls like so:
<a href = 'members/loans/loan-application-form.php'>
You are simply stating you wish to go form the current page to this page, hence why it is being added to the end of the current URL. The best way to avoid this quite simply is to set an absolute URL like so:
<a href = 'http://projectname/members/loans/loan-application-form.php'>
Even when not using http://
it is often still considered relative so be sure to include this.
Another way to do the same but slightly quicker would be to add a variable in say your header file for example:
$url = 'http://example.com';
Then when specifying the URLs you can do say:
<a href = '<?php echo $url;?>/members/loans/loan-application-form.php'>
This just means that should you say change your domain, rather than editing every URL you can simply edit the variable value and be done with it.

- 4,586
- 6
- 39
- 73
-
-
@NielSinel I have never used this myself, according to this question http://stackoverflow.com/questions/1889076/is-it-recommended-to-use-the-base-html-tag some people are quite cautious of using this, which would cause me to be think otherwise, but you are free to try of course and see if it suits you. – The Humble Rat Jul 06 '15 at 09:12
-
@NielSinel You could also use a PHP variable to make this a bit easier, I have included further info in my answer. – The Humble Rat Jul 06 '15 at 09:19