Contents
0) Create subfolder 107.
-----
Flow Chart:
-----
Pseudo Code:
1. BEGIN
2. INPUT var1, var2
3. PRINT var1+var2
4. END
-----
PHP Language sample code:
Note:
$_SERVER['PHP_SELF'] refers to a server variable that stores the value of the current script file (in the example below, it is sumvariables.php)
However, an empty string (e.g “”) is also a valid URI that point to the current script file as well.
sumvariables.php
|
<form method="POST" action="" >
input1: <input type="text" name="input1"><br/>
input2: <input type="text" name="input2"><br/>
<input type="submit" name="submit">
</form>
<?php
//get value from POST input
$var1=$_POST["input1"];
$var2=$_POST["input2"];
echo $var1+$var2;
?>
|
-----
PHP Language sample output :
Note: For the first time running, you would get an error message because the form has not been submitted yet, therefore there is no value for input1 and input2.
After you have entered the values (input1=1 an input2=2) and click submit button, you should get the following output:
-----
Flow Chart:
-----
Pseudo Code:
1. BEGIN
2. INPUT var1, var2
3. PRINT var1 x var2
4. END
-----
PHP Language sample code:
multiplyvariables.php
|
<form method="POST" action="" >
input1: <input type="text" name="input1"><br/>
input2: <input type="text" name="input2"><br/>
<input type="submit" name="submit">
</form>
<?php
//get value from POST input
$var1=$_POST["input1"];
$var2=$_POST["input2"];
echo $var1*$var2;
?>
|
-----
PHP Language sample output:
-----
Flow Chart:
-----
Pseudo Code:
1. BEGIN
2. INPUT var1, var2
3. IF var2=0
4. THEN PRINT "Error. Division by zero."
5. ELSE PRINT var1/var2
6. END
-----
PHP Language sample code:
dividevariables.php
|
<form method="POST" action="" >
input1: <input type="text" name="input1"><br/>
input2: <input type="text" name="input2"><br/>
<input type="submit" name="submit">
</form>
<?php
//get value from POST input. try with input 1,0
$var1=$_POST["input1"];
$var2=$_POST["input2"];
if ($var2==0)
{ echo "Error. Division by zero.";}
else
{ echo $var1/$var2;}
?>
|
-----
PHP Language sample output:
-----
Flow Chart:
-----
Pseudo Code:
1. BEGIN
2. PRINT "Start counting."
3. FOR i LOOP := 1 to 3
4. PRINT i
5. END LOOP
6. PRINT "Done."
7. END
-----
PHP Language sample code:
countnumbers.php
|
<form method="POST" action="" >
input1: <input type="text" name="input1"><br/>
input2: <input type="text" name="input2"><br/>
<input type="submit" name="submit">
</form>
<?php
echo "Start counting."."<br/>";
for ($x=$_POST["input1"]; $x<=$_POST["input2"]; $x++) {
echo $x."<br/>";
}
echo "Counting done."."<br/>";
?>
|
-----
PHP Language sample output:
-----
Flow Chart:
(Note: Change Init to Input)
-----
Pseudo Code:
1. BEGIN
2. INPUT ARRAY arr[5] TO CONTAIN VALUES 1,2,3,4,5
3. PRINT "Displaying array values."
4. FOR i := 1 to 5
5. PRINT arr[i]
6. END FOR LOOP
7. PRINT "Done."
8. END
-----
PHP Language sample code:
displayarrayvalues.php
|
<form method="POST" action="" >
input1: <input type="text" name="input1"><br/>
<input type="submit" name="submit">
</form>
<?php
$str=$_POST["input1"];
$arr=explode(",",$str);
echo "Display array values"."<br/>";
for ($x=0; $x<5; $x++) {
echo $arr[$x]."<br/>";
}
echo "Done"."<br/>";
?>
|
-----
PHP Language sample output:
-----
Note:
This tutorial demonstrates the steps to edit an array value. The program displays the array values before and after the edit is done. There are code redundancies here. Ignore this for now.
This tutorial demonstrates the steps to edit an array value. The program displays the array values before and after the edit is done. There are code redundancies here. Ignore this for now.
-----
Flow Chart:
-----
Pseudo Code:
1. BEGIN
2. INITIALIZE ARRAY arr[i] WITH VALUES 1,2,3,4,5
3. FOR i := 1 to 5
4. PRINT arr[i]
5. END FOR LOOP
6. INPUT indexnumber
7. INPUT arrayvalue
8. arr[indexnumber] = arrayvalue
9. FOR i := 1 to 5
10. PRINT arr[i]
11. END FOR LOOP
12. END
-----
PHP Language sample code:
editarrayvalues.php
|
<form method="POST" action="" >
input1: <input type="text" name="input1"><br/>
input2: <input type="text" name="input2"><br/>
<input type="submit" name="submit">
</form>
<?php
$arr=array('a','b','c','d','e');
echo "Display array values"."<br/>";
for ($x=0; $x<5; $x++) {
echo $arr[$x]."<br/>";
}
echo "Done"."<br/>";
$indexnumber=$_POST['input1'];
$arrayvalue=$_POST['input2'];
$arr[$indexnumber]=$arrayvalue;
echo "Display array values"."<br/>";
for ($x=0; $x<5; $x++) {
echo $arr[$x]."<br/>";
}
echo "Done"."<br/>";
?>
|
-----
PHP Language sample output:
-----
Flow Chart:
-----
Pseudo Code:
1. BEGIN displaydata
2. FOR all item in array PRINT item values
3. END displaydata
1. BEGIN displaydata
2. FOR all item in array PRINT item values
3. END displaydata
1. BEGIN updatedata
2. INPUT indexnumber, arrayvalue
3. arr[indexnumber]=arrayvalue
2. INPUT indexnumber, arrayvalue
3. arr[indexnumber]=arrayvalue
4. END updatedata
1. BEGIN main
2. CALL displaydata
3. CALL updatedata
4. CALL displaydata
5. END main
2. CALL displaydata
3. CALL updatedata
4. CALL displaydata
5. END main
-----
PHP Language sample code:
modularprogram.php
|
<form method="POST" action="" >
input1: <input type="text" name="input1"><br/>
input2: <input type="text" name="input2"><br/>
<input type="submit" name="submit">
</form>
<?php
$arr=array('a','b','c','d','e');
function displayData($arr){
echo "Display array values"."<br/>";
for ($x=0; $x<5; $x++) {
echo $arr[$x]."<br/>";
}
echo "Done"."<br/>";
}
function updateData(&$arr){
echo "Updating Data..."."<br/>";
$indexnumber=$_POST['input1'];
$arrayvalue=$_POST['input2'];
$arr[$indexnumber]=$arrayvalue;
}
displayData($arr);
updateData($arr);
displayData($arr);
?>
|
-----
PHP Language sample output:
10) How to avoid error display during the first time running.
To check that the form has been submitted, use:
if (isset($_POST['submit'])) {
…(put all your existing PHP codes here)...
}
example:
sumvariables.php
|
<form method="POST" action="" >
input1: <input type="text" name="input1"><br/>
input2: <input type="text" name="input2"><br/>
<input type="submit" name="submit">
</form>
<?php
if (isset($_POST['submit'])) {
//assume user input 1,2
$var1=$_POST["input1"];
$var2=$_POST["input2"];
echo $var1+$var2;
}
?>
|
11) How to ensure all input fields are correct.
Use a string variable $strError to store error messages while checking if each of the input field is empty.
Next, if $strError is empty, display the result of sum operation. Else, display the $strError value
example:
sumvariables.php
|
<form method="POST" action="" >
input1: <input type="text" name="input1"><br/>
input2: <input type="text" name="input2"><br/>
<input type="submit" name="submit">
</form>
<?php
if ($_POST["submit"]) {
$strError=null;
if (empty($_POST["input1"])) {
$strError .= "Input1 is required.";
} else {
$var1 = $_POST["input1"];
}
if (empty($_POST["input2"])) {
$strError .= "Input2 is required.";
} else {
$var2 = $_POST["input2"];
}
if (empty($strError)) {
echo $var1+$var2;
} else {
echo "Error: ".$strError;
}
}
?>
|
12) How to hide the form after it has been submitted?
Check if the form has been submitted.
If submit is true, process.
If submit is false, show the form.
There are two techniques to show the html form within the php codes.
first technique:
sumvariables.php
|
<?php
if ($_POST["submit"]) {
$strError=null;
if (empty($_POST["input1"])) {
$strError .= "Input1 is required.";
} else {
$var1 = $_POST["input1"];
}
if (empty($_POST["input2"])) {
$strError .= "Input2 is required.";
} else {
$var2 = $_POST["input2"];
}
if (empty($strError)) {
echo $var1+$var2;
} else {
echo "Error: ".$strError;
}
}
else{
?> //break php tag
<form method="POST" action="" >
input1: <input type="text" name="input1"><br/>
input2: <input type="text" name="input2"><br/>
<input type="submit" name="submit">
</form>
//continue with php tag
<?php
}
?>
|
second technique:
sumvariables.php
|
<?php
if (isset($_POST['submit'])) {
$strError=null;
if (empty($_POST["input1"])) {
$strError .= "Input1 is required.";
} else {
$var1 = $_POST["input1"];
}
if (empty($_POST["input2"])) {
$strError .= "Input2 is required.";
} else {
$var2 = $_POST["input2"];
}
if (empty($strError)) {
echo $var1+$var2."<br/>";
} else {
echo "Error: ".$strError."<br/>";
}
}
else{
echo "<form method='POST' action='' >";
echo "input1: <input type='text' name='input1'><br/>";
echo "input2: <input type='text' name='input2'><br/>";
echo "<input type='submit' name='submit'>";
echo "</form>";
}
?>
|
13) How to allow the user to re-enter the form?
Provide a hyperlink to the script file.
first technique:
sumvariables.php
|
<?php
if ($_POST["submit"]) {
$strError=null;
if (empty($_POST["input1"])) {
$strError .= "Input1 is required.";
} else {
$var1 = $_POST["input1"];
}
if (empty($_POST["input2"])) {
$strError .= "Input2 is required.";
} else {
$var2 = $_POST["input2"];
}
if (empty($strError)) {
echo $var1+$var2."<br/>";
} else {
echo "Error: ".$strError."<br/>";
}
echo "<a href=''>Reenter data</a>";
}
else{
?>
<form method="POST" action="<?php $_SERVER['PHP_SELF']?>" >
input1: <input type="text" name="input1"><br/>
input2: <input type="text" name="input2"><br/>
<input type="submit" name="submit">
</form>
<?php
}
?>
|
second technique:
sumvariables.php
|
<?php
if (isset($_POST['submit'])) {
$strError=null;
if (empty($_POST["input1"])) {
$strError .= "Input1 is required.";
} else {
$var1 = $_POST["input1"];
}
if (empty($_POST["input2"])) {
$strError .= "Input2 is required.";
} else {
$var2 = $_POST["input2"];
}
if (empty($strError)) {
echo $var1+$var2."<br/>";
} else {
echo "Error: ".$strError."<br/>";
}
echo "<a href=''>Reenter data</a>";
}
else{
echo "<form method='POST' action='' >";
echo "input1: <input type='text' name='input1'><br/>";
echo "input2: <input type='text' name='input2'><br/>";
echo "<input type='submit' name='submit'>";
echo "</form>";
}
?>
|
14) How to consolidate operations (sum, multiply and division) into a single script file?
Add a third input field in the form of select tag (“sum”,”multiply”,”division”).
Example (using second technique):
multipleoperations.php
|
<?php
if (isset($_POST['submit'])) {
$strError=null;
if (empty($_POST["input1"])) {
if($_POST["input1"]==0)
{$var1=($_POST["input1"]);}
else
{$strError .= "Input1 is required.";} }
else {
$var1 = $_POST["input1"];
}
if (empty($_POST["input2"])) {
if($_POST["input2"]==0)
{$var2=($_POST["input2"]);}
else
{$strError .= "Input2 is required.";}
} else {
$var2 = $_POST["input2"];
}
if (empty($_POST["selectOperation"])) {
$strError .= "operation is required.";
} else {
$varSelect = $_POST["selectOperation"];
}
if (empty($strError)) {
switch ($varSelect){
case "sum":
echo "sum:".($var1+$var2);
break;
case "multiply":
echo "multiply:".($var1*$var2);
break;
case "division":
//using ternary operator (inline if)
echo "division:".(($var2>0)? ($var1/$var2):"Error.Division by zero");
break;
}
echo "<br/>";
} else {
echo "Error: ".$strError."<br/>";
}
echo "<a href=''>Reenter data</a>";
}
else{
echo "<form method='POST' action='' >";
echo "input1: <input type='text' name='input1'><br/>";
echo "input2: <input type='text' name='input2'><br/>";
echo "<select name='selectOperation'>";
echo "<option value=''>-Select-</option>";
echo "<option value='sum'>Sum</option>";
echo "<option value='multiply'>Multiply</option>";
echo "<option value='division'>Division</option>";
echo "</select>";
echo "<br/>";
echo "<input type='submit' name='submit'>";
echo "</form>";
}
?>
|
No comments:
Post a Comment