Get support from Xavier Media
It is currently Sun Dec 08, 2013 3:44 pm

All times are UTC




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Lesson 6 - Simple Script
PostPosted: Thu Jul 13, 2006 7:06 pm 

Points:
Simple $_GET and $_POST script.


Now it is time to put what we learned about $_GET and $_POST to use. We are going to make a script that prints what you enter into a box onto the screen.

Make a new text file and save it as "post.php" with (use notepad or some similar program - NOT MS Word). Now type the following into it:


[php]

<form action="post.php" method="post"> <!--//Make the HTML Form -->
Your Text:<input name="text" type="text"> <!--//Make the input box -->
<input type="submit" value="Submit"> <!--//Make the Submit button -->
</form> <!--// End the Form -->

<?php
if (isset($_POST['text'])) {
/* "if" something was posted (isset) do the following: */
print "You Said: ". $_POST['text'];
}
?>

[/php]



If you re-save it now and upload it to your web server you will see the something like the following:

Code:
<form>
Your Text:<input name="text" type="text">
<input type="submit" value="Submit">
</form>


When you enter something into YOUR scripts text box and click "Submit" you will then see it printed out on to the screen below the form. If you right-click onto the screen you will see that only the processed "source code" remains:

Code:
<form action="post.php" method="post">          <!--//Make the HTML Form -->
Your Text:<input name="text" type="text">       <!--//Make the input box -->
<input type="submit" value="Submit">        <!--//Make the Submit button -->
</form>                             <!--// End the Form -->
You Said: Hello Everybody!


Now what does each part of this code do? The first part is just a simple HTML Form:

Code:
<form action="post.php" method="post">          <!--//Make the HTML Form -->
Your Text:<input name="text" type="text">       <!--//Make the input box -->
<input type="submit" value="Submit">        <!--//Make the Submit button -->
</form>                                         <!--// End the Form -->


There is NO PHP in it - all it does is make the text area and the submit button for the script. Notice the start of the form says "<form action="post.php" method="post">". This is telling your browser to "POST" the value in the form to the web page "post.php". In other words, whatever is entered into the form should be sent to the post.php page by way of the "$_POST" superglobal.

The next six lines are the PHP code that checks to see if anything was posted:


[php]
<?php
if (isset($_POST['text'])) {
/* "if" something was posted (isset($_POST['text'])) do the following: */
print "You Said: ". $_POST['text'];
}
?>
[/php]


If something is in the "$_POST" superglobal it will "print" it out.


The most important part of this script was the "if". "If" checks to see if the expression is TRUE and if it is - the "if" will run the code below it. In other words - "If" an expression evaluates to TRUE, PHP will execute the following statement, and if it evaluates to FALSE - it'll ignore it.


The following example would display "a is bigger than b" if the variable $a is bigger than $b:

[php]

<?php
if ($a > $b) //Greater than ">"
print "a is bigger than b";
?>

[/php]




About "if":

"Any PHP script is built out of a series of statements. A statement can be an assignment, a function call, a loop, a conditional statement of even a statement that does nothing (an empty statement). Statements usually end with a semicolon. In addition, statements can be grouped into a statement-group by encapsulating a group of statements with curly braces. A statement-group is a statement by itself as well.

The if construct is one of the most important features of many languages, PHP included. It allows for conditional execution of code fragments. PHP features an if structure that is similar to that of C:" -PHP.net

[php]
if (expression)
then do statement
[/php]



For more information please read these articles:

Using If Else Statements
Operators
Looping The Loop

If you find a broken link please PM me, thanks!


Report this post
Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC


Who is online

Registered users: No registered users


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron

Portal » Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
[
SEO MOD © 2007 StarTrekGuide ]