Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information
Transcript of Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information
![Page 1: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/1.jpg)
Client/Server
![Page 2: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/2.jpg)
Submitting forms (client-side)
![Page 3: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/3.jpg)
Submitting forms (client-side)
![Page 4: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/4.jpg)
Submitting forms (client-side)
![Page 5: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/5.jpg)
submit.php
<?php $len = strlen($_POST["password"]); $name = $_POST["name"];
print "Welcome " . $name; print ", your password has " . $len . " characters";?>
![Page 6: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/6.jpg)
submit.php
<?php $len = strlen($_POST["password"]); $name = $_POST["name"];
print "Welcome " . $name; print ", your password has " . $len . " characters";?>
this is php code
![Page 7: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/7.jpg)
submit.php
<?php $len = strlen($_POST["password"]); $name = $_POST["name"];
print "Welcome " . $name; print ", your password has " . $len . " characters";?>
variables in php are prefixed with $
![Page 8: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/8.jpg)
submit.php
<?php $len = strlen($_POST["password"]); $name = $_POST["name"];
print "Welcome " . $name; print ", your password has " . $len . " characters";?>
POST is a variable containing client form data
![Page 9: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/9.jpg)
submit.php
<?php $len = strlen($_POST["password"]); $name = $_POST["name"];
print "Welcome " . $name; print ", your password has " . $len . " characters";?>
POST is a variable containing client form data
![Page 10: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/10.jpg)
submit.php
<?php $len = strlen($_POST["password"]); $name = $_POST["name"];
print "Welcome " . $name; print ", your password has " . $len . " characters";?>
send data back to client by printing
![Page 11: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/11.jpg)
submit.php
<?php $len = strlen($_POST["password"]); $name = $_POST["name"];
print "Welcome " . $name; print ", your password has " . $len . " characters";?>
send data back to client by printing
![Page 12: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/12.jpg)
submit.php
<?php $len = strlen($_POST["password"]); $name = $_POST["name"];
print "Welcome " . $name; print ", your password has " . $len . " characters";?>
commands end with semi-colon
![Page 13: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/13.jpg)
extract client information
<?php $agent = $_SERVER["HTTP_USER_AGENT"]; $ip = $_SERVER["HTTP_CLIENT_IP"]; $referrer = $_SERVER["HTTP_REFERER"]; $time = $_SERVER["REQUEST_TIME"];?>
![Page 14: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/14.jpg)
extract client information
<?php $agent = $_SERVER["HTTP_USER_AGENT"]; $ip = $_SERVER["HTTP_CLIENT_IP"]; $referrer = $_SERVER["HTTP_REFERER"]; $time = $_SERVER["REQUEST_TIME"];?>
![Page 15: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/15.jpg)
extract client information
<?php $agent = $_SERVER["HTTP_USER_AGENT"]; $ip = $_SERVER["HTTP_CLIENT_IP"]; $referrer = $_SERVER["HTTP_REFERER"]; $time = $_SERVER["REQUEST_TIME"];?>
browser
![Page 16: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/16.jpg)
extract client information
<?php $agent = $_SERVER["HTTP_USER_AGENT"]; $ip = $_SERVER["HTTP_CLIENT_IP"]; $referrer = $_SERVER["HTTP_REFERER"]; $time = $_SERVER["REQUEST_TIME"];?>
IP address
![Page 17: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/17.jpg)
extract client information
<?php $agent = $_SERVER["HTTP_USER_AGENT"]; $ip = $_SERVER["HTTP_CLIENT_IP"]; $referrer = $_SERVER["HTTP_REFERER"]; $time = $_SERVER["REQUEST_TIME"];?>
which page referred you to the current page (can be manipulated or may be blank)
![Page 18: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/18.jpg)
extract client information
<?php $agent = $_SERVER["HTTP_USER_AGENT"]; $ip = $_SERVER["HTTP_CLIENT_IP"]; $referrer = $_SERVER["HTTP_REFERER"]; $time = $_SERVER["REQUEST_TIME"];?>
date/time
![Page 19: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/19.jpg)
cookies
<?php setcookie( "<name>", "<value>", <expiration date> );?>
![Page 20: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/20.jpg)
set cookie
<?php setcookie( "DSA", $name, time()+(30*24*60*60) );?>
![Page 21: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/21.jpg)
<?php setcookie( "DSA", $name, time()+(30*24*60*60) );?>
number of seconds since January 1 1970 00:00:00 GMT
set cookie
![Page 22: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/22.jpg)
<?php setcookie( "DSA", $name, time()+(30*24*60*60) );?>
30 days * 24 hrs/day * 60 min/hr * 60 sec/min
set cookie
![Page 23: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/23.jpg)
<?php $cookie = $_COOKIE["DSA"];?>
read cookie
![Page 24: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/24.jpg)
<?php $cookie = $_COOKIE["DSA"];?>
read cookie
![Page 25: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/25.jpg)
delete cookie
<?php setcookie( "DSA", $username, time()-1 );?>
![Page 26: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/26.jpg)
example
http://www.cs.dartmouth.edu/~dsa/exercise/cookie.php
preferences -> show advanced settings -> content settings -> all cookies and site data
![Page 27: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/27.jpg)
php in html
but…
<?php setcookie( "DSA", $username, time()-1 );?>
<html> <head> </head> <body> <p> some html code </p> </body></html>
![Page 28: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/28.jpg)
sessions are an alternative to cookies
data is stored on the server, not the client
![Page 29: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/29.jpg)
PHP
PHP, separate from HTML/Javascript, is a full-blown programming language
![Page 30: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/30.jpg)
Variables
var x = 42;var y = 8;var z = x + y;print( "x + y = " + z );
Javascript
![Page 31: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/31.jpg)
Variables
var x = 42;var y = 8;var z = x + y;print( "x + y = " + z );
Javascript
<?php $x = 42; $y = 8; $z = $x + $y; print( "$x + $y = $z\n" );?>
PHP
![Page 32: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/32.jpg)
Variables
var x = 42;var y = 8;var z = x + y;print( "x + y = " + z );
Javascript
<?php $x = 42; $y = 8; $z = $x + $y; print( "$x + $y = $z\n" );?>
PHP
![Page 33: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/33.jpg)
Variables
var x = 42;var y = 8;var z = x + y;print( "x + y = " + z );
Javascript
<?php $x = 42; $y = 8; $z = $x + $y; print( "$x + $y = $z\n" );?>
PHP
![Page 34: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/34.jpg)
Variables
var x = 42;var y = 8;var z = x + y;print( "x + y = " + z );
Javascript
<?php $x = 42; $y = 8; $z = $x + $y; print( "$x + $y = $z\n" );?>
PHP
![Page 35: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/35.jpg)
Boolean
<?php $x = True; $y = False;?>
![Page 36: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/36.jpg)
Constants
<?php $x = 42; $x = $x + 1; print( "$x" );?>
![Page 37: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/37.jpg)
Constants
<?php define("Y",1); $x = 42; $x = $x + Y; print( "$x" );?>
![Page 38: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/38.jpg)
Constants
<?php define("Y",1); print( "Y" );?>
output: Y
![Page 39: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/39.jpg)
Constants
<?php define("Y",1); print( constant("Y") );?>
output: 1
![Page 40: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/40.jpg)
Constants
<?php define("Y",1); define("Y",2); print( constant("Y") );?>
![Page 41: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/41.jpg)
Constants
<?php define("Y",1); define("Y",2); print( constant("Y") );?>
output: 1
![Page 42: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/42.jpg)
Constants
<?php define("Y",1); define("Y",2); print( constant("Y") );?>
constants cannot (and should not) be redefined
this makes global variables (a bit) safer
![Page 43: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/43.jpg)
Operators
<?php $x = 42; $y = 8; $z = $x + $y; // addition $z = $x * $y; // multiplication $z = $x - $y; // subtraction $z = $x / $y; // division $z = $x % $y; // modular division $x++;// increment by one $x--; // decrement by one?>
![Page 44: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/44.jpg)
Operators
<?php $x = 42; $y = 8; $z = $x == $y; // equality $z = $x != $y; // not equality $z = $x > $y; // greater than $z = $x >= $y; // greater than or equal to $z = $x % $y; // modular division?>
![Page 45: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/45.jpg)
Conditionals
<?php $x = 42; if( $x % 2 == 0 ) { print( "$x is even\n" ); } else { print( "$x is odd\n" ); }?>
PHP’s == vs. Javascript’s ===
![Page 46: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/46.jpg)
Exercise
write the client (html) and server-side (php) code that creates the following webpage with two text inputs and one submit
button. After submitting, your server-side code should return the sum of the two numbers.
NOTE: $x = (int)$x; converts string to integer
www.c9.io
![Page 47: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/47.jpg)
<html> <head> <title> addition </title> </head> <body> <form action="addition.php" method="POST"> <input type="text" name="x"> + <input type="text" name=“y">
<br><br>
<input type="submit"> </form> </body></html>
![Page 48: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/48.jpg)
<html> <head> <title> addition </title> </head> <body> <form action="addition.php" method="POST"> <input type="text" name="x"> + <input type="text" name="y">
<br><br>
<input type="submit"> </form> </body></html>
![Page 49: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/49.jpg)
<html> <head> <title> addition </title> </head> <body> <form action="addition.php" method="POST"> <input type="text" name="x"> + <input type="text" name="y">
<br><br>
<input type="submit"> </form> </body></html>
![Page 50: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/50.jpg)
<html> <head> <title> addition </title> </head> <body> <form action="addition.php" method="POST"> <input type="text" name="x"> + <input type="text" name="y">
<br><br>
<input type="submit"> </form> </body></html>
![Page 51: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/51.jpg)
<html> <head> <title> addition </title> </head> <body> <form action="addition.php" method="POST"> <input type="text" name="x"> + <input type="text" name="y">
<br><br>
<input type="submit"> </form> </body></html>
![Page 52: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/52.jpg)
<html> <head> <title> addition </title> </head> <body> <form action="addition.php" method="POST"> <input type="text" name="x"> + <input type="text" name="y">
<br><br>
<input type="submit"> </form> </body></html>
<?php
$x = $_POST["x"]; $y = $_POST["y"]; $z = (int)$x + (int)$y;
print("$x + $y = $z");
?>
![Page 53: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/53.jpg)
<html> <head> <title> addition </title> </head> <body> <form action="addition.php" method="POST"> <input type="text" name="x"> + <input type="text" name="y">
<br><br>
<input type="submit"> </form> </body></html>
<?php
$x = $_POST["x"]; $y = $_POST["y"]; $z = (int)$x + (int)$y;
print("$x + $y = $z");
?>
![Page 54: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/54.jpg)
<html> <head> <title> addition </title> </head> <body> <form action="addition.php" method="POST"> <input type="text" name="x"> + <input type="text" name="y">
<br><br>
<input type="submit"> </form> </body></html>
<?php
$x = $_POST["x"]; $y = $_POST["y"]; $z = (int)$x + (int)$y;
print("$x + $y = $z");
?>
![Page 55: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/55.jpg)
<html> <head> <title> addition </title> </head> <body> <form action="addition.php" method="POST"> <input type="text" name="x"> + <input type="text" name="y">
<br><br>
<input type="submit"> </form> </body></html>
<?php
$x = $_POST["x"]; $y = $_POST["y"]; $z = (int)$x + (int)$y;
print("$x + $y = $z");
?>
![Page 56: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/56.jpg)
![Page 57: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/57.jpg)
2 5 7
update, not replace, current HTML
![Page 58: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/58.jpg)
<html> <body> <form id='additionForm'> <input type="text" name="x"> + <input type="text" name="y"> = <input type="text" name="answer" id="answer"> <br><br> <input type="submit"> </form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <script> $(document).ready(function(){ $('#additionForm').submit(function(){ $.ajax({ type: 'POST', url: 'additionAjax.php', data: $(this).serialize() }) .done(function(data){ $('#answer').val(data); // show the response }) .fail(function() { alert( "Posting failed." ); // just in case posting your form failed }); return false; // to prevent refreshing the whole page page }); }); </script> </body></html>
![Page 59: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/59.jpg)
<html> <body> <form id='additionForm'> <input type="text" name="x"> + <input type="text" name="y"> = <input type="text" name="answer" id="answer"> <br><br> <input type="submit"> </form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <script> $(document).ready(function(){ $('#additionForm').submit(function(){ $.ajax({ type: 'POST', url: 'additionAjax.php', data: $(this).serialize() }) .done(function(data){ $('#answer').val(data); // show the response }) .fail(function() { alert( "Posting failed." ); // just in case posting your form failed }); return false; // to prevent refreshing the whole page page }); }); </script> </body></html>
![Page 60: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/60.jpg)
no action or method!<html> <body> <form id='additionForm'> <input type="text" name="x"> + <input type="text" name="y"> = <input type="text" name="answer" id="answer"> <br><br> <input type="submit"> </form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <script> $(document).ready(function(){ $('#additionForm').submit(function(){ $.ajax({ type: 'POST', url: 'additionAjax.php', data: $(this).serialize() }) .done(function(data){ $('#answer').val(data); // show the response }) .fail(function() { alert( "Posting failed." ); // just in case posting your form failed }); return false; // to prevent refreshing the whole page page }); }); </script> </body></html>
![Page 61: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/61.jpg)
<html> <body> <form id='additionForm'> <input type="text" name="x"> + <input type="text" name="y"> = <input type="text" name="answer" id="answer"> <br><br> <input type="submit"> </form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <script> $(document).ready(function(){ $('#additionForm').submit(function(){ $.ajax({ type: 'POST', url: 'additionAjax.php', data: $(this).serialize() }) .done(function(data){ $('#answer').val(data); // show the response }) .fail(function() { alert( "Posting failed." ); // just in case posting your form failed }); return false; // to prevent refreshing the whole page page }); }); </script> </body></html>
![Page 62: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/62.jpg)
<html> <body> <form id='additionForm'> <input type="text" name="x"> + <input type="text" name="y"> = <input type="text" name="answer" id="answer"> <br><br> <input type="submit"> </form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <script> $(document).ready(function(){ $('#additionForm').submit(function(){ $.ajax({ type: 'POST', url: 'additionAjax.php', data: $(this).serialize() }) .done(function(data){ $('#answer').val(data); // show the response }) .fail(function() { alert( "Posting failed." ); // just in case posting your form failed }); return false; // to prevent refreshing the whole page page }); }); </script> </body></html>
![Page 63: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/63.jpg)
<html> <body> <form id='additionForm'> <input type="text" name="x"> + <input type="text" name="y"> = <input type="text" name="answer" id="answer"> <br><br> <input type="submit"> </form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <script> $(document).ready(function(){ $('#additionForm').submit(function(){ $.ajax({ type: 'POST', url: 'additionAjax.php', data: $(this).serialize() }) .done(function(data){ $('#answer').val(data); // show the response }) .fail(function() { alert( "Posting failed." ); // just in case posting your form failed }); return false; // to prevent refreshing the whole page page }); }); </script> </body></html>
![Page 64: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/64.jpg)
<html> <body> <form id='additionForm'> <input type="text" name="x"> + <input type="text" name="y"> = <input type="text" name="answer" id="answer"> <br><br> <input type="submit"> </form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <script> $(document).ready(function(){ $('#additionForm').submit(function(){ $.ajax({ type: 'POST', url: 'additionAjax.php', data: $(this).serialize() }) .done(function(data){ $('#answer').val(data); // show the response }) .fail(function() { alert( "Posting failed." ); // just in case posting your form failed }); return false; // to prevent refreshing the whole page page }); }); </script> </body></html>
![Page 65: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/65.jpg)
<html> <body> <form id='additionForm'> <input type="text" name="x"> + <input type="text" name="y"> = <input type="text" name="answer" id="answer"> <br><br> <input type="submit"> </form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <script> $(document).ready(function(){ $('#additionForm').submit(function(){ $.ajax({ type: 'POST', url: 'additionAjax.php', data: $(this).serialize() }) .done(function(data){ $('#answer').val(data); // show the response }) .fail(function() { alert( "Posting failed." ); // just in case posting your form failed }); return false; // to prevent refreshing the whole page page }); }); </script> </body></html>
![Page 66: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/66.jpg)
Loops
for( initialization; condition; increment ) { code to be executed;}
same as Javascript
![Page 67: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/67.jpg)
Loops
for( $i = 0; $i <= 10; $i++ ) { print( "$i" );}
output?
![Page 68: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/68.jpg)
Loops
for( $i = 0; $i <= 10; $i++ ) { print( "$i" );}
012345678910
![Page 69: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/69.jpg)
Loops
for( $i = 0; $i <= 10; $i++ ) { print( "$i\n" );}
0 1 2 3
![Page 70: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/70.jpg)
Exercise
write some php code that multiplies two numbers the old-fashioned way… the product of 5 and 8 is:
8 + 8 + 8 + 8 + 8 = 40
your code should define two variables to be multiplied and after computing the product print out the product
www.c9.io
NOTE: run php multiply.php
![Page 71: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/71.jpg)
<?php $x = 5; $y = 8; $prod = 0; for( $i = 1; $i <= $x; $i++ ) { $prod = $prod + $y; } print( "$prod" );?>
![Page 72: Client/Server - cs.dartmouth.edudsa/lecture02/lecture02.pdf · extract client information](https://reader033.fdocuments.in/reader033/viewer/2022050716/5e3d6c409999ad757864258f/html5/thumbnails/72.jpg)
<?php $x = 5; $y = 8; $prod = 0; for( $i = 1; $i <= $x; $i++ ) { $prod = $prod + $y; } print( "$prod" );?>
always think about edge cases