This is a discussion on "how can I ensure that require form fields are filled??" within the PHP Forum section. This forum, and the thread "how can I ensure that require form fields are filled?? are both part of the Program Your Website category.
|
|
|
|
|
![]() |
||
how can I ensure that require form fields are filled??
|
||
| Notices |
![]() |
|
|
LinkBack | Thread Tools |
|
|||
|
hi
how can I ensure that the required fields are filled with valid data? requesting help thanks |
|
|
|
||||
|
Re: how can I ensure that require form fields are filled??
Yes uddin, this needs to go in another forum -- PHP, ASP, maybe even javascript?
|
|
|||
|
Re: how can I ensure that require form fields are filled??
I am using MySQL n PHP
|
|
|||
|
Re: how can I ensure that require form fields are filled??
Well, you can approach this a number of ways. My first question would be do you know javascript and are you validating client side first? If not I would suggest doing that, it's quite common to validate on your users machine before heading over to PHP validation on the server.
That doesn't mean do not validate at the server. It's a real good idea to catch any potential errors before hand. I'm going to assume you have no knowledge of regular expressions. So you can access your form element in javascript using names like this:
So, say you want to make sure the field is filled in you could use:
Find Javascript info: http://www.w3schools.com/js/default.asp Find PHP info : http://www.php.net/ Man, now that I think about it. That may be TMI and I probably didn't explain it very well. Hope I helped instead of making it worse. Good Luck |
|
||||
|
Re: how can I ensure that require form fields are filled??
Okay, first a word of advice about posting a question. It's a lot easier to answer if you post the related code, both html and php.
Let's suppose you have a form asking for a person's first name and you are going to handle the form on a different page called (www.yoursite.xxx/formhandle.php). Your form will look something like this, only you'll need to format it a bit for presentation.
Now on page "formhandle php" there are several ways to ensure that the first name text box has something in it. One basic way is
The second "if" clause answers your question. it requires that there is something in the form input "f_name". The global variable we chose with our method -- which is $_POST[] because we set (method="post"), and specifically will be named $_POST['f_name'] -- must past the test isset(). (If you don't know the specific meaning of isset() just google "php isset" to get to the php manual or a number of other explanations.) You can use other functions, even the simple "if($_POST['f_name'])". If the form has been filled in correctly, you will now have a lovely variable, $firstname, that you can use however you want. You can just pop it into an sql query written in php, like
You didn't ask about security, but it is absolutely essential that you validate the entry in PHP if you have an open MySQL connection on the page, as you obviously will. The most secure way to do this is to use a regular expression. I use PERL regex and the minimum test for a name would be something like
Notice that you must validate in PHP. You can also validate in javascript as a convenience to the user, because it is capable of printing a warning without loading a new page, but it provides almost zero site security (because javascript can be disabled so easily). Regex provides a high level of security, and if I am not mistaken completely closes this avenue of attack for hackers. I'm sorry to be the bringer of bad news, but you really need to understand the basics of some flavor of regular expressions before you expose a php/mysql form to the public. The bad news is that regex is confusing and rather difficult to learn at first. The good news is that you probably only need a few basics and Jan Goyvaerts has a wonderful free tutorial online at http://www.regular-expressions.info/php.html I suspect from your question that you might want a bit more study on handling php forms. If so, my best recommendation would be a book by Larry Ullman called "PHP and MYSQL for Dynamic Websites". Finally, just in case you are curious, I'll cut and paste an error_alert() definition from my files. I would put this in a separate file and include it high on the handler page:
As long as I'm at it, a note about function definitions: There are two parameters in the definition. The first is required and will be the basic error message. There is an optional second parameter created by the syntax "$rules=". In the example, the error message can include a second paragraph of text, an image, whatever. You could put a default message in this parameter by substituting whatever you want for NULL, which would print or echo something automatically unless you set a different second parameter. (I realize this isn't anything special btw, especially the bloated css, but it makes a pretty alert page.) I've spent too much time here and can't proofread this post -- please feel free to point out any error. |
![]() |
| Tags |
| field |
| Thread Tools | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Hiding / Showing form fields based on previous form input | John Alexander Hopper | PHP Forum | 1 | Mar 10th, 2008 11:30 |
| HTML form fields | jasonjkd | Web Page Design | 2 | Oct 15th, 2007 21:58 |
| Form Fields | robukni | PHP Forum | 7 | Oct 10th, 2007 20:26 |
| Form fields hidden under images | dangergeek | Web Page Design | 5 | Sep 3rd, 2007 13:23 |
| how to send filled feedback form | reeta.vadgama | Web Page Design | 5 | Jul 24th, 2006 23:23 |