A very basic and easy prob to solve

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Snellgrove2
New php-forum User
New php-forum User
Posts: 5
Joined: Wed Feb 19, 2003 8:16 am
Location: South England

A very basic and easy prob to solve

Postby Snellgrove2 » Wed Feb 19, 2003 8:25 am

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

<HTML><HEAD><TITLE>Custom Guitars</TITLE><BODY><BR><BR>
<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
</BODY></HTML>

heres the bit, I need help on.

<HTML><HEAD></HEAD><BODY><BR><BR>

<?

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>"; }

}

?>

</BODY></HTML>

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

User avatar
Redcircle
Moderator
Moderator
Posts: 830
Joined: Tue Jan 21, 2003 10:42 pm
Location: Michigan USA
Contact:

Postby Redcircle » Wed Feb 19, 2003 11:50 am

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.

Hurkle
New php-forum User
New php-forum User
Posts: 36
Joined: Tue Jan 07, 2003 8:45 am
Location: Minnesota, USA

Postby Hurkle » Wed Feb 19, 2003 1:24 pm

First of, if you're talking about the book I think you are,
Image
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..

Code: Select all

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.

Snellgrove2
New php-forum User
New php-forum User
Posts: 5
Joined: Wed Feb 19, 2003 8:16 am
Location: South England

Postby Snellgrove2 » Thu Feb 20, 2003 8:19 am

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)

Hadleigh
New php-forum User
New php-forum User
Posts: 32
Joined: Tue Feb 04, 2003 10:32 am
Location: UK
Contact:

Postby Hadleigh » Thu Feb 20, 2003 8:31 am

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).

Code: Select all

<script language="JavaScript">
<!--
function checkrequired(which) {
var pass=true;
if (document.images) {
for (i=0;i<which.length;i++) {
var tempobj=which.elements[i];
if (tempobj.name.substring(0,3)=="req") {
if (((tempobj.type=="text"||tempobj.type=="textarea")&&
tempobj.value=='')||(tempobj.type.toString().charAt(0)=="s"&&
tempobj.selectedIndex==0)) {
pass=false;
break;
         }
      }
   }
}
if (!pass) {
temp=tempobj.name.substring(3,1000).toUpperCase();
out = "_"; // replace this
add = " "; // with this

while (temp.indexOf(out)>-1) {
pos= temp.indexOf(out);
temp = "" + (temp.substring(0, pos) + add +
temp.substring((pos + out.length), temp.length));
}

alert("Please make sure the "+temp+" field was properly completed.");
return false;
}
else
return true;
}
//-->
</script>


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.

e.g.

Code: Select all

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


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,

Hadleigh.

User avatar
Redcircle
Moderator
Moderator
Posts: 830
Joined: Tue Jan 21, 2003 10:42 pm
Location: Michigan USA
Contact:

Postby Redcircle » Thu Feb 20, 2003 12:58 pm

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


Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 1 guest