Professional Web Applications Themes

Rejecting blank fields in forms - PHP Development

Hi All, I hope that someone maybe able to help me with this question. At the moment I have the following line of code that checks if a field is blank: if($name == "") {echo "<p><b>PLEASE CLICK 'BACK' ON YOUR BROWSER AND ENTER A VALID NAME</b>";} else {echo "Name: $name";}; But is there a command that could just detect a null and go straight back to the form page rather than requesting that the user clicks the back button. I know this is probably a pretty simple problem for some of the pros, but I'm a real newbie to PHP! ...

  1. #1

    Default Rejecting blank fields in forms

    Hi All,

    I hope that someone maybe able to help me with this question. At the moment
    I have the following line of code that checks if a field is blank:

    if($name == "") {echo "<p><b>PLEASE CLICK 'BACK' ON YOUR BROWSER AND ENTER A
    VALID NAME</b>";} else {echo "Name: $name";};

    But is there a command that could just detect a null and go straight back to
    the form page rather than requesting that the user clicks the back button. I
    know this is probably a pretty simple problem for some of the pros, but I'm
    a real newbie to PHP! Of course, a few suggestions on a more elegant way to
    implement this check would also be very interesting to read!

    Thanks if you can help!

    Steve


    Steve Guest

  2. #2

    Default Re: Rejecting blank fields in forms


    "Ian.H" <ianWINDOZEdigiserv.net> wrote in message
    news:pan.2004.04.05.13.16.47.859000bubbleboy.digi serv.net...
    > On Mon, 05 Apr 2004 13:11:39 +0000, Ian.H wrote:
    >
    > > On Mon, 05 Apr 2004 12:26:13 +0000, Steve wrote:
    > >
    > >> Hi All,
    > >>
    > >> I hope that someone maybe able to help me with this question. At the
    moment
    > >> I have the following line of code that checks if a field is blank:
    > >>
    > >> if($name == "") {echo "<p><b>PLEASE CLICK 'BACK' ON YOUR BROWSER AND
    ENTER A
    > >> VALID NAME</b>";} else {echo "Name: $name";};
    > >>
    > >> But is there a command that could just detect a null and go straight
    back to
    > >> the form page rather than requesting that the user clicks the back
    button. I
    > >> know this is probably a pretty simple problem for some of the pros, but
    I'm
    > >> a real newbie to PHP! Of course, a few suggestions on a more elegant
    way to
    > >> implement this check would also be very interesting to read!
    > >>
    > >> Thanks if you can help!
    > >>
    > >> Steve
    > >
    > >
    > > Hi Steve..
    > >
    > > Yes, 'isset()' will detect _NULL_ but this isn't really what you want =)
    > >
    > > There's a similar function... 'empty()' but be aware, this will also
    match
    > > '0' (zero) as empty.
    > >
    > >
    > > if (empty($foo)) {
    > >
    > >
    > > The other way is strlen() (assuming it's a string):
    > >
    > >
    > > if (strlen($foo) < 1) {
    > >
    > >
    > > Like many things.. there's more than one way to do it.. much will depend
    > > on your preferred coding style.. just watch out for the empty() and
    zeros
    > > if applicable =)
    >
    >
    > Should have also mentioned (for the rest of your query).. you can use the
    > header() function to perform a redirect back to your form page (see
    > <http://php.net/> for more info on this) if this fails, for example:
    >
    >
    > if (strlen($_POST['username']) < 1) {
    > header('Location: [url]http://.............com/form.php');[/url]
    > exit;
    > } else {
    > /* Do more processing............. */
    > }
    >
    >
    > Personally, I normally send the form back to the originating script and
    > use a condition:
    >
    >
    > if (!empty($_POST['submit'])) {
    > /* Do processing of form... */
    >
    > } else {
    > /* Display form HTML etc */
    > }
    >
    >
    > and for the form action, something like $_SERVER['PHP_SELF'] (although
    > this depends on any QUERY_STRING vars I might have in use also for
    myself).
    >
    >
    > Again.. HTH =)
    >
    >
    >
    > Regards,
    >
    > Ian
    >
    > --
    > Ian.H
    > digiServ Network
    > London, UK
    > [url]http://digiserv.net/[/url]
    >
    Hi Steve,

    I would say that Ian's suggestions are the most sensible PHP-way to redirect
    the user back to the original form to correct their mistakes. However, the
    original values they entered will not be in the textboxes (unlike if they
    pressed "back" in their browser).

    PHP is not an ideal way to check forms for errors, as it runs on the server
    side and thus all the information is already sent. If I were you, I would
    use javascript to check the fields are full before sending. This way, it
    can provide instant feedback to the user about whether they have made a
    mistake. Naturally, not all people have javascript enabled, so you would
    also want to implement a PHP-check (as Ian suggests) to cover all grounds.

    For an explanation on how to validate forms using javascript, see the link
    below:

    [url]http://www.yourhtmlsource.com/javascript/formvalidation.html[/url]

    Alternatively, if the form does not contain sensitive information, perhaps
    you could use a PHP page to regenerate the form with the values previously
    entered. E.g. <input value="theoldvalue">.

    Regards,

    Duncan Jones
    [url]http://www.renquish.com[/url]


    Duncan Jones Guest

  3. #3

    Default Re: Rejecting blank fields in forms

    Thanks for the help there, guys - it's got me up and running!

    Steve

    "Ian.H" <ianWINDOZEdigiserv.net> wrote in message
    news:pan.2004.04.05.13.51.38.625000bubbleboy.digi serv.net...
    > On Mon, 05 Apr 2004 14:35:40 +0100, Duncan Jones wrote:
    >
    >
    > [ snip ]
    >
    >
    > > Alternatively, if the form does not contain sensitive information,
    perhaps
    > > you could use a PHP page to regenerate the form with the values
    previously
    > > entered. E.g. <input value="theoldvalue">.
    >
    >
    > This works for sure and normally what'll do (I didn't for this brief
    > example). Especially if the form is posting to iself this becomes a
    > trivial task to do.. just validate, format and print back as you say.
    >
    > The other alternative I've used is SESSIONs. When the form posts, store
    > the info (_NOT!_ passwords etc) in a SESSION var which is then accessihble
    > to do the same echo as you explain above.
    >
    > Your suggestion of both Javascript and PHP validation is also a good one
    > IMO =)
    >
    >
    >
    > Regards,
    >
    > Ian
    >
    > --
    > Ian.H
    > digiServ Network
    > London, UK
    > [url]http://digiserv.net/[/url]
    >

    Steve Guest

  4. #4

    Default Re: Rejecting blank fields in forms

    Hello,

    On 04/05/2004 09:26 AM, Steve wrote:
    > I hope that someone maybe able to help me with this question. At the moment
    > I have the following line of code that checks if a field is blank:
    >
    > if($name == "") {echo "<p><b>PLEASE CLICK 'BACK' ON YOUR BROWSER AND ENTER A
    > VALID NAME</b>";} else {echo "Name: $name";};
    >
    > But is there a command that could just detect a null and go straight back to
    > the form page rather than requesting that the user clicks the back button. I
    > know this is probably a pretty simple problem for some of the pros, but I'm
    > a real newbie to PHP! Of course, a few suggestions on a more elegant way to
    > implement this check would also be very interesting to read!
    You may want to take a look at this forms generation and validation
    class that was meant exactly for what you want:

    [url]http://www.phpclasses.org/formsgeneration[/url]


    --

    Regards,
    Manuel Lemos

    PHP Classes - Free ready to use OOP components written in PHP
    [url]http://www.phpclasses.org/[/url]

    PHP Reviews - Reviews of PHP books and other products
    [url]http://www.phpclasses.org/reviews/[/url]

    Metastorage - Data object relational mapping layer generator
    [url]http://www.meta-language.net/metastorage.html[/url]
    Manuel Lemos Guest

Similar Threads

  1. Help with Access fields when they are to remain blank
    By I Need Help! in forum Dreamweaver AppDev
    Replies: 7
    Last Post: May 3rd, 11:33 PM
  2. Blank fields in database
    By Sue in forum ASP Database
    Replies: 7
    Last Post: August 12th, 07:55 AM
  3. ASP & Mysql - Problem with blank text fields
    By Joel in forum ASP Database
    Replies: 1
    Last Post: August 1st, 03:18 PM
  4. Datasheet Fields Blank
    By Andrew H in forum Microsoft Access
    Replies: 4
    Last Post: July 4th, 01:48 PM
  5. Blank form fields passed to SQL
    By The ScareCrow in forum Macromedia Dynamic HTML
    Replies: 0
    Last Post: June 26th, 03:14 AM

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
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139