/    Sign up×
Community /Pin to ProfileBookmark

Error checking

Ok im not sure why im drawing such a blank on this but i cant figure it out to save my life.
Basicly I have a form. When users submit the form i want it to check for any blanks and either decline and tell them by a red star next to the field they forgot to imput something.
Any help??

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogNov 22.2006 — It's probably easiest to do this by having the form and the form-processing on the same page (therefore the form action points to its own page). Then you can start that page by checking to see if the form has been submitted, typically by checking to see if some $_POST or $_GET value is set (e.g.: [b]if(isset($_POST['fieldname']))[/b]). Within that conditional processing block, you first do your form validation. If everything is OK, then you process the request, output the results (or redirect to a "success" page), and exit. If any errors were detected, you then let it fall out of your conditional block and let the form display. Within the form, you can have conditional statements checking for error conditions you may have trapped in the form validation, and output error messages or other error indicators as desired.
Copy linkTweet thisAlerts:
@so_is_thisNov 22.2006 — ... you can start that page by checking to see if the form has been submitted, typically by checking to see if some $_POST or $_GET value is set (e.g.: [b]if(isset($_POST['fieldname']))[/b]).[/QUOTE]

Seems to me it is better to do the following for checking to see if the form has been submitted:

[code=php]
if ($_SERVER['REQUEST_METHOD'] == 'POST') # or 'GET'[/code]

Of course, by the same token, it is best if the FORM uses METHOD="POST" so that its submission cannot be confused with normal navigation (which is also a REQUEST_METHOD of GET).
Copy linkTweet thisAlerts:
@NogDogNov 22.2006 — Generally, the choice of POST or GET method should be based on whether the request is to do something that is "idempotent" or not:
The "get" method should be used when the form is idempotent (i.e., causes no side-effects). Many database searches have no visible side-effects and make ideal applications for the "get" method.

If the service associated with the processing of a form causes side effects (for example, if the form modifies a database or subscription to a service), the "post" method should be used.

[i]Note. The "get" method restricts form data set values to ASCII characters. Only the "post" method (with enctype="multipart/form-data") is specified to cover the entire [ISO10646] character set.[/i] [Which might therefore be a reason to use POST for certain idempotent requests -- NogDog]
[/quote]

(From the [url=http://www.w3.org/TR/html4/interact/forms.html#submit-format]w3.org HTML 4.01 specification[/url].)
Copy linkTweet thisAlerts:
@prophitauthorNov 22.2006 — I have it with post now. Here is my script...
<i>
</i>&lt;form method="post" action="copy.php"&gt;
&lt;table border="1" id="hardware"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;b&gt;Reason for Replacement:&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;&lt;/b&gt;&lt;input type="text" name="rfr" size="60"&gt;&lt;br&gt;&lt;/td&gt;
&lt;tr&gt;&lt;td&gt;&lt;b&gt;Make :&lt;/b&gt;&lt;/td&gt;&lt;td&gt;
&lt;select name="make"&gt;
&lt;option value=""&gt;Please Choose
&lt;option value="zyxel"&gt;Zyxel
&lt;option value="nomadix"&gt;Nomadix
&lt;option value="proxim"&gt;Proxim
&lt;option value="ascendance"&gt;Ascendance
&lt;/SELECT&gt;
&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
&lt;td&gt;
&lt;b&gt;Model :&lt;/b&gt;&lt;/td&gt;&lt;td&gt;
&lt;select name="model"&gt;
&lt;option value=""&gt;Please Choose
&lt;option value="vsg"&gt;VSG1200
&lt;option value="ag"&gt;AG3000
&lt;option value="hsg"&gt;HSG
&lt;option value="es2024"&gt;ES2024
&lt;option value="es3024"&gt;ES3024
&lt;option value="es3124"&gt;ES3124
&lt;option value="es2008"&gt;ES2008
&lt;option value="b3000"&gt;B3000
&lt;option value="g1000"&gt;G1000
&lt;option value="g3000"&gt;G3000H
&lt;option value="ap700"&gt;AP700
&lt;option value="a350"&gt;A350
&lt;option value="ves1012"&gt;VES-1012
&lt;option value="b120"&gt;Zyair B120
&lt;option value="b122"&gt;Zyair B122
&lt;option value="g405"&gt;Zyair G405
&lt;option value="poe10"&gt;Zyxel POE10
&lt;option value="poe80"&gt;Zyxel POE80
&lt;/SELECT&gt;
&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;b&gt;WDS:&lt;/b&gt;&lt;/td&gt;&lt;td&gt;
&lt;select name="wds"&gt;
&lt;option value=""&gt;Please Choose
&lt;option value="yes"&gt;Yes
&lt;option value="no"&gt;No
&lt;/select&gt;&lt;/td&gt;&lt;/tr&gt;

&lt;tr&gt;&lt;td&gt;&lt;b&gt;Serial Number:&lt;/b&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="text" name="sn" size="12"&gt;&lt;/tr&gt;&lt;/td&gt;
&lt;tr&gt;&lt;td&gt;&lt;b&gt;Mac Address:&lt;/b&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="text" name="mac" size="14"&gt;&lt;/tr&gt;&lt;/td&gt;
&lt;tr&gt;&lt;td&gt;&lt;b&gt;IP Address:&lt;/b&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="text" name="ipaddr" size="15"&gt;&lt;/tr&gt;&lt;/td&gt;
&lt;tr&gt;&lt;td&gt;&lt;b&gt;Property ID:&lt;/b&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="text" name="prop" size="7"&gt;&lt;/tr&gt;&lt;/td&gt;
&lt;tr&gt;&lt;td&gt;&lt;b&gt;Property Contact Name/Title:&lt;/b&gt; &lt;/td&gt;&lt;td&gt;&lt;input type="text" name="contact" size="20"&gt;&lt;/tr&gt;&lt;/td&gt;
&lt;tr&gt;&lt;td&gt;&lt;b&gt;Shipping:&lt;/td&gt;&lt;td&gt;&lt;/b&gt;
&lt;select name="ship"&gt;
&lt;option value=""&gt;Please Choose
&lt;option value="ground"&gt;Ground
&lt;option value="next"&gt;Next Day Air
&lt;option value="3day"&gt;3 Day
&lt;option value="nextsat"&gt;Next Saturday
&lt;/SELECT&gt;&lt;/tr&gt;&lt;/td&gt;
&lt;tr&gt;&lt;td&gt;&lt;b&gt;Ticket#:&lt;/b&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="text" name="ticket" size="7"&gt;&lt;/tr&gt;&lt;/td&gt;
&lt;tr&gt;&lt;td&gt;&lt;b&gt;Comments:&lt;/b&gt;&lt;/td&gt;&lt;td&gt;
&lt;textarea name="status" rows="4" cols="40"&gt;&lt;/textarea&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;input type="submit" name="value" value="Submit Request"&gt;
&lt;/form&gt;

I have it going to another page cause i need them to copy the data but it doesnt need to.
Copy linkTweet thisAlerts:
@NogDogNov 23.2006 — If you want or need to keep the form and form-handler on different pages and you are using sessions (or can add sessions to it), you can use session variables to store error information. The form-handler can write errors to the $_SESSION array then use header() to redirect back to the form page. The form page can check for errors values in $_SESSION, and if found output them on the page (and then unset them so that they don't persist).
×

Success!

Help @prophit spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...