A very basic and easy prob to solve



I just registered, these look like good forums, v. handy!

Im a n00bie to PHP..

I have a very simple problem, and I cant think how to solve it.. and my book (PHP and MySQL Web Development) aint helpin too much, So I was wonderin if any of you could help? :wink:

Basically, I have made a simple form and If a User hasnt filled in all the fields, and they hit Submit, I want it to not have the Proceed button in the next one, but only a "go back" button. there is 6 variables, (one doesnt need to be filled in jst to make life hard) Here is the code..

Index.php is very simple, and.. well, its html haha

<FORM ACTION="/matt/form.php">
<INPUT TYPE="hidden" NAME="submitted" VALUE="1">
Name* <INPUT TYPE="text" NAME="variable1"><BR>
Address 1* <INPUT TYPE="text" NAME="variable2"><BR>
Address 2* <INPUT TYPE="text" NAME="variable3"><BR>
Address 3* <INPUT TYPE="text" NAME="variable4"><BR>
Address 4 <INPUT TYPE="text" NAME="variable5"><BR>
Post Code* <INPUT TYPE="text" NAME="variable6"><BR>
<INPUT TYPE="submit" VALUE="submit"><BR><BR>

All items marked * are Required

heres the bit, I need help on.



if ($submitted) {
if ($variable1=="") {
echo "your name was not filled in<BR><BR>"; }
else {
echo "Name = $variable1 <BR><BR>"; }

if ($variable2=="") {
echo "Address 1 was not filled in<BR><BR>"; }
else {
echo "Address 1 = $variable2 <BR><BR>"; }

if ($variable3=="") {
echo "Address 2 was not filled in<BR><BR>"; }
else {
echo "Address 2 = $variable3 <BR><BR>"; }

if ($variable4=="") {
echo "Address 3 was not filled in<BR><BR>"; }
else {
echo "Address 3 = $variable4 <BR><BR>"; }

if ($variable5=="") {
echo "Address 4 = N/A <BR><BR>"; }
else {
echo "Address 4 = $variable5 <BR><BR>"; }

if ($variable6=="") {
echo "Post Code was not filled in<BR><BR>"; }
else {
echo "Post Code = $variable6 <BR><BR>"; }




I guess at the bottom, I need an If( statement, but am not sure on how to do this. Variable5 isn't fully required, as you can see.

I want it to complain when any one of the var1, 2, 3, 4, and 6 are left empty..

Cheers in advance for any help you can give! (sorry its a long post!)

by the way, its not for a commercial site, or anything in case your wondering, lol... just part of some IT work, for school
Your book should be burned because it is showing you the archaic almost deprecated way of doing it.

Most likely you are having problems because register_globlas is turned off on your server.

If you put $_POST['submitted'] around each of the posted variables it should work for you. also I looked at your form tag and it is missing a posting method.

IM me at rc1005 if you continue to have problems.
First of, if you're talking about the book I think you are,

it should be burned, it's a huge waste of time and money. First time in 8 years I've been dissapointed by Sams.

Second of all, I would suggest naming your html form elements something useful, connected to their purpose, rather than variable1 etc. For example, txt_name, txt_add_1, txt_add_2, txt_city, txt_state_prov, txt_post_code. You could also give your form elements the same names as the database fields they'll eventually get saved to.. if done right, this can lead to some pretty slick code.

Lastly, you're building your form in straight html. If you did it in php, you'd have some advantages. If someome partly filled out a form, and you wanted to display some warning text and redraw the form, consider the advanteges of something like this..

echo "<INPUT type = 'text' name = 'txt_name' value = '  ";

if (isset($_POST['txt_name'])
  echo $_POST['txt_name'];

echo "  '>";

Hope this helps a little bit. If you can still return that book, do so, it really chaps my hide what a bad start that book got me off too.
Yeah, im afraid that is the book I have.. Recommended to me, by my ICT Teacher and a friend who runs some sites, and stuff. Doubt I can return it now..

I'll buy another one, which do you recommend? 8)
As a back up and to limit the number of times the page reloads on your site you might want to employ Javascript (I know this is a PHP forum but it's a horses for courses situation).

<script language="JavaScript">


Place the code above in your header and put 'req' in front of every text box that you want to make obligatory (this works only for text boxes). If you name the text box 'reqfirst_name' the '_' will be replaced with a space and the Javascript alert will say 'Please make sure the FIRST NAME field was properly completed.' before the POST method is undertaken.


<FORM name="address" method="post" action="#" onSubmit="return checkrequired(this)">
Address 1* <INPUT TYPE="text" NAME="reqaddress_1"><BR>

You should also double-check it via PHP though, as some people turn Javascript off. However, this method of notifying a user that they are producing an error is quicker and more user friendly.

Hope this helps,

well.. That book was awesome two years ago.. not with the changes to php 4.2 on though