IPW Courses
IPW Courses
IPW Courses
$_COOKIE $_SESSION
$_SESSION
classes & objects $product-
>description=$_POST['prod
accessing properties& uctDescription'];
methods $id=R::store($product);
$objectName- find and print objects
>propertyName; $products=R::findall("product"
$objectName- );
>methodName(); <?php
ClassName::staticFunction() foreach ($products as $p){
ClassName::constantValue; ?>
$objectName::constantValue; <tr>
:: can be used to access <td><?=$p->name ?></td>
protected values <td><?=$p->price ?></td>
Php ORM <td><?=$p->description ?
ORM= object relational ></td>
mapping </tr>
allows you to work with <?php } ?>
databases using object-
oriented code
example with:
https://redbeanphp.com/
using redbean
require 'rb-mysql.php';
R::setup( 'mysql:host=localho • Client-side Scripting
st;dbname=dbname', Languages
‘user', ‘pass' ); • Introduction to Javascript
add object to database • Plan of the course
$product=R::dispense("produ • Javascript – Why, When,
ct"); What
$product- • How to include Javascript in
>name=$_POST['productNa HTML
me']; • Javascript syntax
$product- • Document Object Model
>price=$_POST['productPri • Examples
ce'];
• Javascript – Why, When,
What
• At first we had only simple • If the script is included in
text + links + images pages the body page
• “live” pages were required – The script is executed
by the market when the markup is
• Netscape invented found
“LiveScript” in 1995 • Javascript Syntax
• Later renamed to Javascript • Very similar with C & Java
• Javascript – run on • No types for variables
browsers, access – Declare variables using
dynamically to the html the term let
page – let x=5, y=7;
• The language was (is) • Functions are declared
interpreted usually in using the function keyword
different way by the – function sum(x,y)
browsers – { let rez=x+y; return
• Include Javascript into rez;}
HTML • Calling functions is the
• script included in the html same as in C/Java
page – let suma=sum(2,5);
– <script • Javascript objects
type=”text/javascript”> • Objects have
– //cod script – methods (functions)
– </script> – Properties
• Script included in an • Example
external file – let student={nume:
– <script "ion", an:2, note:
src=”fisier_sursa.js”></ {mate:9, pc:8}};
script> – alert(student.nume
+"<br>" );
• When does the code run – alert(student.an
• If the script tag is included +"<br>");
in the <head> tag – let nota
– The script is executed – let student={nume:
when the page is "ion", an:2, note:
loaded – before {mate:9, pc:8}};
actually rendering it • Javascript predefined
objects
• Math //!!! length is a property and not
– http:// a method
www.w3schools.com/ alert(y.substr(0,5));
jsref/ //compute and display as an
jsref_obj_math.asp alert the substring of y
• String between the first and the 5th
– http:// character
www.w3schools.com/ alert(x+" "+y.length+“
jsref/ “+y.substr(5,y.length));
jsref_obj_string.asp //display as an alert the string
• Array formed by x, length of y and
– http:// the substring formed from
www.w3schools.com/ the 5th character of y until
jsref/ the last one
jsref_obj_array.asp }
• Date </script>
– http://
www.w3schools.com/ • Javascript example -
jsref/jsref_obj_date.asp followup
• Examples • String concatenation
<script type="text/javascript"> operator “+”
function printValue() • The alert is used for
//declare a function displaying information
{ during development. NOT to
let x=Math.random()*10; be used in applications
//compute the value of x as • Objects can have methods
a random value between 0 like y.substr(0,5) and
and 10 properties like y.length
alert(x); //display an alert • All types of variables are
containing the value of x declared using var
var y="a random text";
//create a variable of type • Events
string • HTML elements can detect
alert(y.length); //display an when the user interacts with
alert containing the length of them
y • Examples of interactions
– Mouse over (mouse – Html elements
out) – Text
– Click • The tree elements can be
– Key pressed accessed
– Blur – By traversing the tree
– change (See Data structures
• We can add javascript code course)
to handle every interaction – By accessing them
directly by name
• Events examples (getElementsByTagNa
<script type=“text/javascript”> me)
function youClicked(element) – By accessing them
{this.innerHTML="you clicked directly by id
this element";} (getElementById)
function youMousedOver() – Addressing them
directly (as in an array)
{alert("mouse over detected"); • The root of the DOM tree is
} the document
</script>
<h1 • Methods and properties
onclick="alert('youclicked');y • document.write(“text”)
ouClicked(this);" – Adds the “text” to the
onmouseover="youMoused given page
Over()"> Introduction dans – If the page is finished
la programmation web</h1> loading it rewrites it
– Example
• Javascript Events <script
• Why we need and what is type="text/javascript">
the “this” pointer function printValue()
• DOM {
• DOM=Document Object let
Model x=Math.random()*
• DOM = a way to access the 10;
elements of a HTML page alert(x);
• DOM let y="a random text";
• The DOM tree contains alert(y.length);
nodes which can be alert(y.substr(0,5));
alert(x+" – The most used method
"+y.length+"!!!"+y.s to access an element
ubstr(5,y.length)); of a html page
document.write(x+" – We have to be careful
"+y.length+"!!!"+ to set ids for the
y.substr(5,y.lengt elements
h)); function modifyFirstH1()
} {
</script> //retrieve the element with
• DOM Methods and the id h1id1
properties let
• getElementsByTagName h1Element=document.
– Returns an array of getElementById("h1id1
elements with a given ");
name //set the HTML value for
– The we need to know the document
the position of the h1Element.innerHTML="n
element we need to ew title";
modify inside the array }
function • DOM objects methods and
modifySecondH1() properties
{ • Direct access to the
let element
headersArray=docume • Predefined collections
nt.getElementsByTagN – Forms
ame("h1"); //retrieves – Links
all the elements whose – Images
names are h1 • document.forms[0].usernam
//elements’ numbering in e.value – accesses the first
the array starts at 0 form in the document and
sets the value property for
headersArray[1].innerH the username input
TML=Math.random()*5; • Example – using javascript
} to validate forms
• DOM Methods and • When a form is submitted
properties we need to validate it first
• document.getElementById on the client-side
• The form should be {alert("please make sure the
validated before submitting username is longer than 4
• The event should be added letters");return; }
to the submit button document.forms[0].submit();
• For example we want to }
check if 2 passwords have • Example of form validation
the same value and if the with Javascript (II)
username is longer than 4
characters <form action="script.php"
• Validate forms with method="POST">
Javascript – example (I) nom d'utilisateur<input
function validateForm(){ type="text"
let id="username" /><br/>
usernameElement=docume mot de passe<input
nt.getElementById("userna type="password"
me"); id="password" /> <br/>
let mot de passe encore une
passwordElement=docume fois <input id="repassword"
nt.getElementById("passwor type="password"> <br/>
d"); <input type="button"
let value="send"
rePasswordElement=docum onclick="validateForm();"/>
ent.getElementById("repass </form>
word"); • Javascript debugging
if(passwordElement.value! • developer console in all
=rePasswordElement.value major browsers
|| • Javascript debugging
passwordElement.value.len example
gth==0)
{alert("please make sure the
password is entered the
same twice");return;}
if
(usernameElement.value.le
ngth<4)
• arrow function
• standard definition of functions
• let sum = function(a, b)
{ return a +b;}
• arrow functions
• let sum = (a, b) => a+b
• arrow functions can be used in
the following ways
• forEach
• https://developer.mozilla.org/
en-US/docs/Web/JavaScript/
Reference/Global_Objects/
Array/forEach
• JS • The forEach() method is
an iterative method. It calls a
• template literals provided callbackFn function
• a way to add variables in strings once for each element in an
• strings delimited by the backtick array in ascending-index order.
`
• let firstName = "John"; • foreach with arrow function
let lastName = "Doe";
• fetch/promise
let text = `Welcome $ • fetch – makes a request to a
{firstName}, $ web server
{lastName}!`;
• fetch(file)
.then(x => x.text())
.then(y => f(y))
• callback
• a function is sent as a • promise
parameter to another function • fetch uses the concept of
• similar with pointers to promise
functions in C/C++
• producing code – code that can promise q, p will adopt the
take time state of q.
• consuming code – code that • dom functions – createElement
uses the result of the producing • createElement – creates a DOM
code element
• promise is an object that links
producing code and consuming • dom functions - appendChild
code • adds a new node to an existing
element