Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,466
    Thanks
    71
    Thanked 104 Times in 103 Posts

    Display multiple fields that are blank

    I am trying to create an error alert for all fields that are blank. Instead of having about 20 different IF statements to change for blank field, username exists, etc. How would I go about doing that?
    This is what I have for a single alert (which works great) but I'd like to display multiple into it.

    Thanks!

    PHP Code:
    if ($username == "") {
           ?>
            <script type="text/javascript">
        alert("The username field is blank, Please enter a username of at least 3 characters.");
        history.back();
      </script>
           <?php
          
    }
          
          if ((
    $password == "") || $password '4') {
            echo 
    "Password is either blank or must be at least 4 characters long. <br />";
            die;
          }
          
          if (
    $email == "") {
            echo 
    "Email address is blank. <br />";
            die;
          }
    Last edited by myfayt; 04-08-2014 at 03:39 AM.
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    $_POST is an array, so you can simply iterate it:
    PHP Code:
    foreach ($_POST AS $key => $item)
    {
       if (empty(
    $item))
       {
          
    printf("%s is blank, please add content"$key);
       }

    $key would have to be the proper name to display, or you can write an array to match them up.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,778
    Thanks
    6
    Thanked 1,021 Times in 994 Posts
    I hope the JS alert and history.back() method have just sneaked in there by accident?

  • #4
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,466
    Thanks
    71
    Thanked 104 Times in 103 Posts
    So you are saying do this?

    PHP Code:
    $array = array(
        
    "username" => "username",
        
    "password" => "password",
    );

    foreach (
    $_POST AS $array => $item)
    {
       if (empty(
    $item))
       {
          
    printf("%s is blank, please add content"$array);
       }

    Been a sign maker for 7 years. My business:
    American Made Signs

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Surely as you test each field to see if the content is valid you'd catch any blank fields because they don't contain the necessary characters. I'd have thought that there would be no need to separately test if they are blank.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #6
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,466
    Thanks
    71
    Thanked 104 Times in 103 Posts
    Or something more like this?

    PHP Code:
    $arr = (usernamepasswordcharnameemailfullnamefulladdressdob);
          foreach (
    $_POST AS $arr => $item)
    {
       if (empty(
    $item))
       {
          
    printf("%s is blank, please add content"$arr);
       }

    Been a sign maker for 7 years. My business:
    American Made Signs

  • #7
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    852
    Thanks
    173
    Thanked 94 Times in 94 Posts
    PHP Code:
    <?php
    if ( isset( $_POST['secrethash'] ) ) {
    $notice = array('txt1'=>'Name''txt2'=>'Address''txt3'=>'Whatever');
        foreach (
    $_POST as $key => $item)
        { 
               if (empty(
    $item)) 
               { 
                       
    printf("%s is required, please add content<br />"$notice[$key]); 
               } 
        } 
    }
    ?>
    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Untitled Document</title>
    </head>

    <body>
    <form action="" method="post">
    <input type="text" value="" name="txt1">
    <input type="text" value="" name="txt2">
    <input type="text" value="" name="txt3">
    <input type="submit" value="Send" name="submit">
    <input type="hidden" value="hashvalue" name="secrethash">
    </form>
    </body>
    </html>
    "The greatest revenge is to accomplish what others say you cannot do."
    ~ Unknown

    I used to be indecisive, but now I'm not so sure.

  • Users who have thanked low tech for this post:

    myfayt (04-08-2014)

  • #8
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,466
    Thanks
    71
    Thanked 104 Times in 103 Posts
    Thank you, it appears to be working fine.
    Been a sign maker for 7 years. My business:
    American Made Signs


  •  

    Posting Permissions

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