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>

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

Archived

This topic is now archived and is closed to further replies.

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