Jump to content

[SOLVED] global variable problem


sw45acp

Recommended Posts

this has gotta be the most annoying thing: i'm trying to retrieve a variable from the url bar, and then write it out when the user presses the submit button.  all I need is for someone to be able to press the button, like OK to delete something

 

<?php
//this works fine...
$id = $_GET["id"];
echo $id;

//but this doesn't...
$id = $_GET["id"];

if (isset($_POST["submit"]))
{
    global $id;
    echo $id;
}
?>

and the form code

<form action="same page as script" method="post">
    <input type="submit" name="submit" id="submit" value="submit" />
</form>

 

i have no clue what's wrong here, even delcaring id as a global variable wont work, i have tried setting form to method="get", that doesnt work, $_REQUEST doesn't do it, I do have superglobals and register_globals on

 

any clues??

Link to comment
Share on other sites

You shouldn't have to use global to use the variable $id inside the IF statement.

 

<?php
//this works fine...
$id = $_GET["id"];

//but this doesn't...
$id = $_GET["id"];

 

...They both look identical to me.

 

Are you positive "id" is set in the URL? Copy and paste your URL AFTER you submit the form.

 

 

Link to comment
Share on other sites

so in other words, this wouldnt work?

<?php
isnt GET used to get urls from address bar?
//this works fine
$id = $_GET["id"];
echo $id;

//yet again this doesnt
if (isset($_POST["submit"]))
{
echo $id;
}

?>
<form action="same as page" method="post">
<input type="submit" name="submit" value="submit" />
</form>

example url: http://www.test.com/test.php?id=17, this is NOT the form action

Link to comment
Share on other sites

Well, what does your form action look like? Where it says "same as page".

 

if your url looks like this: test.php?id=17 WHILE the form is displaying and the form action isn't set to keep the id in the URL, then no...it isn't going to work. When you click submit it's just going to clear all the GET information from the URL unless you have it set to stay.

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.