Jump to content

Do not want to use the _Request supergloabal in this code


LaserGunCarrier

Recommended Posts

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>

Link to comment
Share on other sites

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

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.