SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Guru etidd's Avatar
    Join Date
    May 2011
    Location
    atlanta, ga
    Posts
    632
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Question Script Not Catching Empty Name Field

    Hello all,

    I have a question about the home page e-mail submission box for my website. When you interact and get to the second form, the script does not output an error message if the name field is left empty.

    The script:
    PHP Code:
    <?php
        $instance 
    = new CheckForm;
        
    $instance -> checkSubmission();
        
        class 
    CheckForm
        
    {
            public function 
    checkSubmission()
            {    
                
    $response = array("validation" => " ""message" => " ""database" => " ");    
                if (
    $_POST['country'] != "Select Country")
                {
                    if (!
    is_null($_POST['confirmEmail']) && !is_null($_POST['name']))
                    {
                        
    $origEmail $_POST['origEmail'];
                        
    $confirmEmail $_POST['confirmEmail'];
                        if (
    $origEmail == $confirmEmail)
                        {
                            
    $name htmlspecialchars($_POST['name']);
                            
    $ageRange $_POST['age'];
                            
    $country $_POST['country'];
                            
                            require_once(
    "categoryfinder.php");
                            
    $categoryFinder = new CategoryFinder;
                            
    $category $categoryFinder -> getCategory();
                            
    $response = array("validation" => "pass""message" => "Thanks for joining the e-mail list, <b>" $name "</b>, under the e-mail address, <b>" $confirmEmail "</b>.""database" => "pass");
                            try{
                                require_once(
    'databasewriter.php');
                                
    $dbWriter = new DatabaseWriter;
                                
    $dbWriter -> writeUserToDatabase($confirmEmail$name$ageRange$country$category);
                            } catch (
    PDOException $e) {
                                
    $response = array("validation" => "pass""message" => "Thanks for joining the e-mail list, <b>" $name "</b>, under the e-mail address, <b>" $confirmEmail "</b>.""database" => "fail");
                            }

                        } else {
                            
    $response = array("validation" => "fail""message" => "E-mail addresses don't match.""database" => "fail");
                        }
                    } else {
                        if (
    is_null($_POST['confirmemail'])){
                            
    $response = array("validation" => "fail""message" => "Confirmation e-mail not entered.");
                        } elseif (
    is_null($_POST['name'])) {    
                            
    $response = array("validation" => "fail""message" => "Please enter a name.");
                        }
                    }
                } else {
                    
    $response = array("validation" => "fail""message" => "Please select a country.");
                }
                echo 
    json_encode($response);
            }
        }
    ?>

    What seems to be the issue? :cap:

  2. #2
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if (!is_null($_POST['confirmEmail']) && !is_null($_POST['name'])) 
    This says If 'confirmEmail' and (&&) 'name' are both null.

    What you want is
    PHP Code:
    if (!is_null($_POST['confirmEmail']) || !is_null($_POST['name'])) 
    If either 'confirmEmail' OR (||) 'name' are null....
    Denny Schlesinger
    web services

  3. #3
    SitePoint Guru etidd's Avatar
    Join Date
    May 2011
    Location
    atlanta, ga
    Posts
    632
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yeah, you're right!

    But it's still not posting that particular error to the #errormessage <p> element, whereas if I don't select a country it says "Please select a country" like it should.

    I think it may be due to the nested if statement near the end of the script.

  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)
    Perhaps you could "fail early" on each of your tests, rather than invoke a shed-load of indentation ....

    PHP Code:
    if ($_POST['country'] === "Select Country") { 
        
    $response = array("validation" => "fail""message" => "Please select a country.");


  5. #5
    SitePoint Guru etidd's Avatar
    Join Date
    May 2011
    Location
    atlanta, ga
    Posts
    632
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Exclamation

    All right, I tried mixing this PHP script up, but I'm still not seeing an error message if the name is left empty!

    PHP Code:
    <?php
        $instance 
    = new CheckForm;
        
    $instance -> checkSubmission();
        
        class 
    CheckForm
        
    {
            public function 
    checkSubmission()
            {    
                
    $origEmail $_POST['origEmail'];
                
    $confirmEmail htmlspecialchars($_POST['confirmEmail']);
                
    $name htmlspecialchars($_POST['name']);
                
    $ageRange $_POST['age'];
                
    $country $_POST['country'];
                
    $status 0;
            
                
    $response = array("validation" => " ""message" => " ""database" => " ");
            
                if (
    is_null($confirmEmail) && is_null($name) && $country === "Select Country") {
                    
    $response = array("validation" => "fail""message" => "That's not a valid submission.");
                } elseif (
    $country === "Select Country") { 
                    
    $response = array("validation" => "fail""message" => "Please select a country.");
                } elseif (
    is_null($name)) {
                    
    $response = array("validation" => "fail""message" => "Please enter a name.");
                } elseif (
    is_null($confirmEmail)) {
                    
    $response = array("validation" => "fail""message" => "No confirmation e-mail was entered.");
                } elseif (
    $origEmail != $confirmEmail) {
                    
    $response = array("validation" => "fail""message" => "E-mail addresses don't match.");
                } else
                    
    $status 1;
                
                if (
    $status === 1) {
                            require_once(
    "categoryfinder.php");
                            
    $categoryFinder = new CategoryFinder;
                            
    $category $categoryFinder -> getCategory();
                            
    $response = array("validation" => "pass""message" => "Thanks for joining the e-mail list, <b>" $name "</b>, under the e-mail address, <b>" $confirmEmail "</b>.""database" => "pass");
                            try{
                                require_once(
    'databasewriter.php');
                                
    $dbWriter = new DatabaseWriter;
                                
    $dbWriter -> writeUserToDatabase($confirmEmail$name$ageRange$country$category);
                            } catch (
    PDOException $e) {
                                
    $response = array("validation" => "pass""message" => "Thanks for joining the e-mail list, <b>" $name "</b>, under the e-mail address, <b>" $confirmEmail "</b>.""database" => "fail");
                            }
                }

                echo 
    json_encode($response);
            }
        }
    ?>
    Thanks!

  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)
    PHP Code:
    if(empty($name)){




  7. #7
    SitePoint Guru etidd's Avatar
    Join Date
    May 2011
    Location
    atlanta, ga
    Posts
    632
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Thanks, Cups!


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
  •