Problem with $_GET (getting variables from a href) - PHP & MySQL - Kevin Yank

I recently started reading PHP & MySQL Novice to Professional by Kevin Yank. I ran into a problem on the second example I attempted to use in the book.

So this is what I tried to do:

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

I’m trying to send firstname and lastname to $_GET, which the file name.php will handle. Which is located in the same folder.

Here’s the PHP:

$firstname = $_GET['firstname'];
$lastname = $_GET['lastname'];

echo 'Welcome to our website, ' . htmlspecialchars($firstname, ENT_QUOTES, 'UTF-8') . ' ' . htmlspecialchars($lastname, ENT_QUOTES, 'UTF-8') . '!';

The output looks like this:

Notice: Undefined index: firstname in C:\apache\htdocs\Novice2Professional\HTMLandPHP
ame.php on line 2

Notice: Undefined index: lastname in C:\apache\htdocs\Novice2Professional\HTMLandPHP
ame.php on line 3
Welcome to our website, !

I think the problem is with the & in the HTML. I could send one value just fine.

If you think the & is the problem try this:

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

See if you get the result you expect. I’m not sure why you are getting an undefined index on firstname though, as that looks to be properly defined and if you are receiving the firstname, then that shouldn’t be a problem…

try instead:

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

Then get used to adding a line like this to the top of your scripts, temporarily, rm them when you put on a live server:

// temp lines of debug
var_dump( $_GET);
echo '<hr />';

// carry on with your output ....

var_dump() is something you should be using all the time when expectations are not being met.

Or xDebug if you are using an IDE that has support for it :slight_smile:

xDebug is a good thing, not sure how many new ppl will dig it though. Good thing to mention all the same, the more ppl see it the more likely they will be to seek it out and give it a try.

hmmm… maybe that is too subtle:

Install xDebug and save your sanity.

Thanks, that did the trick:

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

Could anyone explain why this is the case though? I’m using eclipse for this, and I believe it is encoded with utf-8. If that makes any difference. Thank you for all of your replies though, I really appreciate it.

And cups, thanks for that, I’m going to try that out and see how it works. Not sure if that was the issue in this case though. But about xDebug, is there much that it can do that eclipse can’t?

xDebug is a tool that helps you debug your php code. I believe you can install it as a plugin for eclipse but don’t hold me to that statement. In short, you usually press a button to tell xDebug you want to debug your code. It then watches your php execution and you can set breakpoints in your code so you can watch the execution, see the values of your variables, etc.

Another neat feature it provides is a profiler. Where it will watch your code execution and tell you what methods/functions are being called the most often and how long it takes them to run.

Actually, no I cannot, and that put me off replying to your initial post for a while.

I had experience in the past of using & when I wanted to pass xhtml strict standards for my html output back in the day, and I had it working and I recall reading on the W3C site the reasons for that, but the facts have dribbled out of my head.

I fear it will not be not easy to say exactly why it will not work for you in this instance without seeing a lot more settings, I have a feeling it will have to do with some kind of doctype, but that is just a misty memory.

Unless anyone else pipes up with a definitive answer, I would just use & and get on with things.

Sorry to come back with such a crap answer.

I would like to point out one thing: & is not the problem - actually, when you use a URL (or any attribute value) in HTML/XHTML you have to use & - every & character must be encoded as &. Using & alone is incorrect and any HTML validator will raise an error. Sure, browsers accept plain & without problems but that is because this is one of the most common (and promoted) mistakes people make (even Google Map API docs contain incorrect <script> tags with unencoded & characters!).

& cannot be the problem as it is the correct way of encoding & in HTML. Unfortunately, I have no idea what the problem might be, it looks very strange to me. Most probably it’s some trivial error that is very hard to spot. If you were able to provide the actual source code of these two files that you find problematic - that would be great help - but not pasted here as text in this forum but archived in a zip file - sometimes there can be hard to spot problems with some special characters which are hard to notice or can be lost in text transmission.

Since there was discussion on xDebug, I wanted to point out that phpMaster just put out an article on xDebug that would be well worth your read.

Hmm. Could you elaborate on what exactly you would need in order to figure this out? When you say the source code, do you want me to zip the actual .php files (one includes HTML) or do you want the source from the output (HTML)?

It’s worth noting that utf-8 encoding is not working on the webpage either, for some reason. And I do include a <meta> tag for utf-8, and the project in eclipse (my code editor) was set to utf-8 as well, but still my browser insists on giving me question marks where I use characters like ú/í or something like that.

Oh and as for xDebug, I really appreciate you pointing that out to me. I’d love to see which part of my code runs slower than average, as well as use some breakpoints. My PHP is pretty cluttered with echo statements at the moment, since that’s what I use to see the content of my variables, and if parts of my code are reached. Is there a reputation system in place so I could upgrade you all a bit?

Edit: sorry, I first didn’t notice I actually replied to the wrong post! :smiley: Here is the corrected answer:

What I meant was that we can only see parts of your code and it doesn’t work although in should - logically. That’s why it would be good to see the complete code because the problem may be elsewhere. These things would be enough:

  1. All code of the file that displays the link.
  2. All code of the file that accepts $_GET data from the link and displays it on the page.

Zip for upload would be better because we would have 100% certainty we have the same code.

I edited my answer above since I initially confused this topic with a different thread!

Agreed. I went for the simplest approach first and to come back to this later.

First, open your web page in a browser, view source and verify that when you had & instead of & that it didn’t produce &amp; as your HTML output.

So edit your link to use &, open it in a webpage, view source, and see if you have &amp; or if it reads &

When doing a view source, copy and paste the HTML output shown in the browser here in [ html ] [ /html ] tags (without the extra spacing) so we can see what output was generated. Another thing you can do is set the content-type via the header() function in PHP.


header('Content-Type: text/html; charset=utf-8');

xDebug is a fantastic tool! If you have troubles setting it up, please start a new thread stating your IDE and your troubles (I feel a new thread will be of more use than trying to append it to this one ;)) As for reputation system, there isn’t one that I know of, but if you feel comfortable with it, nominate any of the above members in the SPF Community Awards

Thanks, I think I just might. Running into some problems with just trying to use the breakpoints.

As for the & issue, I honestly don’t know what to tell you, but it works now. I haven’t changed anything that I’m aware of, but I have restarted my computer since the first post.

I know for a fact that the example in the book works because I had no problem with it.

So, I went back to the book to see where you might have went wrong and here’s what I think it could be. In the book, Kevin Yank says “code up” an html page, but he only shows you the info that he wants you to drop into that html file. He assumes that you already KNOW to add the rest of the information. Thus name.html should look like this:

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>some title</title>
<a href="name.php?firstname=Kevin&amp;lastname=Yank">Hi, I&rsquo;m Kevin!</a>

Also, make sure that you are parsing the page through your web browser and not just finding and clicking on the file itself. Another thing, make sure that the editor that you are using is set to “utf-8” as well (most aren’t). If no of this works it might have something to do with your apache setup.

Good luck.

Thanks for the reply. It does work - and yeah, I do have to set it to utf-8 manually (haven’t had a look at how to make it default). But for some reason the code (unaltered) worked when I came back to it 2 days later.