0% found this document useful (0 votes)
81 views47 pages

PHP 2

The document discusses PHP forms and control structures. It provides examples of how to create HTML forms in PHP, read form data, and process it using control structures like if/else, while loops, and switch statements. Key points covered include how to define a basic HTML form, read submitted form data using the $_POST and $_GET superglobals, and conditionally output different responses using if/else statements depending on the submitted data.

Uploaded by

Mladen Radojevic
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views47 pages

PHP 2

The document discusses PHP forms and control structures. It provides examples of how to create HTML forms in PHP, read form data, and process it using control structures like if/else, while loops, and switch statements. Key points covered include how to define a basic HTML form, read submitted form data using the $_POST and $_GET superglobals, and conditionally output different responses using if/else statements depending on the submitted data.

Uploaded by

Mladen Radojevic
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 47

PHP (Lekcija 2)

Miladin Stefanović
[email protected]
PHP
 2 predavanje
 Forme
 Kontrolne stukture
Forme HTML
Forma
 <html>
<head></head>
<body>
<form action="message.php" method="post">
Unesi svoju poruku: <input type="text" name="msg" size="30">
<input type="submit" value="Send">
</form>
</body>
</html>

 Najvažnija linija je <form> tag

<form action="message.php" method="post">


...
</form>
Čitanje i procesiranje podataka
 <html>
<head></head>
<body>

<?php
// Prihvatanje poslatih podataka iz forme
$input = $_POST['msg'];
// use it
echo “Porucio su: <i>$input</i>";
?>

</body>
</html>
Forme
 <form name="form1" method="post" action="index.php">
 <p> <select name="select">
 <option value="jedan">1</option>
 <option value="dva">2</option>
 </select> </p>
 <p> <textarea name="textarea"></textarea></p>
 <p> <input type="checkbox" name="m" value="m"></p>
 <p> <input type="submit" name="Submit" value="Submit">
 <input type="reset" name="Submit2" value="Reset"> </p>
 </form>
 <a href=index.php?action=viewedit&id=<?
echo $id; ?>>промени</a>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href=index.php?action=del&id=<?
echo $id; ?>>избриши</a></font>
 $id=$_GET[‘id’];
 $action=$_GET[‘action’];
Kontrolne strukture
 if  continue
 else  switch
 elseif/else if  return
 Skracena sintaksa  require
 while  include
 do-while  require_once
 for  include_once
 foreach  goto
 break
If-else
 if (condition) {
uradi 1
}
else {
uradi 2
}
If-else
 <?php
if ($a > $b)
echo "a je vece od b";
?>
Primer
 <html>
<head></head>
<body>
<form action=“godine.php" method="post">
Unesi godine: <input name="age" size="2">
</form>
</body>
</html>
godine.php
 <html>
<head></head>
<body>
<?php
// prezimanje podataka
$age = $_POST['age'];
// procesiranje
if ($age >=18) {
echo ‘Punoletan si';
}
if ($age < 18) {
echo “Maloletan si";
}
?>
</body>
</html>
Skraćeno pisanje ☺
 <?php  <?php

if ($numTries > 10) { $msg = $numTries >


$msg = 'Blocking 10 ? 'Blocking your
your account...'; account...' :
} 'Welcome!';
else {
$msg = 'Welcome!'; ?>
}

?>
Skraćeno pisanje
 <?php if ($a == 5): ?>
A je jednako 5
<?php endif; ?>

 <?php
$hour = 11;

print $foo = ($hour < 12) ? "Good morning!" :


"Good afternoon!";

?>
Ugnježdavanje
 <?php
if ($dan == ‘Sreda') {
if ($vreme == ‘0915') {
if ($fakultet == ‘PMF') {
$predmet = ‘Web programiranje';
}
}
}
?>
…ili mnogo elegantnije ☺
 <?php

if ($dan == ‘Sreda' && $vreme == '0915' &&


$fakultet == ‘PMF') {
$predmet = ‘Web programiranje';
}

?>
if-elseif-else
 if (prvi uslov je T) {
akcija!
}
elseif (drugi uslov je T) {
akcija!
}
elseif (treciuslov je T) {
akcija!
}
... itd...
else {
akcija!
}
Primer
 <html>
