|
• Tutorials: PHP: Making a Form Yes, you may have heard php is simple, it is, if you are just including files, but as you try to improve your php skills further and further, you realize how many functions there are. I will now show you how to create a form, i'll display the code and then analize it by using shell style comments '#'. The below can all go in one file. ----------------------------------------------------------------------- PHP CODE, JUST COPY AND PASTE COMMENTS WILL NOT SHOW ON THE BROWSER ----------------------------------------------------------------------- <?php echo"<form action=$PHP_SELF method=post> <table cellpadding=\"2\" border=\"0\" cellspacing=0><tr><td bgcolor=\"c0c0c0\">"; # Now that the header is out the way, notice how you don't need to have quotes # after the = on most html tags, although I would always recommend it. Also look at # the end of our line, if it has an ';' on the end of it. You always need this after every # statement in php you make, i.e '$i=6-1;' or 'include("news.txt");'. If you are going to # use quotes within other quotes, always put a '\' before the quote, as i have done. # Also note the $PHP_SELF, when echo'ed this displays the filename of page that is currently # being viewed if($submit){ # We tell php that if the submit button has been pressed, $submit in the brackets on its own # just tells the php code to do the stuff between { and } if submit has any value at all. $len = strtok($post," "); while($len) { $length=strlen("$len"); if($length >= 60){echo"You are not allowed to enter a word over 14 characters in length";exit;} $len = strtok(" "); } # We don't want them messing up our tables now, so we make sure they dont enter a word over # 60 chracters in $post, other wise stop the rest of the script for executing. if(strlen("$post") < 10){ echo"Your comments were too short, please go back and try again"; # strlen() is a useful command, it gets how many characters there are in a string, now we # don't want them making a pathetic comment now do we? So we say, if the length of the string # $post is less than 10 to go and say that their comments were to short. }elseif(!$name){ echo"You did not enter a name, please go back and try again"; # If the length of the the post is ok we then want to see if they have entered a name # Not the use of (!$name), this works exactly the same as ($name == ""). }else{ nl2br($post); # We Want to make sure any new lines they entered are displayed as a newline, that code # above should be perfect :) $name=strip_tags("$name"); $post=strip_tags("$post", "<a></a><b></b><u></u><i></i>"); if($email){strip_tags("$email");} # We are now saying if they have submitted, if the post is long enough and if they have # entered a name to strip all the html from the fields, except for post where we allow # links, bold, underline and italics. Notice an if() statement doesnt need to be followed # up by an else or else if statement. The if() is just telling php that if the email field # has been filled in, to strip the html from the email field. echo"Your Name</td><td>$name</td></tr>"; if($email){echo"<tr><td bgcolor=c0c0c0>Your Email</td><td>$email</td></tr>";} echo"<tr><td bgcolor=c0c0c0>Your comments</td><td>$post</td></tr>"; # We are now displaying the information they just entered } # We have now closed the technical stuff }else{ # Remember when we did the if() statement all the way up the top? well we are now gonna # bring it back using else! if the submit button isnt pressed, then the form is displayed echo"Your Name</td><td><input type=text name=\"name\"></td></tr> <tr><td bgcolor=c0c0c0>Your Email</td><td><input type=text name=\"email\"></td></tr> <tr><td bgcolor=c0c0c0 valign=top>Your comments</td><td><textarea name=\"post\" cols=30 rows=5></textarea></td></tr> <tr><td bgcolor=cccccc colspan=2 align=center><input type=submit name=\"submit\" value=submit></td></tr>"; # I take it you know a little about forms so ill leave it, except you must name the submit # button \"submit\" } # Closes the form echo"</table>"; # Shows the footer php?> ----------------------------------------------------------------------- END OF PHP CODE ----------------------------------------------------------------------- I hope i explained it well, if you have any questions email Deano. |