Jump to content

If string exists in current url echo $_GET


NoamChomsky

Recommended Posts

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!

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.";
}

 

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.