بحـث
المواضيع الأخيرة
programming with php البرمجة بلفة php
صفحة 1 من اصل 1
programming with php البرمجة بلفة php
Programming with php the powerfull website language
Introduction
Introduction
PHP is a language that was designed to be easily embedded into HTML pages. Most PHP pages have PHP code and HTML intermixed. When a Web server reads a PHP page, it is looking for two things to let it know it should start reading the page as PHP rather than HTML, the start and end PHP tags: <?php and ?>, respectively.
Get Started:
<h1><? echo “First PHP Page.”; ?>!</h1>
The code above, when viewed via a Web server, simply prints out String enclosed in the quotes.
In general, individual lines of PHP code should end with a semicolon, although it is not necessary to use semicolons if a beginning or an ending bracket is used (this will make sense when you look at if/then statements).
For example:
<?
echo "<p>a line of code";
echo "<p>another line of code;
?>
Variables in PHP are denoted by the "$". To assign the value "Hello World" to the variable $str, you simply call it in your code:
$str = "Hello World";
Strings must be enclosed by quotes, but they can contain variables themselves:
$a = 12;
$string = "The value of a is $a";
// $string = "The Value of a is 12";
Variables in PHP are denoted by the "$". To assign the value "Hello World" to the variable $str, you simply call it in your code:
$str = "Hello World";
Strings must be enclosed by quotes, but they can contain variables themselves:
$a = 12;
$string = "The value of a is $a";
// $string = "The Value of a is 12";
PHP variables do not have to be declared ahead of time, nor do they require a type definition. Note that you may get a warning about using undeclared variables if you try to use them before giving them a value (depending on how you set up error reporting in php.ini). For example:
$a = 4;
$c = $a + $b;
// $c = 4, but a warning appears "Warning: Undefined variable..".
Warnings do not stop a script from continuing. If you forgot to add a semicolon at the end of one of the lines, then you would get a Parser error, which prohibits the script from running.
Since PHP variables are not typed, you don't have to worry about performing mathematical equations on the wrong type, as you might in C. For example:
$a = 4;
$b = "5";
$c = $a + $b;
// $c = 9;
PHP also supports boolean variables, which can be assigned either a one or a zero, or the words true or false:
$a = true;
$b = 1;
//$a = $b
$c = false;
$d = 0;
//$c = $d
Variable Naming Rules
A variable name must start with a letter or an underscore.
A variable name can only contain alpha-numeric characters and underscores (a-Z, 0-9, and _ )
Variables
A variable name should not contain spaces. If a variable name is more than one word, it should be separated with underscore ($my_string), or with capitalization ($myString)
PHP variables do not have to be declared ahead of time, nor do they require a type definition. Note that you may get a warning about using undeclared variables if you try to use them before giving them a value (depending on how you set up error reporting in php.ini). For example:
$a = 4;
$c = $a + $b;
// $c = 4, but a warning appears "Warning: Undefined variable..".
Warnings do not stop a script from continuing. If you forgot to add a semicolon at the end of one of the lines, then you would get a Parser error, which prohibits the script from running.
Since PHP variables are not typed, you don't have to worry about performing mathematical equations on the wrong type, as you might in C. For example:
$a = 4;
$b = "5";
$c = $a + $b;
// $c = 9;
PHP also supports boolean variables, which can be assigned either a one or a zero, or the words true or false:
$a = true;
$b = 1;
//$a = $b
$c = false;
$d = 0;
//$c = $d
Variable Naming Rules
A variable name must start with a letter or an underscore.
A variable name can only contain alpha-numeric characters and underscores (a-Z, 0-9, and _ )
A variable name should not contain spaces. If a variable name is more than one word, it should be separated with underscore ($my_string), or with capitalization ($myString)
php Strings
String variables are used to store and manipulate a piece of text.
<?php
$msg="Hello World";
echo $msg;
?>
The output will be
Hello World
The Concatenation Operator
Dot (.) is used as concatenation operator. To concatenate two variables together, use the dot (.) operator:
<?php
$str1="Hello World";
$str2="Adam";
echo $ str1. " " . $ str2;
?>
Hello World Adam
Length of a String:
The strlen() function is used to find the length of a string.
<?php
$string=”Hello World”;
echo strlen($string);
?>
Output will be
11
The strpos() function:
The strpos() function is used to search for a string or character within another string.
If the string is found in the string, this function will return the position of the first match. If no match is found, it will return FALSE.
<?php
echo strpos("Example of strops function","of");
?>
The output will be
8
Operators
Operators are used to manipulate or perform operations on variables and values. There are many operators used in PHP, we have separated them into the following categories.
Assignment Operators
Arithmetic Operators
Comparison Operators
Logical Operators
Assignment Operators:
Assignment operators are used to set a variable equal to a value or set a variable to another variable's value. ‘=’ is used for assignment operator.
Example:
$var = 23;
$var2 = $var;
Operator Example Is The Same As
= a=b a=b
+= a+=b a=a+b
-= a-=b a=a-b
*= a*=b a=a*b
/= a/=b a=a/b
.= a.=b a=a.b
%= a%=b a=a%b
Arithmetic Operators:
Operator Function
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus (division remainder)
++ Increment
-- Decrement
Comparison Operators:
Operator Function Example Result
== Equal To $x == $y false
!= Not Equal To $x != $y true
<
Less Than $x < $y true
> Greater Than $x > $y false
<= Less Than or Equal To $x <= $y true
>= Greater Than or Equal To $x >= $y false
Logical Operators:
Operator Function Example
&& and x=8 y=4
(x < 10 && y > 1) returns true
|| or x=12
y=8
(x>=5 || y<=5) returns true
! not x=6
y=3
!(x==y) returns true
Loops
Control structures if/else statment
One of the most common PHP language constructs that you will encounter is the if…then statement. The if…then statement allows you to evaluate an expression and then, depending if it is true or not, take a course of action. For example:
$x = 1;
if($x) {
echo "True!";
}
Since $x = 1, then PHP interprets it in the context of the if statement to be a boolean type. Since 1 = true, the if statements prints out the message. Literally you can read it as "If True, then print out "True!".
Another example, but this time with an "else" clause:
$a = 5;
$b = "10";
if($a > $b) {
echo "$a is greater than $b";
} else {
echo "$a is not greater than $b";
}
PHP doesn't care that $a is an integer and $b is a string. It recognizes that $b also makes a pretty good integer as well. It then evaluates the expression, "If $a is greater than $b then print out that $a is greater than $b; if not (else) then print out $a is not greater than $b." It's also important to note that you don't say, "$a is less than $b," since it is possible that $a could be equal to $b. If that is the case, then the second part of the expression, the else statement, is the part that runs.
Note the use of brackets to enclose the actions. Also note the lack of semicolons where the brackets are used.
One final example is the if…elseif…else statement:
if($a == $b) {
// do something
} elseif ($a > $b) {
// do something else
} elseif($a < $b) {
// do yet something else
} else {
// if nothing else we do this...
}
Switch Statement:
The Switch statement is used to perform one of several different actions based on one of several different conditions. Switch statements allow for additional evaluations of the data, even though one of the cases may have been met. Switch statements can also save you from typing many if…elseif…elseif… statements.
Syntax:
switch (expression)
{
case label1:
Statement(s);
break;
case label2:
Statement(s);
break;
default:
Statement(s);
}
Example:
$a = "100";
switch($a) {
case(10):
echo "The value is 10";
break;
case (100):
echo "The value is 100<br>";
case (1000):
echo "The value is 1000";
break;
default:
echo "<p>Wrong Input";
}
switch statements have four basic parts:
The switch— The switch identifies what value or expression is going to be evaluated in each of the cases. In the example above, you tell the switch statement that you are going to evaluate the variable $a in the subsequent cases.
The case— The case statement evaluates the variable you passed from the switch command. You can use case() the same way you'd use an if statement. If the case holds true, then everything after the case statement is executed, until the parser encounters a break command, at which point execution stops and the switch is exited.
Breakpoints— Defined by the break command, exit the parser from the switch. Breakpoints are normally put after statements executed when a case() is met.
Default— The default is a special kind of case. It is executed if none of the other case statements in the switch have been executed.
PHp Loops
Loops are used to repeat statement or block of statement more than one time. The idea of a loop is to do something over and over again until the task has been completed.
In PHP we have the following looping statements:
For Loop:
For loops are useful constructions to loop through a finite set of data, such as data in an array.
Syntax:
for ( initialization; conditional statement; increment/decrement){
statement(s);
}
Initialization—You can pass in an already assigned variable or assign a value to the variable in the for statement.
The condition required to continue the for loop— The statements are executed until the given condition goes false.
Statement to modify the counter on each pass of the loop.
Example:
<?php
for ($i=1; $i<=10; $i++)
{
echo "Hello World<br />";
}
?>
Foreach Loop:
Foreach loops allow you to quickly traverse through an array.
$array = array("name" => "Jet", "occupation" => "Bounty Hunter" );
foreach ($array as $val) {
echo "<P>$val";
}
Prints out:
<P>Jet
<P>Bounty Hunter
You can also use foreach loops to get the key of the values in the array:
foreach ($array as $key => $val) {
echo "<P>$key : $val";
}
Prints out:
<P>name : Jet
<P>occupation : Bounty Hunter
While Loop:
While loops are another useful construct to loop through data. While loops continue to loop until the expression in the while loop evaluates to false. A common use of the while loop is to return the rows in an array from a result set. The while loop continues to execute until all of the rows have been returned from the result set.
Syntax:
while (condition){
statement(s);
}
Example:
<?php
$i=1;
while($i<=9)
{
echo "The number is " . $i . "<br />";
$i++;
}
?>
Do-While Loop:
Do while loops are another useful loop construct. Do while loops work exactly the same as normal while loops, except that the script evaluates the while expression after the loop has completed, instead of before the loop executes, as in a normal while loop.
The important difference between a do while loop and a normal while loop is that a do while loop is always executed at least once. A normal while loop may not be executed at all, depending on the expression. The following do while loop is executed one time, printing out $i to the screen:
$i = 0;
do {
print $i;
} while ($i>0);
Whereas the following is not executed at all:
$i = 0;
while($i > 0) {
print $i;
}
Arrays:
An array is a data structure that stores one or more values in a single value. PHP supports both numerical arrays (array items are indexed by their numerical order) as well as associative arrays (array items are indexed by named keys).
Example:
$a = array(1, 2, 3, 4);
//$a[0] = 1
//$a[1] = 2
//$a[2] = 3
//$a[3] = 4
$b = array("name"=>"Fred", "age" => 30);
//$b['name'] = "Fred"
//$b['age'] = 30
php functions
In addition to PHP's built-in functions, you can create your own functions. Remember that if you want to use variables that exist outside of the function, then you must declare them as global variables or assign them as arguments to the function itself
function check_age($age) {
if ($age > 21) {
return 1;
} else {
return 0;
}
}
//usage:
if(check_age($age)) {
echo "You may enter!";
} else {
echo "Access Not Allowed!";
exit();
}
The function would be called from within your script at the appropriate time, using the following syntax:
$age = 10;
check_age($age);
Example:
function simpletable($text) {
print("<table border=0 cellpadding=0 cellspacing=0><tr>");
print("<td>$text</td>");
print("</tr></table>");
}
phpinfo( )
phpinfo() is a very useful function that allows you to see what version of PHP is running on your Web server, as well as all of the specific settings that are enabled in that version.
<?
phpinfo();
?>
php forms
Forms are the most fundamental method of interaction for your users. Users must use a form to enter information into a site. Think about it, every bulletin board, shopping cart, feedback form, and poll is a type of form. Without forms, the Web is nothing more than a publishing medium for those who can FTP Web pages up to a server.
GET & POST Methods:
There are two methods that you can use when creating a form in HTML. They are post and get, as in:
<form action=”saveinfo.php” method=post>
or:
<form action=”saveinfo.php” method=get>
If you don't specify a method, then the Web server assumes that you are using the get method. So what's the deal? They do the same thing right? Well, almost. You may have noticed that the URL looks a lot longer after you submit a form that uses the get method. For example, you may see something like:
http://studiesinn.com/form.php?name=fred&age=20&comments=This+site+rocks
That's because the get method puts the contents of the form right in the URL. There are a few disadvantages to this. First, depending on your Web server's operating system and software, there is a limit to how much data you can send through a form using the get method. On many systems, this limit is 256 characters. Also, the individual get queries may be stored in your Web server logs. If you are using space on a shared server, then other people may be able to decipher data sent from your forms that use the get method.
The post method was created to correct the inadequacies of the get method. The information sent using the post method is not visible in the URL, and form data cannot be deciphered by looking in the Web server logs. There also isn't such a small limit on the amount of data that can be sent from a form. Again, it depends on your server, but you probably won't ever hit the limit of sending data using the post method for a text-based form.
I use the post method for my scripts unless I need to debug something. When you need to debug something on a form, it is easy enough to switch to the get method (by changing the action line in your script) and then check the URL after you submit your buggy form. You can usually pick up typos and such with a quick look.
Example:
<form action="display.php" method="get">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
When you click the "Submit" button on the form the URL sent could look something like this:
http://www.studiesinn.com/display.php?name=adam&age=22
On display.php page you can retrieve the value by using the following code
Welcome <?php echo $_GET["name"]; ?>.<br />
You are <?php echo $_GET["age"]; ?> years old!
The $_REQUEST Variable:
The PHP $_REQUEST variable can be used as the replacement of $_GET, $_POST, and $_COOKIE. The $_REQUEST variable can be used to get the result from form data sent with both the GET and POST methods.
Example:
<form action="display.php" method="post">
Enter your name: <input type="text" name="name" />
Enter your age: <input type="text" name="age" />
<input type="submit" />
</form>
When you click the Submit button on the form, following URL will be sent.
http://www.studiesinn.com/display.php
On display.php page you can retrieve the value as
Welcome <?php echo $_POST["name"]; ?>.<br />
You are <?php echo $_POST["age"]; ?> years old!
If you need moreClick here for further readingyou will get usful links
Get Started:
<h1><? echo “First PHP Page.”; ?>!</h1>
The code above, when viewed via a Web server, simply prints out String enclosed in the quotes.
In general, individual lines of PHP code should end with a semicolon, although it is not necessary to use semicolons if a beginning or an ending bracket is used (this will make sense when you look at if/then statements).
For example:
<?
echo "<p>a line of code";
echo "<p>another line of code;
?>
Variables in PHP are denoted by the "$". To assign the value "Hello World" to the variable $str, you simply call it in your code:
$str = "Hello World";
Strings must be enclosed by quotes, but they can contain variables themselves:
$a = 12;
$string = "The value of a is $a";
// $string = "The Value of a is 12";
Variables in PHP are denoted by the "$". To assign the value "Hello World" to the variable $str, you simply call it in your code:
$str = "Hello World";
Strings must be enclosed by quotes, but they can contain variables themselves:
$a = 12;
$string = "The value of a is $a";
// $string = "The Value of a is 12";
PHP variables do not have to be declared ahead of time, nor do they require a type definition. Note that you may get a warning about using undeclared variables if you try to use them before giving them a value (depending on how you set up error reporting in php.ini). For example:
$a = 4;
$c = $a + $b;
// $c = 4, but a warning appears "Warning: Undefined variable..".
Warnings do not stop a script from continuing. If you forgot to add a semicolon at the end of one of the lines, then you would get a Parser error, which prohibits the script from running.
Since PHP variables are not typed, you don't have to worry about performing mathematical equations on the wrong type, as you might in C. For example:
$a = 4;
$b = "5";
$c = $a + $b;
// $c = 9;
PHP also supports boolean variables, which can be assigned either a one or a zero, or the words true or false:
$a = true;
$b = 1;
//$a = $b
$c = false;
$d = 0;
//$c = $d
Variable Naming Rules
A variable name must start with a letter or an underscore.
A variable name can only contain alpha-numeric characters and underscores (a-Z, 0-9, and _ )
Variables
A variable name should not contain spaces. If a variable name is more than one word, it should be separated with underscore ($my_string), or with capitalization ($myString)
PHP variables do not have to be declared ahead of time, nor do they require a type definition. Note that you may get a warning about using undeclared variables if you try to use them before giving them a value (depending on how you set up error reporting in php.ini). For example:
$a = 4;
$c = $a + $b;
// $c = 4, but a warning appears "Warning: Undefined variable..".
Warnings do not stop a script from continuing. If you forgot to add a semicolon at the end of one of the lines, then you would get a Parser error, which prohibits the script from running.
Since PHP variables are not typed, you don't have to worry about performing mathematical equations on the wrong type, as you might in C. For example:
$a = 4;
$b = "5";
$c = $a + $b;
// $c = 9;
PHP also supports boolean variables, which can be assigned either a one or a zero, or the words true or false:
$a = true;
$b = 1;
//$a = $b
$c = false;
$d = 0;
//$c = $d
Variable Naming Rules
A variable name must start with a letter or an underscore.
A variable name can only contain alpha-numeric characters and underscores (a-Z, 0-9, and _ )
A variable name should not contain spaces. If a variable name is more than one word, it should be separated with underscore ($my_string), or with capitalization ($myString)
php Strings
String variables are used to store and manipulate a piece of text.
<?php
$msg="Hello World";
echo $msg;
?>
The output will be
Hello World
The Concatenation Operator
Dot (.) is used as concatenation operator. To concatenate two variables together, use the dot (.) operator:
<?php
$str1="Hello World";
$str2="Adam";
echo $ str1. " " . $ str2;
?>
Hello World Adam
Length of a String:
The strlen() function is used to find the length of a string.
<?php
$string=”Hello World”;
echo strlen($string);
?>
Output will be
11
The strpos() function:
The strpos() function is used to search for a string or character within another string.
If the string is found in the string, this function will return the position of the first match. If no match is found, it will return FALSE.
<?php
echo strpos("Example of strops function","of");
?>
The output will be
8
Operators
Operators are used to manipulate or perform operations on variables and values. There are many operators used in PHP, we have separated them into the following categories.
Assignment Operators
Arithmetic Operators
Comparison Operators
Logical Operators
Assignment Operators:
Assignment operators are used to set a variable equal to a value or set a variable to another variable's value. ‘=’ is used for assignment operator.
Example:
$var = 23;
$var2 = $var;
Operator Example Is The Same As
= a=b a=b
+= a+=b a=a+b
-= a-=b a=a-b
*= a*=b a=a*b
/= a/=b a=a/b
.= a.=b a=a.b
%= a%=b a=a%b
Arithmetic Operators:
Operator Function
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus (division remainder)
++ Increment
-- Decrement
Comparison Operators:
Operator Function Example Result
== Equal To $x == $y false
!= Not Equal To $x != $y true
<
Less Than $x < $y true
> Greater Than $x > $y false
<= Less Than or Equal To $x <= $y true
>= Greater Than or Equal To $x >= $y false
Logical Operators:
Operator Function Example
&& and x=8 y=4
(x < 10 && y > 1) returns true
|| or x=12
y=8
(x>=5 || y<=5) returns true
! not x=6
y=3
!(x==y) returns true
Loops
Control structures if/else statment
One of the most common PHP language constructs that you will encounter is the if…then statement. The if…then statement allows you to evaluate an expression and then, depending if it is true or not, take a course of action. For example:
$x = 1;
if($x) {
echo "True!";
}
Since $x = 1, then PHP interprets it in the context of the if statement to be a boolean type. Since 1 = true, the if statements prints out the message. Literally you can read it as "If True, then print out "True!".
Another example, but this time with an "else" clause:
$a = 5;
$b = "10";
if($a > $b) {
echo "$a is greater than $b";
} else {
echo "$a is not greater than $b";
}
PHP doesn't care that $a is an integer and $b is a string. It recognizes that $b also makes a pretty good integer as well. It then evaluates the expression, "If $a is greater than $b then print out that $a is greater than $b; if not (else) then print out $a is not greater than $b." It's also important to note that you don't say, "$a is less than $b," since it is possible that $a could be equal to $b. If that is the case, then the second part of the expression, the else statement, is the part that runs.
Note the use of brackets to enclose the actions. Also note the lack of semicolons where the brackets are used.
One final example is the if…elseif…else statement:
if($a == $b) {
// do something
} elseif ($a > $b) {
// do something else
} elseif($a < $b) {
// do yet something else
} else {
// if nothing else we do this...
}
Switch Statement:
The Switch statement is used to perform one of several different actions based on one of several different conditions. Switch statements allow for additional evaluations of the data, even though one of the cases may have been met. Switch statements can also save you from typing many if…elseif…elseif… statements.
Syntax:
switch (expression)
{
case label1:
Statement(s);
break;
case label2:
Statement(s);
break;
default:
Statement(s);
}
Example:
$a = "100";
switch($a) {
case(10):
echo "The value is 10";
break;
case (100):
echo "The value is 100<br>";
case (1000):
echo "The value is 1000";
break;
default:
echo "<p>Wrong Input";
}
switch statements have four basic parts:
The switch— The switch identifies what value or expression is going to be evaluated in each of the cases. In the example above, you tell the switch statement that you are going to evaluate the variable $a in the subsequent cases.
The case— The case statement evaluates the variable you passed from the switch command. You can use case() the same way you'd use an if statement. If the case holds true, then everything after the case statement is executed, until the parser encounters a break command, at which point execution stops and the switch is exited.
Breakpoints— Defined by the break command, exit the parser from the switch. Breakpoints are normally put after statements executed when a case() is met.
Default— The default is a special kind of case. It is executed if none of the other case statements in the switch have been executed.
PHp Loops
Loops are used to repeat statement or block of statement more than one time. The idea of a loop is to do something over and over again until the task has been completed.
In PHP we have the following looping statements:
For Loop:
For loops are useful constructions to loop through a finite set of data, such as data in an array.
Syntax:
for ( initialization; conditional statement; increment/decrement){
statement(s);
}
Initialization—You can pass in an already assigned variable or assign a value to the variable in the for statement.
The condition required to continue the for loop— The statements are executed until the given condition goes false.
Statement to modify the counter on each pass of the loop.
Example:
<?php
for ($i=1; $i<=10; $i++)
{
echo "Hello World<br />";
}
?>
Foreach Loop:
Foreach loops allow you to quickly traverse through an array.
$array = array("name" => "Jet", "occupation" => "Bounty Hunter" );
foreach ($array as $val) {
echo "<P>$val";
}
Prints out:
<P>Jet
<P>Bounty Hunter
You can also use foreach loops to get the key of the values in the array:
foreach ($array as $key => $val) {
echo "<P>$key : $val";
}
Prints out:
<P>name : Jet
<P>occupation : Bounty Hunter
While Loop:
While loops are another useful construct to loop through data. While loops continue to loop until the expression in the while loop evaluates to false. A common use of the while loop is to return the rows in an array from a result set. The while loop continues to execute until all of the rows have been returned from the result set.
Syntax:
while (condition){
statement(s);
}
Example:
<?php
$i=1;
while($i<=9)
{
echo "The number is " . $i . "<br />";
$i++;
}
?>
Do-While Loop:
Do while loops are another useful loop construct. Do while loops work exactly the same as normal while loops, except that the script evaluates the while expression after the loop has completed, instead of before the loop executes, as in a normal while loop.
The important difference between a do while loop and a normal while loop is that a do while loop is always executed at least once. A normal while loop may not be executed at all, depending on the expression. The following do while loop is executed one time, printing out $i to the screen:
$i = 0;
do {
print $i;
} while ($i>0);
Whereas the following is not executed at all:
$i = 0;
while($i > 0) {
print $i;
}
Arrays:
An array is a data structure that stores one or more values in a single value. PHP supports both numerical arrays (array items are indexed by their numerical order) as well as associative arrays (array items are indexed by named keys).
Example:
$a = array(1, 2, 3, 4);
//$a[0] = 1
//$a[1] = 2
//$a[2] = 3
//$a[3] = 4
$b = array("name"=>"Fred", "age" => 30);
//$b['name'] = "Fred"
//$b['age'] = 30
php functions
In addition to PHP's built-in functions, you can create your own functions. Remember that if you want to use variables that exist outside of the function, then you must declare them as global variables or assign them as arguments to the function itself
function check_age($age) {
if ($age > 21) {
return 1;
} else {
return 0;
}
}
//usage:
if(check_age($age)) {
echo "You may enter!";
} else {
echo "Access Not Allowed!";
exit();
}
The function would be called from within your script at the appropriate time, using the following syntax:
$age = 10;
check_age($age);
Example:
function simpletable($text) {
print("<table border=0 cellpadding=0 cellspacing=0><tr>");
print("<td>$text</td>");
print("</tr></table>");
}
phpinfo( )
phpinfo() is a very useful function that allows you to see what version of PHP is running on your Web server, as well as all of the specific settings that are enabled in that version.
<?
phpinfo();
?>
php forms
Forms are the most fundamental method of interaction for your users. Users must use a form to enter information into a site. Think about it, every bulletin board, shopping cart, feedback form, and poll is a type of form. Without forms, the Web is nothing more than a publishing medium for those who can FTP Web pages up to a server.
GET & POST Methods:
There are two methods that you can use when creating a form in HTML. They are post and get, as in:
<form action=”saveinfo.php” method=post>
or:
<form action=”saveinfo.php” method=get>
If you don't specify a method, then the Web server assumes that you are using the get method. So what's the deal? They do the same thing right? Well, almost. You may have noticed that the URL looks a lot longer after you submit a form that uses the get method. For example, you may see something like:
http://studiesinn.com/form.php?name=fred&age=20&comments=This+site+rocks
That's because the get method puts the contents of the form right in the URL. There are a few disadvantages to this. First, depending on your Web server's operating system and software, there is a limit to how much data you can send through a form using the get method. On many systems, this limit is 256 characters. Also, the individual get queries may be stored in your Web server logs. If you are using space on a shared server, then other people may be able to decipher data sent from your forms that use the get method.
The post method was created to correct the inadequacies of the get method. The information sent using the post method is not visible in the URL, and form data cannot be deciphered by looking in the Web server logs. There also isn't such a small limit on the amount of data that can be sent from a form. Again, it depends on your server, but you probably won't ever hit the limit of sending data using the post method for a text-based form.
I use the post method for my scripts unless I need to debug something. When you need to debug something on a form, it is easy enough to switch to the get method (by changing the action line in your script) and then check the URL after you submit your buggy form. You can usually pick up typos and such with a quick look.
Example:
<form action="display.php" method="get">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
When you click the "Submit" button on the form the URL sent could look something like this:
http://www.studiesinn.com/display.php?name=adam&age=22
On display.php page you can retrieve the value by using the following code
Welcome <?php echo $_GET["name"]; ?>.<br />
You are <?php echo $_GET["age"]; ?> years old!
The $_REQUEST Variable:
The PHP $_REQUEST variable can be used as the replacement of $_GET, $_POST, and $_COOKIE. The $_REQUEST variable can be used to get the result from form data sent with both the GET and POST methods.
Example:
<form action="display.php" method="post">
Enter your name: <input type="text" name="name" />
Enter your age: <input type="text" name="age" />
<input type="submit" />
</form>
When you click the Submit button on the form, following URL will be sent.
http://www.studiesinn.com/display.php
On display.php page you can retrieve the value as
Welcome <?php echo $_POST["name"]; ?>.<br />
You are <?php echo $_POST["age"]; ?> years old!
If you need moreClick here for further readingyou will get usful links
صفحة 1 من اصل 1
صلاحيات هذا المنتدى:
لاتستطيع الرد على المواضيع في هذا المنتدى
2014-03-07, 2:55 am من طرف Admin
» موقف الفقه السني من الزنا هل صحيح
2014-03-07, 2:36 am من طرف Admin
» تحميل صور خلفيات شاشة حاسبة جميلة
2012-07-15, 2:00 am من طرف Admin
» فوئد الاكلات القاعدية والحامضية وتصنيفاتها
2012-05-29, 12:02 am من طرف Admin
» هل تعلم الفرق بين الضاد والظاء
2012-04-18, 7:22 pm من طرف Admin
» احراز وعوذات مروية عن رسول الله ص واهل البيت عليهم السلام
2012-04-02, 4:16 am من طرف Admin
» الهندباء علاج لكثير من الامراض فوائده ومحاذيره وطرق تحضيره
2012-03-28, 3:43 pm من طرف Admin
» هل تعلم لماذا نهى الرسول الكريم عن لمس الكلب.....حقيقة علمية جديدة
2012-03-15, 2:46 am من طرف Admin
» فوائد عصير الخيار لصحة الانسان
2012-03-07, 2:46 am من طرف Admin
» فوائد الشاي الاخضر مضاره وطريقة تحضيره
2012-03-04, 8:30 pm من طرف Admin
» فوائد الزعتر
2012-02-15, 11:48 pm من طرف Admin
» فوائد اليانسون وشربه في الشتاء
2012-02-15, 11:32 pm من طرف Admin
» الثوم علاج ناجع للاصابة بالفطريات الجلدية
2012-01-25, 11:13 pm من طرف Admin
» قصيدة رائعة
2012-01-21, 4:29 pm من طرف Admin
» حمل مجانا لعبة مطاردات الشرطة
2012-01-20, 12:33 am من طرف Admin