LaserGunCarrier Posted February 25, 2012 Share Posted February 25, 2012 Any ideas on how to make this code usable without using the $_REQUEST superglobal? Here is my code: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title></title> </head> <body> <?php if($_GET['submit'] == "Change Background Color") { $bgcolor = strval($_POST['thecolor']); } else { $bgcolor = "red"; } ?> <body bgcolor="<?= $bgcolor; ?>"> <form name="color" method="get" action="<?= $_SERVER['PHP_SELF']; ?>"> Enter a color: <input type="text" name="thecolor" value=""> <input type ="submit" value="Change Background Color"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/257735-do-not-want-to-use-the-_request-supergloabal-in-this-code/ Share on other sites More sharing options...
joel24 Posted February 25, 2012 Share Posted February 25, 2012 You don't need to use $_SERVER superglobal, if you omit the form action, it will post to itself by default. As for your dilemma, you are sending the form via GET and retrieving values via POST and GET; you need to use one or the other - stick with POST. <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title></title> </head> <body> <?php if(isset($_POST['submit'])) //just check if the form is being posted { $bgcolor = strval($_POST['thecolor']); } else { $bgcolor = "red"; } ?> <body bgcolor="<?= $bgcolor; ?>"> <form name="color" method="post"> Enter a color: <input type="text" name="thecolor" value=""> <input type ="submit" name='submit' value="Change Background Color"> </form> *edit - made it a bit prettier and set the form submit button name Quote Link to comment https://forums.phpfreaks.com/topic/257735-do-not-want-to-use-the-_request-supergloabal-in-this-code/#findComment-1321014 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.