1 Replies - 237 Views - Last Post: 22 April 2011 - 10:09 PM Rate Topic: -----

#1 redfox1160  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 22-April 11

How could I make a variable that changes when a form is filled out?

Posted 22 April 2011 - 08:17 PM

I have an associative array with movies and the dates they won oscars. I want to let a user guess the year of a random movie that is displayed. If they put the year in wrong, I don't want the movie to change, but I cant seem to do this. This is my code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        //get form data
        extract($_POST);

        $awardWinners = array(
            "annie hall" => "1977",
            "around the world in 80 days" => "1956",
            "slumdog millionaire" => "2008",
            "dances with wolves" => "1990",
            "the departed" => "2006",
            "crash" => "2005",
            "Casablanca" => "1943",
            "cariots of fire" => "1981",
            "patton" => "1970",
            "the hurt locker" => "2009"
        );

        for ($x=3; $x >= 0; $x--){
        
            if ($x == 3){
                $q = rand(0,9);
                $movieNames = array_keys($awardWinners);
                $movie = $movieNames[$q];
            }
            if (!preg_match('/\d$/', $_POST['answer'])){
                echo "Please enter a year!<br />";
            }

        }
        ?>
        <h4> Test Your Academy Award Trivia Skills</h4>
        <h4>Give the year the movie below won the Academy Award</h4>
        <form method="post" action="AcademyAward.php">
            <label>Movie:</label><br />
            <input type="text" readonly="readonly" name="movie" size="45" value="<?php echo "$movie"; ?>" /><br /><br />
            <label>Year it won the Oscar: </label> <input type="text" name="answer" value="" /><br />
            <input type="submit" name="submit" value="Get Result"/>
        </form>
    </body>
</html>



I have no clue how to make this work. Right now (as you can see) i'm trying to use a for loop, but it doesn't wait for a user's response. Can someone help me?

Is This A Good Question/Topic? 0
  • +

Replies To: How could I make a variable that changes when a form is filled out?

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10194
  • View blog
  • Posts: 37,646
  • Joined: 27-December 08

Re: How could I make a variable that changes when a form is filled out?

Posted 22 April 2011 - 10:09 PM

In your PHP block, you can check if($_POST) before you run your loop. That will check if the user has submitted the form.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1