SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Jul 2010
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    BYO Database Driven Website Using PHP & MySQL - Chapter 3 - Pages 109 - 112

    My code for all three files is exactly as written in the book, however, after submission, the form outputs the 'welcome.html.php' page even if nothing is entered in the 'firstname' box of the form.

    Is the problem that $_REQUEST['firstname'] is considered to have been set even though it's value is nothing?

    TIA

    index.php
    PHP Code:
    <?php
    if (!isset($_REQUEST['firstname']))
    {
        include 
    'form.html.php';
    }
    else
    {
        
    $firstname $_REQUEST['firstname'];
        
    $lastname $_REQUEST['lastname'];
        if (
    $firstname == 'Kevin' and $lastname == 'Yank')
        {
            
    $output 'Welcome, oh glorious leader!';
        }
        else
        {
            
    $output 'Welcome to our web site, ' .
                
    htmlspecialchars($firstnameENT_QUOTES'utf-8') . ' ' .
                
    htmlspecialchars($lastnameENT_QUOTES'utf-8') . '!';
        }
        
        include 
    'welcome.html.php';
    }
    ?>

    form.html.php
    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml1" xml:lang="en" lang="en">
    	<head>
    		<title>Form Example</title>
    		<meta http-equiv="content-type" content="text/html; charset=utf-8" />
    </head>
    <body>
        <form action="" method="post">
    		<div><label for="firstname">First name:
    			<input type="text" name="firstname" id="firstname" /></label>
    		</div>
    		<div><label for="lastname">Last name:
    			<input type="text" name="lastname" id="lastname" /></label>
    		</div>
    		<div><input type="submit" value="GO" /></div>
    	</form>
    </body>
    </html>

    welcome.html.php
    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml1" xml:lang="en" lang="en">
    	<head>
    		<title>Form Example</title>
    		<meta http-equiv="content-type" content="text/html; charset=utf-8" />
    </head>
    <body>
    	<p>
    		<?php echo $output; ?>
    	</p>
    </body>
    </html>

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,494
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    To check the contents of $_REQUEST (although I would personally use $_POST since your form's method is 'post'), you can add a print_r($_REQUEST) to the index.php:
    PHP Code:
    <?php
    print_r
    ($_REQUEST);
    if (!isset(
    $_REQUEST['firstname']))
    {
        include 
    'form.html.php';
    }
    else
    {
        
    $firstname $_REQUEST['firstname'];
        
    $lastname $_REQUEST['lastname'];
        if (
    $firstname == 'Kevin' and $lastname == 'Yank')
        {
            
    $output 'Welcome, oh glorious leader!';
        }
        else
        {
            
    $output 'Welcome to our web site, ' .
                
    htmlspecialchars($firstnameENT_QUOTES'utf-8') . ' ' .
                
    htmlspecialchars($lastnameENT_QUOTES'utf-8') . '!';
        }
        
        include 
    'welcome.html.php';
    }
    ?>
    It'll show everytime what is contained in $_REQUEST, the first time you call index.php, and then after the submit of the form.

  3. #3
    SitePoint Zealot
    Join Date
    Apr 2009
    Location
    Wenatchee, WA
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For the OP... I'm going thru this book as well, at a similar point also. I think you may be seeing some residual issues due to browser caching of values... try closing the browser and reloading the page from scratch.

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Are you submitting the form with a missing first name value?

    Check the HTML source code of the resulting (welcome) page carefully.

    Have you managed to get form.html.php to arrive at all after a false submission?

  5. #5
    SitePoint Member
    Join Date
    Dec 2012
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I to have the same problem! =(
    Can someone check why code does not work?
    If submitting empty form the welcome page is displayed with "Welcome to our web site, "
    The print_r shows that array is empty but I can not figure out why its not working......
    Tried with different browsers and still same result...

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Isolate the problem, make sure your form is at least correctly formed.

    Does this work for you?

    (put it in another file, test.php and see if it works.

    Check carefully what is dumped when you submit an empty form.

    PHP Code:
    <?php
    var_dump
    ($_POST);
    echo 
    ''<hr />';
    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml1" xml:lang="en" lang="en">
        <head>
            <title>Form Example</title>
            <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    </head>
    <body>
        <form action="" method="post">
            <div><label for="firstname">First name:
                <input type="text" name="firstname" id="firstname" /></label>
            </div>
            <div><label for="lastname">Last name:
                <input type="text" name="lastname" id="lastname" /></label>
            </div>
            <div><input type="submit" value="GO" /></div>
        </form>
    </body>
    </html>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •