fattwam Posted March 28, 2008 Report Posted March 28, 2008 Now I want to use these pieces we have discussed to make a couple of interactive scripts. We are going to make a VERY simple one person chat script. I know what your thinking, What good is that!? But this is just to give you some examples of using strings with Loops (Control Structures) and POST. (Later on we will be making a REAL chat script) Start your text editor - you should have SciTE (The Scintilla Text Editor) by now. If not you can find it in DownloadsIf not open notepad and type this is into it: /* First we need to define the page's layout and store it in a string so that we can keep our code neat and pretty. */ $startofpage = "</pre><table border="1">"; $endofpage = "</table>";<br><br>// Now that we have a basic layout we can move onto writing the code. <br><br><br><br><br><br>/* Second we need to make a statement that checks to see if<br> this is the first time the person has come to this page - <br> or in this case has "Posted" anything to this page. This<br> will evaluate to FALSE the first time the page is run <br> because there will be nothing "Posted" to the page. However,<br> after something has BEEN posted from then on this will be TRUE.<br>*/<br><br><br>if (isset($_POST['text'])) {<br> $newtext = $_POST['text'];<br> $oldtext = $_POST['oldtext'];<br> <br> $form2 = "<form action="" method="post"> $oldtext $newtext </form>(This is the code run from the "If" statement)";<br> <br> echo $startofpage;<br> echo $form2;<br> echo $endofpage;<br> <br> <br> <br> <br> <br><br> <br>/* <br> Last, if nothing has been posted to the page, (In other words this is the<br> first visit) this "else" will be TRUE and the following code will run. <br> However, after someone posts something it will be FALSE and the code <br> ABOVE will run.<br>*/<br><br> <br>} else { //nothing was posted<br><br> $firstform = "<form action="post.php" method="post"> What is your name? </form> (This is the code run from the "else" statement)";<br> <br> echo $startofpage;<br> echo $firstform;<br> echo $endofpage;<br>}<br><br><br><br Ok, now save it as "post.php" (or whatever you want) and upload it to your server and run it. You will see a square border around a large box, a small box, and a "submit" button. When you type something into the second box and press "submit" what you entered will be posted to the first box along with what was already there. After posting a couple of things and giving yourself a pat on the back for a job well done, come back and lets go over the code. First lets look at the starting strings: $startofpage =" </pre><table border="1">"; $endofpage = "</table>";<br If you know HTML, (Which you should, if not read the chapters on HTML Before any more PHP lessons) this is just a simple layout. We make a table with one column and one row and center it in the page. Next we make the first and ONLY Control Structure - an "if" loop: if (isset($_POST['text'])) { $newtext = $_POST['text']; $oldtext = $_POST['oldtext']; $form2 = "</pre><form action="" method="post"> $oldtext $newtext </form>(This is the code run from the "If" statement)";<br> <br> echo $startofpage;<br> echo $form2;<br> echo $endofpage;<br><br This first loop checks to see if something was posted to that page: if (isset($_POST['text'])) [/code] In human language that would be: [code] if a value in the variable "text" has been set/made If something was posted/set/made/whatever we pull it out an put it in a variable: $newtext = $_POST['text']; $oldtext = $_POST['oldtext']; (note: we also get the value from "oldtext") Then we take those values and put them in a form and show it to the user again: $form2 = "</pre><form action="" method="post"> $oldtext $newtext </form>(This is the code run from the "If" statement)";<br> <br> echo $startofpage;<br> echo $form2;<br> echo $endofpag That would end the code that would run. However, if nothing was posted to the page, i.e. this is the first visit, the "if" would be FALSE and PHP would move on to the "else" statement: } else { //nothing was posted $firstform = "</pre><form action="post.php" method="post"> What is your name? </form> (This is the code run from the "else" statement)";<br> <br> echo $startofpage;<br> echo $firstform;<br> echo $endofpage;<br>}<br><br First we would make a form and put the starting text in "oldtext". Then we would make a field called "text" where the user would answer. So now we have TWO values in this form "oldtext" and "text"(note: this is the same as the other form) Then all we need to do is print the form to the user and we are done: echo $startofpage; echo $firstform; echo $endofpage; Note: I put code below each form so that when you are experimenting with this script you will know which loop has run - "if" or "else". Note: If you leave the page and come back, what you have entered will be lost because this script is not saving what you post anywhere. It is just passing the value and the old value back to the page every time it is run. So the COMPLETE CODE is this: $startofpage = "</pre><table border="1">"; $endofpage = "</table>";<br><br>if (isset($_POST['text'])) {<br> $newtext = $_POST['text'];<br> $oldtext = $_POST['oldtext'];<br> <br> $form2 = "<form action="" method="post"> $oldtext $newtext </form>(This is the code run from the "If" statement)";<br> <br> echo $startofpage;<br> echo $form2;<br> echo $endofpage;<br><br> <br>} else { //nothing was posted<br><br> $firstform = "<form action="post.php" method="post"> What is your name? </form> (This is the code run from the "else" statement)";<br> <br> echo $startofpage;<br> echo $firstform;<br> echo $endofpage;<br>}<br Note: You can download this file below. Now that we have a functional script lets do something?s to it! First up, did you notice that every time you post something that the "What is your Name?" text moves over more? This is because there is space between the and the "what is your name?" text. and every time you repost the "oldtext" variable you take that space and re-add it to it. If you aren't following me sorry, I'm not that good at explaining... Anyway, lets fix it! This is a handy function called "trim" which strips whitespace (or other characters) from the beginning and end of a string. so lets add this to the script: if (isset($_POST['text'])) { $newtext = $_POST['text']; $oldtext = $_POST['oldtext']; $newtext = trim($newtext); //Added! $oldtext = trim($oldtext); //Added! $form2 = "</pre><form action="" method="post"> $oldtext $newtext </form>(This is the code run from the "If" statement)";<br> <br> echo $startofpage;<br> echo $form2;<br> echo $endofpag This will keep the code clean! Now lets suppose that someone is really bored (and boring) and rather than doing something for this world, they waste their time by trying to take down web sites for no reason. So with this form someone could post some kind of malicious code that might redirect to another site (I am sure you can figure out where) or mess-up our layout or something. So now we are going to add another string function to our code that searches the string for any dangerous PHP or HTML characters and removes them. if (isset($_POST['text'])) { $newtext = $_POST['text']; $oldtext = $_POST['oldtext']; $newtext = strip_tags(trim($newtext)); //Added Again! $oldtext = strip_tags(trim($oldtext)); //Added Again! $form2 = "</pre><form action="" method="post"> $oldtext $newtext </form>(This is the code run from the "If" statement)";<br> <br> echo $startofpage;<br> echo $form2;<br> echo $endofpag Now we trim the code down to the text by removing extra spaces and such with "trim" and then remove any PHP or HTML code that might mess up the page with "strip_tags". What is left is then put in to the variable. Extra Addons to the code Now lets add some code that prints how many words were posted back to the user. For this we are going to use the "str_word_count" function. We can also add "strlen" to print out how many characters are in the posted comment. if (isset($_POST['text'])) { $newtext = $_POST['text']; $oldtext = $_POST['oldtext']; $newtext = strip_tags(trim($newtext)); $oldtext = strip_tags(trim($oldtext)); $form2 = "</pre><form action="" method="post"> $oldtext $newtext </form>(This is the code run from the "If" statement)";<br> <br> echo $startofpage;<br> echo $form2;<br>// Just Added<br> echo "You Posted ";<br> echo str_word_count($newtext);<br> echo " words and ";<br> echo strlen($newtext);<br> echo "Characters.";<br>// Just Added <br> echo $endofpag Here we passed the variable "$newtext" that holds what the user posted to the function str_word_count which then returned the number of word in the variable. Now we are going to add a function called "ord" that echo?s the first characters ASCII value. This only serves as an example of more things you can to your strings. if (isset($_POST['text'])) { $newtext = $_POST['text']; $oldtext = $_POST['oldtext']; $newtext = strip_tags(trim($newtext)); $oldtext = strip_tags(trim($oldtext)); $form2 = "</pre><form action="" method="post"> $oldtext $newtext </form>(This is the code run from the "If" statement)";<br> <br> echo $startofpage;<br> echo $form2;<br><br> echo ""; //Added to center the text<br><br> echo "You Posted ";<br> echo str_word_count($newtext);<br> echo " words and ";<br> echo strlen($newtext);<br> echo "Characters."; <br> <br> echo "Here is the ASCII Value for the first character you posted: "; //Added<br> echo ord($newtext); //Added<br> <br> echo ""; //Added to center the text<br> echo $endofpag This Fun addon will print the users post backwards! echo $startofpage; echo $form2; echo ""; echo "You Posted "; echo str_word_count($newtext); echo " words and "; echo strlen($newtext); echo "Characters."; echo "Here is the ASCII Value for the first character you posted: "; echo ord($newtext); echo "Here is your text Backwards: "; //Added echo strrev($newtext); //Added echo ""; echo $endofpage; You can also build a bad-word filter to keep your posts clean! For that we can use the function "str_replace" which works like this: $bodytag = str_replace("%body%", "black", ""); // Provides: ?> if (isset($_POST['text'])) { $newtext = strip_tags(trim($_POST['text'])); $oldtext = strip_tags(trim($_POST['oldtext'])); $badwords = array("sucks", "damn", "#!@?", "ASP"); //Added $newtext = str_replace($badwords, "****", "$newtext"); //Added You can add more words to the filter, just put a "," after each one. Ok, well that is it for this script, but don't worry we will make a real chat script soon enough. However this was good practice using strings and posts. If you want to do more with this script you can visit PHP.net - Strings for more functions and examples of how to use them. You can also download the complete "post.php" file below. I incourage you to try to add more things to this script maybe something that makes all of the text entered LOWER CASE.You can download the file here.
Recommended Posts
Archived
This topic is now archived and is closed to further replies.