SitePoint Sponsor

User Tag List

Results 1 to 19 of 19
  1. #1
    SitePoint Member
    Join Date
    Nov 2012
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:

    HTML Code:
    <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:

    PHP Code:
    <?php
    $firstname 
    $_GET['firstname'];
    $lastname $_GET['lastname'];

    echo 
    'Welcome to our website, ' htmlspecialchars($firstnameENT_QUOTES'UTF-8') . ' ' htmlspecialchars($lastnameENT_QUOTES'UTF-8') . '!';
    ?>
    The output looks like this:

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

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

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

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,240
    Mentioned
    155 Post(s)
    Tagged
    0 Thread(s)
    If you think the &amp; is the problem try this:
    PHP Code:
    <a href="name.php?firstname=Kevin&lastname=Yank">HiI&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...

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    try instead:

    Code:
    <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:

    PHP Code:
    <?php
    // temp lines of debug
    if(isset($_GET)){
    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.

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,240
    Mentioned
    155 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    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

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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.

  6. #6
    SitePoint Member
    Join Date
    Nov 2012
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, that did the trick:

    HTML Code:
    <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.

  7. #7
    SitePoint Member
    Join Date
    Nov 2012
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?

  8. #8
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,240
    Mentioned
    155 Post(s)
    Tagged
    0 Thread(s)
    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.

  9. #9
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Nov21
    Could anyone explain why this is the case though?
    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 &amp; 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.

  10. #10
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Nov21 View Post
    I think the problem is with the &amp; in the HTML. I could send one value just fine.
    I would like to point out one thing: &amp; is not the problem - actually, when you use a URL (or any attribute value) in HTML/XHTML you have to use &amp; - every & character must be encoded as &amp;. 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!).

    &amp; 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.

  11. #11
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,240
    Mentioned
    155 Post(s)
    Tagged
    0 Thread(s)
    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.
    http://phpmaster.com/debugging-and-p...p-with-xdebug/

  12. #12
    SitePoint Member
    Join Date
    Nov 2012
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Lemon Juice View Post
    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.
    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.

  13. #13
    SitePoint Member
    Join Date
    Nov 2012
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?

  14. #14
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Edit: sorry, I first didn't notice I actually replied to the wrong post! Here is the corrected answer:

    Quote Originally Posted by Nov21 View Post
    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)?
    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.

  15. #15
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    I edited my answer above since I initially confused this topic with a different thread!

  16. #16
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,240
    Mentioned
    155 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Lemon Juice View Post
    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.
    Agreed. I went for the simplest approach first and to come back to this later.

    Quote Originally Posted by Nov21 View Post
    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)?
    First, open your web page in a browser, view source and verify that when you had &amp; instead of & that it didn't produce &amp;amp; as your HTML output.

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

    Quote Originally Posted by Nov21 View Post
    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.
    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.

    Example:
    PHP Code:
    header('Content-Type: text/html; charset=utf-8'); 
    Quote Originally Posted by Nov21 View Post
    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?
    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

  17. #17
    SitePoint Member
    Join Date
    Nov 2012
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, I think I just might. Running into some problems with just trying to use the breakpoints.

    As for the &amp; 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.

  18. #18
    SitePoint Member
    Join Date
    Nov 2012
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:

    HTML Code:
    <!doctype html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <title>some title</title>
    </head>
    <body>
    <p>
    <a href="name.php?firstname=Kevin&amp;lastname=Yank">Hi, I&rsquo;m Kevin!</a>
    </p>
    </body>
    </html>
    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.

  19. #19
    SitePoint Member
    Join Date
    Nov 2012
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 14u2ponder View 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:

    HTML Code:
    <!doctype html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <title>some title</title>
    </head>
    <body>
    <p>
    <a href="name.php?firstname=Kevin&amp;lastname=Yank">Hi, I&rsquo;m Kevin!</a>
    </p>
    </body>
    </html>
    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •