NoamChomsky Posted July 2, 2014 Share Posted July 2, 2014 Hi all - I'm trying to echo $_GET something from the URL, and display a short message to the user if the echo $_GET fails - but I'm new to PHP and the code I've whipped up doesn't work. It always displays the message even if the information is present in the url and the $_GET should work. Here's the code I've got: <h3> <?php $qset = strpos($_SERVER['SCRIPT_NAME'], 'qtitle'); if ($qset === false) { echo "You haven't added any questions yet."; } else { echo $_GET['qtitle']; } ?> </h3> The URL will contain "qtitle=(something)" if they've filled out a form on the prior page. Can someone tell me what I'm doing wrong? I have a feeling I've overcomplicated things... Thanks again! Quote Link to comment Share on other sites More sharing options...
Solution Jacques1 Posted July 2, 2014 Solution Share Posted July 2, 2014 The $_SERVER['SCRIPT_NAME'] variable contains the filename of the script, nothing else. The parameters after the question mark are not included. I'm not really sure why you're using this strpos thing, anyway. You obviously know the $_GET array, so why not use that? if (isset($_GET['qtitle'])) { echo $_GET['qtitle']; } else { echo "You haven't added any questions yet."; } Quote Link to comment Share on other sites More sharing options...
NoamChomsky Posted July 2, 2014 Author Share Posted July 2, 2014 The $_SERVER['SCRIPT_NAME'] variable contains the filename of the script, nothing else. The parameters after the question mark are not included. I'm not really sure why you're using this strpos thing, anyway. You obviously know the $_GET array, so why not use that? if (isset($_GET['qtitle'])) { echo $_GET['qtitle']; } else { echo "You haven't added any questions yet."; } Thanks - that worked. I can't lie about this - I don't know PHP at all well at this stage. I'm a front-end guy prototyping an interface that will then be made to work by a backend guy. Which is why my code was so ridiculous. I didn't know you could check if what you're trying to GET is set in that way, in researching an answer I read up on something that was more complicated than it needed to be. Cheers for the help, much appreciated! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.