Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Empty Form Field Validation?

  • This topic is empty.
Viewing 15 posts - 16 through 30 (of 58 total)
  • Author
    Posts
  • #159347
    MBM
    Participant

    The array is named $message not errMessage. I renamed it errMessage just in case. I must have made a type somewhere as although I still don’t get an error message in the address bar I see :

    message=Array

    #159407
    __
    Participant

    The array is named $message not errMessage. I renamed it errMessage just in case.

    No, you’re right: $message is correct. My mistake. I’m not sure what’s going on.

    Try this: under

    if( $errMessage ){
    

    add this line and see what it prints:

    var_dump( $errMessage ); exit;
    
    #159419
    MBM
    Participant
    if($formValue['forename']==""){
    $message [] = "Please enter your forename" ;
    var_dump( $message ); exit;
    header("Location: register.php?message=$message");
    

    No data entered :

    array(1) { [0]=> string(23) "Please enter a forename" }

    Forename entered, no other data entered :

    array(1) { [0]=> string(22) "Please enter a surname" }

    Forename & surname entered, no other data entered :

    array(1) { [0]=> string(23) "Please enter a username" }

    etc, etc

    Two of the strings have the same value (23). Is that the problem?

    And the address bar reads :

    register.php?CreateRecord=1

    Which is from :

    <form action="register.php?CreateRecord=1" method="post" enctype="multipart/form-data" name="form1" id="registerform">

    #159422
    __
    Participant

    Two of the strings have the same value (23). Is that the problem?

    No; 23 is the number of characters in the string, not the value.

    array(1)

    To clarify, you only get one element in the array, even when you enter no data?

    What version of PHP are you running?

    Would you also post the complete current script you are using (perhaps make a gist on github)?

    #159423
    MBM
    Participant

    Correct. Only one element is displayed regardless of how many of the fields are filled in.

    PHP Version 5.3.3.

    The registration form php (content removed) :

    https://gist.github.com/gyprosetti/f9606ea721650f4756be

    common.php :

    https://gist.github.com/gyprosetti/715e2344708256acaf23

    I removed the links to my css and uploaded and test that, just in case, but it didn’t make a difference.

    #159424
    __
    Participant

    You’re still putting a header call with every test. Once you send that Location header, the client (browser) abandons you script in favor of the new location. It will never see anything after the first one.

    Compare this to the example I posted, where the Location header is sent only after all of the checks have been completed.

    #159443
    MBM
    Participant

    There’s something wrong with my syntax :

    if(isset($_POST['Submit'])){
    if($formValue['forename']==""){
    $errmessage[] = "Please enter a forename" ;
    }elseif($formValue['surname']==""){
    $errmessage[] = "Please enter a surname" ;
    }elseif($formValue['username']==""){
    $errmessage[] = "Please enter a username" ;
    }elseif($formValue['password']==""){
    $errmessage[] = "Please enter a password" ;
    }elseif($formValue['email']==""){
    $errmessage[] = "Please enter your email address" ;
    if( $errmessage ){
    $message = implode( "<br>",$errmessage );
    header( "Location: register2.php?message=$message" );
    exit;
    }
    }else{
    DBConnect();
    
    #159460
    __
    Participant

    If you indent your code, it will be much easier to pick out syntax and logical errors.

    Beyond that, however, you should be using a code editor that can take care of basics like this for you. I use Komodo, which is free (as in beer) and runs on Linux/Mac/Windows. It’s an amazing piece of software. (If you’re serious about coding, the full IDE is well worth its price as well. Unpaid testimonial.)

    <?php
    if(isset($_POST['Submit'])){    
        if($formValue['forename']==""){
            $errmessage[] = "Please enter a forename" ;
        }
        elseif($formValue['surname']==""){
            $errmessage[] = "Please enter a surname" ;
        }
        elseif($formValue['username']==""){
            $errmessage[] = "Please enter a username" ;
        }
        elseif($formValue['password']==""){
            $errmessage[] = "Please enter a password" ;
        }
        elseif($formValue['email']==""){
            $errmessage[] = "Please enter your email address" ;
            if( $errmessage ){
                $message = implode( "<br>",$errmessage );
                header( "Location: register2.php?message=$message" );
                exit;
            }
        }else{
            DBConnect();
    

    See it? You’re missing at least two closing brackets, and I suspect your if( $errmessage ) is not where you meant it to be. There’s a few other things, as well.

    <?php
    if(isset($_POST['Submit'])){    
        if($formValue['forename']==""){
            $errmessage[] = "Please enter a forename" ;
        }
    
        // use if(), not elseif().
        // elseif() means you'll only deal with one error at a time.
        if($formValue['surname']==""){
            $errmessage[] = "Please enter a surname" ;
        }
        if($formValue['username']==""){
            $errmessage[] = "Please enter a username" ;
        }
        if($formValue['password']==""){
            $errmessage[] = "Please enter a password" ;
        }
        if($formValue['email']==""){
            $errmessage[] = "Please enter your email address" ;
    
        // I think one of your closing brackets was meant to go here:
        }
        if( $errmessage ){
            $message = implode( "<br>",$errmessage );
    
            // the "Location" header requires a FULLY QUALIFIED URL.
            // you _must_ include the schema and domain name.
            // the path alone is incorrect, 
            //  and won't work in all browsers under all circumstances.
            header( "Location: http://example.com/register2.php?message=$message" );
            exit;
        }
    }else{
            DBConnect();
    
    // this is the other missing bracket
    }
    
    #159475
    MBM
    Participant

    I’m getting :

    Parse error: syntax error, unexpected '}' in /web/users/j9136124/rating/register2.php on line 44

    When I remove it, it flags another error.

    In the header locations I have :

    header( "Location: https://mywebsite.com/register2.php?message=$message" );

    #159491
    __
    Participant

    I’m getting :

    Parse error: syntax error, unexpected '}' in /web/users/j9136124/rating/register2.php on line 44

    Is this using the code you showed me above?

    Have you tried reformatting it (indenting your code)?

    Keep in mind that when PHP finds an error, it isn’t necessarily the exact line you need to fix. PHP has no way of knowing what you wanted to do; it just knows what line it was on when things got so bad it had to stop parsing. Often times, the mistake you actually need to fix is a few lines above, so make sure to check before simply deleting whatever is on the line PHP reported.

    In the header locations I have :

    header( "Location: https://mywebsite.com/register2.php?message=$message" );

    Good.

    #159499
    MBM
    Participant

    I’ve added the code as you posted it, complete with indents. The updated gist is here :

    https://gist.github.com/gyprosetti/f9606ea721650f4756be

    #159603
    __
    Participant

    Okay, look here:

    }else{ 
        DBConnect();
    
    // this is the other missing bracket 
    } 
    $Link = mysql_connect( // . . .
    

    I suggested that closing bracket because the else block was open, and that was the end of the code you posted.

    Doing this (adding the rest of your code after) means that DBConnect() will only happen if the form was not submitted, while the $Link ... and all of the following code will always happen.

    Is that what you intended? or should $Link ... be inside the first if (i.e., “happens when the form is submitted and there were no errors”)?

    I’ve added the code as you posted it, …

    Note that I want to see the code you’re actually using

    …which may or may not be the code I showed you. : )

    #159604
    MBM
    Participant

    $Link = mysql_connect($Host, $User, $Password);

    Is after the DBConnect();

    I’ve updated the github and removed non relevant HTML content.

    https://gist.github.com/gyprosetti/f9606ea721650f4756be

    #159870
    MBM
    Participant

    I’ve debugged the code but it’s still not working as it should, the error messages are displayed in the address bar and not the page itself.

    I’ve tried :

    <?php print $message ; ?>
    

    To display the messages.

    #159871
    Alen
    Participant

    Try using

    <?php echo $_POST['message']; ?>
    
Viewing 15 posts - 16 through 30 (of 58 total)
  • The forum ‘Back End’ is closed to new topics and replies.