SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    May 2002
    Location
    Oslo
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with form validation and $PHP_SELF

    I have this simple php-file:

    <html>

    <head>
    <title>Insert ad</title>
    </head>

    <body>

    <form ENCTYPE="multipart/form-data" action="<? echo $PHP_SELF; ?>" method="POST">
    <p>Name:
    <input type="text" name="name" size="20"></p>
    <p>Membernumber: <input type="text" name="membernumber" size="20"></p>
    <p>Picture:
    <input type="file" name="picture" size="20"></p>
    <p> </p>
    <p><input type="submit" value="Send" name="B1"><input type="reset" value="Reset" name="B2"></p>
    </form>

    </body>

    </html>

    <?
    require("/somepath/upload.php"); //class used for uploads
    require("/somepath/error_message.php"); //class used for generating errormessages

    if (!$name or !$membernumber or !$picture_name) //Here's the error. This if-test gets automatically executed when the page is loaded, causing the errormessage to be printed.
    {
    $error_message = new error_message($name,$membernumber,$picture_name,$picture);
    $error_message->standard_error_message("You didn't fill out all the fields.");
    }

    else
    {
    echo "Good! You've filled out all the fields:";
    echo "<br>$name";
    echo "<br>$membernumber";
    echo "<br>$picture_name";
    echo "<br>size: $picture_size";
    echo "<br>type: $picture_type";
    $upload = new upload($name,$membernumber,$picture_name,$picture,$picture_size,$picture_type);
    }

    ?>

    The script checks if the user has entered something in all the fields. If not, an errormessage is written. The problem is that the errormessage gets written when the page is loaded. The if-test should not be executed until the user presses the send-button. What's wrong with this script?
    Last edited by Pompiuses; May 14, 2002 at 07:23.

  2. #2
    gingham dress, army boots... silver trophy redux's Avatar
    Join Date
    Apr 2002
    Location
    Salford / Manchester / UK
    Posts
    4,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what's wrong is that PHP doesn't work this way. when a user requests a php page, the server executes all the scripts within it, then sends out the finished file to the user's browser.
    your assumption that it will display the html, then sit there waiting for the user to click submit, and only then execute the rest of the php code (namely the if statement) is fundamentally wrong.
    re·dux (adj.): brought back; returned. used postpositively
    [latin : re-, re- + dux, leader; see duke.]
    WaSP Accessibility Task Force Member
    splintered.co.uk | photographia.co.uk | redux.deviantart.com

  3. #3
    gingham dress, army boots... silver trophy redux's Avatar
    Join Date
    Apr 2002
    Location
    Salford / Manchester / UK
    Posts
    4,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, maybe i was being harsh in the last post. apologies.
    anyhow, here's how to fix this: you need to have a hidden field in your form, something like "is_submitted" or whatever. the whole code at the end (which you should really shift to the beginning, imho) is only executed if this variable is set.
    so
    PHP Code:
    <?php
    require("/somepath/upload.php"); //class used for uploads 
    require("/somepath/error_message.php"); //class used for generating errormessages 

    if ($_POST['is_submitted']==1) {
        if (!
    $name or !$membernumber or !$picture_name//Here's the error. This if-test gets automatically executed when the page is loaded, causing the errormessage to be printed. 
        

            
    $error_message = new error_message($name,$membernumber,$picture_name,$picture); 
            
    $error_message->standard_error_message("You didn't fill out all the fields."); 
        } 
        else 
        { 
            echo 
    "Good! You've filled out all the fields:"
            echo 
    "<br>$name"
            echo 
    "<br>$membernumber"
            echo 
    "<br>$picture_name"
            echo 
    "<br>size: $picture_size"
            echo 
    "<br>type: $picture_type"
            
    $upload = new upload($name,$membernumber,$picture_name,$picture,$picture_size,$picture_type); 
        }
    }

    ?> 
    <html> 

    <head> 
    <title>Insert ad</title> 
    </head> 

    <body> 

    <form ENCTYPE="multipart/form-data" action="<? echo $PHP_SELF?>" method="POST"> 
    <p>Name: 
    <input type="hidden" name="is_submitted" value="1">
    <input type="text" name="name" size="20"></p> 
    <p>Membernumber: <input type="text" name="membernumber" size="20"></p> 
    <p>Picture: 
    <input type="file" name="picture" size="20"></p> 
    <p> </p> 
    <p><input type="submit" value="Send" name="B1"><input type="reset" value="Reset" name="B2"></p> 
    </form>

    </body> 
    </html>
    re·dux (adj.): brought back; returned. used postpositively
    [latin : re-, re- + dux, leader; see duke.]
    WaSP Accessibility Task Force Member
    splintered.co.uk | photographia.co.uk | redux.deviantart.com


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
  •