Start your text editor - you should have SciTE (The Scintilla Text Editor) by now. If not you can find it in Downloads
If not open notepad and type this is into it:
";$endofpage = " |
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 ="
"; $endofpage = " |
?>
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:
(This is the code run from the "If" statement)"; echo $startofpage; echo $form2; echo $endofpage;?>
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:
(This is the code run from the "If" statement)"; echo $startofpage; echo $form2; echo $endofpage;
$form2 = "
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:
(This is the code run from the "else" statement)"; echo $startofpage; echo $firstform; echo $endofpage;}?>
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:
";$endofpage = " |
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