sw45acp Posted February 14, 2008 Share Posted February 14, 2008 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?? Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted February 14, 2008 Share Posted February 14, 2008 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. Quote Link to comment Share on other sites More sharing options...
sw45acp Posted February 14, 2008 Author Share Posted February 14, 2008 .../_delete.php?id=7 Quote Link to comment Share on other sites More sharing options...
sw45acp Posted February 14, 2008 Author Share Posted February 14, 2008 that is the url that the form is working off of, using any random id Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 14, 2008 Share Posted February 14, 2008 When you use the form, you are using method="post", therefore the "id" is in the $_POST array, not the $_GET array, use: <?php if (isset($_POST["submit"])) { $id = $_POST['id']; echo $id; } ?> Ken Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted February 14, 2008 Share Posted February 14, 2008 that is the url that the form is working off of, using any random id Once the submit button is pressed, the URL isn't going to stick, unless you put it in the form action to do so. You can put the id in a hidden input and carry it over to the next page as Ken did in his example. Quote Link to comment Share on other sites More sharing options...
haku Posted February 14, 2008 Share Posted February 14, 2008 As his script is set up now, $_POST['id'] wont exist. But, unless the id variable is hard coded into the URL in the <form> tag, $_GET['id'] is also not going to exist. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 14, 2008 Share Posted February 14, 2008 Ah, I mis-read the code... Sorry Ken Quote Link to comment Share on other sites More sharing options...
sw45acp Posted February 14, 2008 Author Share Posted February 14, 2008 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 Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted February 14, 2008 Share Posted February 14, 2008 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. Quote Link to comment Share on other sites More sharing options...
sw45acp Posted February 14, 2008 Author Share Posted February 14, 2008 the page the form is displayed on is test.php?id=anny random id, this is generated from another page the form action = test.php, which is the same page how would I get it to save its value during the processing? Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted February 14, 2008 Share Posted February 14, 2008 Your form action should look like this: <form action="test.php?id=<?php echo $_GET['id']; ?>" method="post"> Quote Link to comment Share on other sites More sharing options...
sw45acp Posted February 14, 2008 Author Share Posted February 14, 2008 thank all of you for your help. I have no idea how you people know all this!! Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted February 14, 2008 Share Posted February 14, 2008 thank all of you for your help. I have no idea how you people know all this!! Just practice Don't forget to press solved. 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.