PHP and MySQL Novice to Ninja Chapter 3 - welcome to our website Kevin

Hi im new to coding and as im sure youll tell my knowledge is very limited!
I have started reading PHP and MySQL Novice to Ninja. Im on chapter 3 and struggling with the ‘Passing variables in links’ task. Ive created a HTML file with the following:

<a href="name.php?name=Kevin">Hi, I&rsquo;m Kevin!</a>

This displays the link “Hi I’m Kevin”. Thats all fine but when i click on it, it should take me to a page saying “Welcome to our website Kevin” using the following code:

<body>
	<?php
	$name = $_GET['name'];
	echo 'Welcome to our website, ', $name . '!';
	?>
</body>

But when i click the link it takes me to a blank page, the URL it takes me to is:

file:///C:/xampp/htdocs/name.php?name=Kevin

When i enter the following URL:

http://localhost/name.html

it works fine?

Im wondering if ive installed something in the wrong place, im confused!

If anyone has any ideas id appreciate the help.

Thanks,

Rhiannon

In order to make it work, you need to be using the second type of link - opening via the http://localhost/ url. If you just double-click the html file in your explorer window, it will use the file associations to open it in the browser rather than via the server, which will mean that it doesn’t process the PHP code.

Ahh ok, that makes sense. It said in the book load the file in your browser - so i double clicked it as you said. So to make it work i had to type in the link “http://localhost/name.html”, so i’ll have to type it in future also?

Thanks very much for your help :smile:

With a link like “name.php?whatever”, the browser looks at it and says “There’s no domain information (www.whatever.com) in this link. So i’ll use whatever domain i’m CURRENTLY on. There’s no Directory information (link doesnt start with a /) so i’ll use the current directory also.”

That leads you to file:///C:/xampp/htdocs/ if you opened the original page as a file in that directory.

@rhi519 - Notice the protocol being used in the browser address, “file://”? This is the protocol used when you open a file directly from your hard drive. For testing web pages, you need to check that you’re using the “http://” protocol. In other words, using the localhost testing web service on your computer.

The link’s href value that you’re using, “name.php?name=Kevin”, is called a document relative value. The value is relative to the current URL in the browser. An example of an absolute value would be “http://localhost/name.php?name=Kevin”. Absolute values will work regardless of protocol. However the relative value is best for portability, i.e going live. Otherwise you’d be stuck with a bunch of links to localhost, which wont work of course.

In addition to the other answers, all of which are good info, you’ve also got a typo in your code:

<body>
	<?php
	$name = $_GET['name'];
	echo 'Welcome to our website, '. $name . '!';
	?>
</body>

Just before $name in the echo, that needs to be a period, and you have it currently as a comma.

I suspect if you fix that, view the page using http protocol (using a local or remote webserver like apache + PHP), it’ll work.

Thanks :slight_smile:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.