Jump to content

If string exists in current url echo $_GET


NoamChomsky
Go to solution Solved by Jacques1,

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!

Link to comment
Share on other sites

  • Solution

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.";
}
Link to comment
Share on other sites

 

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.