<head></head>
<body>
<h2>Specijalitet kuce</h2>
<p>
<form method="get" action="cooking.php">
<select name="day">
<option value="1">Ponedeljak / Sreda
<option value="2">Utorak / Cetvrtak
<option value="3">Petak / Nedelja
<option value="4">Subota
</select>
<input type="submit" value="Send">
</form>
</body>
</html>
cooking.php
 <?php  elseif ($day == 3) {
// get form selection $special = ‘prasetina';
$day = $_GET['day']; }
// Provera else {
if ($day == 1) { $special = ‘jaretina';
$special = piletina'; }
} ?>
elseif ($day == 2) {
$special = jagnjetina'; <h2>Specijalitet
} kuce:</h2>
<?php echo $special; ?>
</body>
</html>
while
 while (izraz):
 statement ...
 endwhile;
Primer1
 <?php  <?php
/* primer 1 */ /* primer 2 */

$i = 1; $i = 1;
while ($i <= 10) { while ($i <= 10):
echo $i++; echo $i;
} $i++;
?> endwhile;
?>
Do-while
 <?php
$i = 0;
do {
echo $i;
} while ($i > 0);
?>
Primer 2
 <html>  <html>
<head></head> <head></head>
<body> <body>
<form action=“kvadrati.php"
method="POST"> <?php
Svi kvadrati izmedju 1 i <input
type="text" name="limit" size="4" // setuj promenljive
maxlength="4"> $upperLimit = $_POST['limit'];
<input type="submit" name="submit" $lowerLimit = 1;
value=“Racunaj"> // racunanje kvadrata
</form> while ($lowerLimit <=
</body> $upperLimit) {
</html> echo ($lowerLimit *
$lowerLimit).'&nbsp;';
$lowerLimit++;
}
// stampanje kraja posla
echo 'END';
?>
</body>
</html>
for
 for (expr1; expr2; expr3) statement
 Expr 1 se uvek, bezuslovno izvršava na početku
petlje.
 Na početku svake iteracije Expr2 se procenjuje.
Ukoliko je TRUE, petlja se nastavlja i izvršavaju se
ugnježdene naredbe. Ukoliko je FALSE izvršavanje
petlje se prekida.
 Na kraju svake iteracije Expr 3 se izvršava, ocenjuje.
Primeri 1
for ($i = 1; $i <= 10; $i++)  for ($i = 1; ; $i++) {
{ if ($i > 10) {
echo $i; break;
} }
echo $i;
}
Primeri 2
 $i = 1; for ($i = 1, $j = 0; $i <= 10;
for (; ; ) { $j += $i, print $i, $i++);
if ($i > 10) {
break;
}
echo $i;
$i++;
}
foreach
 <?php
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
$value = $value * 2;
}
// $arr je sada niz (2, 4, 6, 8)
unset($value); //razresava referencu
?>
Break
 break prekida izvršavanje tekuće:
 for,
 foreach,
 while,
 do-while ili
 switch
strukture
Continue
 Continue se koristi unutar strukture petlji za
prekidanje ostatka tekuće petlje i nastavljanja
izvršavanja na oceni uslova na početku
sledeće iteracije.
Switch
 if ($i == 0) {  switch ($i) {
echo "i equals 0"; case 0:
} echo "i equals 0";
 elseif ($i == 1) { break;
echo "i equals 1"; case 1:
} echo "i equals 1";
 elseif ($i == 2) { break;
echo "i equals 2"; case 2:
} echo "i equals 2";
break;
}
Primeri 1
 switch ($i) {  switch ($i) {
case “jabuka": case 0:
echo "i je jabuka"; case 1:
break; case 2:
case “kruska": echo "i je manje
echo "i je kruska"; od 3 ali nije
break; negativno";
case “visnja": break;
echo "i je visnja"; case 3:
break; echo "i je 3";
} }
Primeri 2 default
 <?php
