Required Fields on Forms - Almost There I Think...

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
zunebuggy
Forum Commoner
Posts: 41
Joined: Wed Aug 27, 2008 1:22 pm

Required Fields on Forms - Almost There I Think...

Post by zunebuggy »

I am new to PHP.

I have a simple HTML form. When a user clicks Submit it will do the following: <FORM METHOD="POST" ACTION="whatever.php">

I want my whatever.php script to check the fields from the form (they are ALL required) and if any are NULL, I would like it to return back to the HTML form and display a message on the form telling them what fields need to be taken care of.

Right now I have a form at http://www.mydomain.com/register.html (for example). I have heard you can have a PHP script that actually creates the form dynamically and can display error messages. I can sort of picture how to do this, but my register page is a link (/register.html) off from my home page. It does not seem logical or correct to me to have a link to register.php that generates a register.html. But I might be wrong...

So I really have two questions here:
1). Should I create my form dynamically with PHP?
2). Can someone give an example of the correct way to redirect or re-create the form page based on a missing field? I think I have the beginnings here, correct?

Code: Select all

 
if ($username == "")
{ //redirect back to register.html or create new form dynamically
}
 
if ($password == "")
{ //...
}
 
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Required Fields on Forms - Almost There I Think...

Post by JAB Creations »

1.) I highly recommend reading this page for a minimal of thirty seconds...
index.php

...once you do you'll know why many of the regulars here get annoyed at code posts in the General Discussion forum. :|

2.) Easy as cake question which I'm glad I'm able to be the one helping someone else out.

XHTML

Code: Select all

<form action="" method="get">
<input name="example_get" value="juice" />
</form>
PHP

Code: Select all

<?php
echo $_GET['example_get']; //will echo juice
?>
XHTML

Code: Select all

<form action="" method="post">
<input name="example_post" value="fruit" />
</form>
PHP

Code: Select all

<?php
echo $_POST['example_post']; //will echo fruit
?>
As far as I am concerned working examples trump five useless pages of explanations. :)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Required Fields on Forms - Almost There I Think...

Post by Christopher »

Vivid examples, but the OP was about checking fields an showing error messages.
zunebuggy wrote:So I really have two questions here:
1). Should I create my form dynamically with PHP?
You don't need to create the HTML dynamically. But yes, you should use PHP.
zunebuggy wrote:2). Can someone give an example of the correct way to redirect or re-create the form page based on a missing field? I think I have the beginnings here, correct?
Instead use http://www.mydomain.com/register.php and have it submit the form to itself. Check to see if the values are set in $_POST and not empty strings. Redisplay the form with error messages if there are errors. Process the data and then redirect if all fields are ok.
(#10850)
Post Reply