+ Post New Thread
Page 1 of 4 1234 LastLast
Results 1 to 15 of 47
Coding Thread, PHP form in Coding and Web Development; Have a couple of issues with this form and wondering if anyone can help? Here's the code I am using: ...
  1. #1

    Edu-IT's Avatar
    Join Date
    Nov 2007
    Posts
    7,160
    Thank Post
    403
    Thanked 623 Times in 569 Posts
    Rep Power
    181

    PHP form

    Have a couple of issues with this form and wondering if anyone can help?

    Here's the code I am using: http://pastebin.com/bEkihqgY



    The problems:
    • If you submit the empty form, the error validation returns back "Please fill in all the required fields and submit again." twice. How can I get it to say the name of the fields instead?
    • The profanity filter doesn't work on the name field?
    Last edited by Edu-IT; 6th May 2011 at 09:41 AM.

  2. #2

    CESIL's Avatar
    Join Date
    Nov 2006
    Location
    Hampshire
    Posts
    1,404
    Thank Post
    109
    Thanked 267 Times in 198 Posts
    Rep Power
    168
    Instead of outputting the error message each time an empty field is found you need to build a variable containing the names of the empty fields.

    Then after you have looped through POST values test if the variable contains any value and then output the error message including the list of empty fields.

    Another approach I have used in the past is to output a standard message and use css to highlight the empty fields by filling them with red or changing the border...

  3. #3

    CESIL's Avatar
    Join Date
    Nov 2006
    Location
    Hampshire
    Posts
    1,404
    Thank Post
    109
    Thanked 267 Times in 198 Posts
    Rep Power
    168
    Something like this would also do it
    Code:
    if (!isset($_POST[$field]) || empty($_POST[$field]))
         $error_msg .= "Please fill in the $field field and submit again.\r\n";
    }
    BTW I like the 'orgasim' entry in your profanity list...shows how good sex-ed is doesn't it

  4. #4

    Edu-IT's Avatar
    Join Date
    Nov 2007
    Posts
    7,160
    Thank Post
    403
    Thanked 623 Times in 569 Posts
    Rep Power
    181
    Not me who wrote the code. ;-)

    I'll adjust that, thanks. Any ideas why profanity doesn't cover the name field?
    Last edited by Edu-IT; 6th May 2011 at 11:55 AM.

  5. #5

    Edu-IT's Avatar
    Join Date
    Nov 2007
    Posts
    7,160
    Thank Post
    403
    Thanked 623 Times in 569 Posts
    Rep Power
    181
    Any ideas how to get the form to entered retain data on error?

  6. #6

    Hightower's Avatar
    Join Date
    Jun 2008
    Location
    Cloud 9
    Posts
    4,920
    Thank Post
    494
    Thanked 690 Times in 444 Posts
    Rep Power
    241
    Quote Originally Posted by Edu-IT View Post
    Any ideas how to get the form to entered retain data on error?
    PHP Code:
    <input type="text" name="email" value="<?php if (isset($_POST['email']) && !empty($_POST['email'])) echo $_POST['email']; ?>" />
    Last edited by Hightower; 6th May 2011 at 02:54 PM.

  7. #7

    CESIL's Avatar
    Join Date
    Nov 2006
    Location
    Hampshire
    Posts
    1,404
    Thank Post
    109
    Thanked 267 Times in 198 Posts
    Rep Power
    168
    Quote Originally Posted by Hightower View Post
    PHP Code:
    <input type="text" name="email" value="<?php if (isset($_POST['email']) && !empty($_POST['email'])) echo $_POST['username']; ?>" />
    shouldn't that last echo be outputting 'email'?

  8. #8

    Hightower's Avatar
    Join Date
    Jun 2008
    Location
    Cloud 9
    Posts
    4,920
    Thank Post
    494
    Thanked 690 Times in 444 Posts
    Rep Power
    241
    Quote Originally Posted by CESIL View Post
    shouldn't that last echo be outputting 'email'?
    It's 3 o'clock on Friday on the first week back from a 2 week break.....

  9. #9

    Edu-IT's Avatar
    Join Date
    Nov 2007
    Posts
    7,160
    Thank Post
    403
    Thanked 623 Times in 569 Posts
    Rep Power
    181
    I assume there's one of those for each field?

  10. #10

    Hightower's Avatar
    Join Date
    Jun 2008
    Location
    Cloud 9
    Posts
    4,920
    Thank Post
    494
    Thanked 690 Times in 444 Posts
    Rep Power
    241
    Quote Originally Posted by Edu-IT View Post
    I assume there's one of those for each field?
    Yep, just stick the PHP script in the value for each one and change the $_POST['email'] to whatever

  11. #11

    Edu-IT's Avatar
    Join Date
    Nov 2007
    Posts
    7,160
    Thank Post
    403
    Thanked 623 Times in 569 Posts
    Rep Power
    181
    Awesome, thanks! :-)

  12. #12

    Edu-IT's Avatar
    Join Date
    Nov 2007
    Posts
    7,160
    Thank Post
    403
    Thanked 623 Times in 569 Posts
    Rep Power
    181
    Where am I going wrong here? Certain fields don't line up.

    HTML Code:
    <style type="text/css">
    /*Contact form*/
    label {
    width:4em;
    float:left;
    text-align:right;
    margin-right:1.5em;
    display:block
    }
    .submit input {
    margin-left:4.5em;
    }
    input.text {
    border:1px solid [URL=http://www.edugeek.net/misc.php?do=dbtech_usertag_hash&hash=232a15]#232a15[/URL]
    margin:2px;
    border:1px solid [URL=http://www.edugeek.net/misc.php?do=dbtech_usertag_hash&hash=A8A8A8]#A8A8A8[/URL]
    font-family:Verdana,Arial,Helvetica,sans-serif;
    }
    textarea {
    border: 1px solid [URL=http://www.edugeek.net/misc.php?do=dbtech_usertag_hash&hash=232a15]#232a15[/URL]
    margin: 2px;
    border:1px solid [URL=http://www.edugeek.net/misc.php?do=dbtech_usertag_hash&hash=A8A8A8]#A8A8A8[/URL]
    font-family:Verdana,Arial,Helvetica,sans-serif;
    }
    fieldset {
    border:1px solid [URL=http://www.edugeek.net/misc.php?do=dbtech_usertag_hash&hash=232a15]#232a15[/URL]
    padding: 4px;
    border:1px solid [URL=http://www.edugeek.net/misc.php?do=dbtech_usertag_hash&hash=A8A8A8]#A8A8A8[/URL]
    }
    p.error, p.success {
       font-weight: bold;
       padding: 10px;
       border: 1px solid;
      }
      p.error {
       background: [URL=http://www.edugeek.net/misc.php?do=dbtech_usertag_hash&hash=ffc0c0]#ffc0c0[/URL]
       color: [URL=http://www.edugeek.net/misc.php?do=dbtech_usertag_hash&hash=900]#900[/URL]
      }
      p.success {
       background: [URL=http://www.edugeek.net/misc.php?do=dbtech_usertag_hash&hash=b3ff69]#b3ff69[/URL]
       color: [URL=http://www.edugeek.net/misc.php?do=dbtech_usertag_hash&hash=4fa000]#4fa000[/URL]
      }
    </style>
    <form action="<?php echo basename(__FILE__); ?>" method="post">
    <noscript>
      <p><input type="hidden" name="nojs" id="nojs" /></p>
    </noscript>
         <fieldset>
         <div>
         <label for="name">Name</label>
         <input type="text" class="text" name="name" id="name" size="30" value="<?php if (isset($_POST['name']) && !empty($_POST['name'])) echo $_POST['name']; ?>" />
        </div>
         <div>
         <label for="pupilsname">Pupils Name (if different)</label>
         <input type="text" class="text" name="pupilsname" id="pupilsname" size="30" value="<?php if (isset($_POST['pupilsname']) && !empty($_POST['pupilsname'])) echo $_POST['pupilsname']; ?>" />
        </div>
         <div>
         <label for="tutorgroup">Tutor group</label>
         <input type="text" class="text" name="tutorgroup" id="tutorgroup" size="30" value="<?php if (isset($_POST['tutorgroup']) && !empty($_POST['tutorgroup'])) echo $_POST['tutorgroup']; ?>" />
        </div>
        <div>
         <label for="pupilssportactivity">Pupils sport/activity</label>
         <input type="text" class="text" name="pupilssportactivity" id="pupilssportactivity" size="30" value="<?php if (isset($_POST['pupilssportactivity']) && !empty($_POST['pupilssportactivity'])) echo $_POST['pupilssportactivity']; ?>" />
        </div>
        <div>
         <label for="pupilsachievement">Pupils achievement</label>
         <textarea rows="6" cols="40" name="pupilsachievement" id="pupilsachievement" value="<?php if (isset($_POST['pupilsachievement']) && !empty($_POST['pupilsachievement'])) echo $_POST['pupilsachievement']; ?>" /></textarea>
        </div>
         <div>
         <label for="email">E-mail</label>
         <input type="text" class="text" name="email" id="email" size="30" value="" />
        </div>
        <div class="submit">
         <input type="submit" name="submit" id="submit" value="Submit" />
        </div>
        </fieldset>
       </form>
    Tutor group, pupils sport, pupils achievement all display out of line.

  13. #13

    Edu-IT's Avatar
    Join Date
    Nov 2007
    Posts
    7,160
    Thank Post
    403
    Thanked 623 Times in 569 Posts
    Rep Power
    181
    Also I notice that on submit, the fields still retain data. Can this be cleared on successful submit?

    And finally, how do I get the text area to retain entered data on error?

  14. #14

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,697
    Thank Post
    335
    Thanked 515 Times in 483 Posts
    Rep Power
    179
    Simple answer to first bit :P

    Your length of the name is too long. Change "name if different" etc, to "name" and it'll realign, as it's pushing it onto new line. (or incease size of column)

    Steve

  15. #15

    Edu-IT's Avatar
    Join Date
    Nov 2007
    Posts
    7,160
    Thank Post
    403
    Thanked 623 Times in 569 Posts
    Rep Power
    181
    Quote Originally Posted by Steve21 View Post
    Simple answer to first bit :P

    Your length of the name is too long. Change "name if different" etc, to "name" and it'll realign, as it's pushing it onto new line. (or incease size of column)

    Steve
    That's not it unfortunately.

    Edit: Another thing I just noticed, when a field is not submitted, is it possible to just have the 'please complete all fields' messages displayed just once?
    Last edited by Edu-IT; 26th May 2011 at 12:13 PM.

SHARE:
+ Post New Thread
Page 1 of 4 1234 LastLast

Similar Threads

  1. [PHP] Form to Email.php
    By tomgrindle in forum Web Development
    Replies: 4
    Last Post: 26th April 2011, 12:26 PM
  2. HTML/PHP form problem
    By glennda in forum Web Development
    Replies: 4
    Last Post: 17th November 2010, 10:22 AM
  3. PHP - Form POST script
    By BarryWAaMC in forum Coding
    Replies: 9
    Last Post: 31st March 2010, 11:32 AM
  4. [PHP] PHP Form Generator
    By _Bat_ in forum Web Development
    Replies: 1
    Last Post: 25th March 2009, 11:39 AM
  5. PHP form validation
    By thesk8rjesus in forum Web Development
    Replies: 10
    Last Post: 21st January 2009, 12:09 AM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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