switch ($i) {
case 0:
echo "i equals 0";
break;
case 1:
echo "i equals 1";
break;
case 2:
echo "i equals 2";
break;
default:
echo "i is not equal to 0, 1 or 2";
}
?>
return()
 Povratak iz funkcije
require() & inlcude()
 require() i include() su vrlo slicni, sa tom razlikom
sto po nastanku greske require zaustavlja skrpit, a
include samo daje upozorenje.

 <?php require "../B.php"; ?>


 require ("desno1.php");
 include “veza.inc”;
Primer
 vars.php  test.php
<?php <?php

$boje = ‘zelena'; echo “Jedna $boja $voce";


$voce = ‘jabuka'; // Jedna

?> include 'vars.php';


echo "
Jedna $boja $voce";
 // Jedna zelena jabuka
?>
Primeri 1
 <?php

// ne radi
include 'http://www.example.com/file.txt?foo=1&bar=2';

// ne radi 'file.php?foo=1&bar=2'
include 'file.php?foo=1&bar=2';

// radi
include 'http://www.example.com/file.php?foo=1&bar=2';

$foo = 1;
$bar = 2;
include 'file.txt'; // radi
include 'file.php'; // radi
?>
Primer 2
 return.php  testreturns.php
<?php <?php
$var = 'PHP'; $foo = include 'return.php';
return $var; echo $foo; // stampa 'PHP‘
?> $bar = include 'noreturn.php';

noreturn.php echo $bar; // stampa 1


<?php
$var = 'PHP'; ?>
?>
Ranjivosti
 <?php
$page = $_GET['page'];
if (file_exists('pages/'.$page.'.php'))
{
include('pages/'.$page.'.php');
}
?>
index.php?page=/../../../../../../etc/passwd%00.html
Korisni primeri
 include("site.inc.php");
 include("header.php");
 include("menu.php");
require_once() & inlcude_once()
 Identicno kao require i include s tom razlikom sto
skript proverava da li je bilo ukljucenja require ili
include, ukoliko jeste ne radi to ponovo.
Go to
 <?php
goto a;
echo 'Foo';

a:
echo 'Bar';
?>
 // Bar
Primer 1
 <?php
do {
if ($i < 5) {
echo "i nije dovoljno veliko";
break;
}
$i *= $factor;
if ($i < $minimum_limit) {
break;
}
echo "i je ok";

/* procesiraj i */

} while (0);
?>
Primer 2
echo ("<form action='".$_SERVER['PHP_SELF']."' method='post'>");
echo ("<tr>");
echo ("<td bgcolor='#EEEEEE' width='100%' colspan='2' align='center'
class='smallBold'>");
echo ($current_mms." of ".$row_mms_count['count']);
echo ("</td>");
echo ("</tr>");
echo ("<tr>");
echo ("<td bgcolor='#EEEEEE' width='50%' align='right'>");
echo ("<input type='submit' name='submit' value='<<' class='form'>");
echo ("</td>");
echo ("<td bgcolor='#EEEEEE' width='50%'>");
echo ("<input type='submit' name='submit' value='>>' class='form'>");
echo ("</td>");
echo ("</tr>");
Primer 2
if ($sec=="1") {
include("sec1.inc.php");
}
elseif ($sec=="2") {
include("sec2.inc.php");
}
elseif ($sec=="3") {
include("sec3.inc.php");
}
elseif ($sec=="4") {
include("sec4.inc.php");
}
Primer 3a
 <html>
<head></head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Enter number of rows <input name="rows" type="text" size="4"> and columns
<input name="columns" type="text" size="4"> <input type="submit"
name="submit" value="Draw Table">
</form>
Primer 3b
 <?php
if (isset($_POST['submit'])) {
echo "<table width = 90% border = '1' cellspacing = '5' cellpadding = '0'>";
// set variables from form input
$rows = $_POST['rows'];
$columns = $_POST['columns'];
// loop to create rows
for ($r = 1; $r <= $rows; $r++) {
echo "<tr>";
// loop to create columns
for ($c = 1; $c <= $columns;$c++) {
echo "<td>&nbsp;</td> ";
} echo "</tr> ";
}
echo "</table> ";
}
?>

You might also like