ACM Web Development Workshop

advertisement
ACM Web Development Workshop PHP
By Luis Torres
DON’T FORGET TO INSTALL “WAMP or
MAMP SERVER” ON YOUR COMPUTER
google/yahoo/bing it 

Running wamp:
Click Start the services!
Localhost will open
whatever is named
index.html/index.php in
the www directory
This folder will contain
our website files, if you
want to run a php file
it has to be INSIDE of
this folder
This is how the www folder will look
like.(more or less)
Open “index.php”, erase all its content and
replace with the following
BE CAREFUL:
This is the concatenation character in PHP.
In java is the “+” character. Don’t confuse them!
Different approaches to code in php
Lets do functions!
Our function doMath will take
two inputs, and compute its sum.
Since we are not declaring
types, we ASSUME that
the inputs will be
integers.
Inside the function we could
check the types if we
wanted to make sure that
everything is ok.
OMG PHP is easy :D
Well.. Lets continue to the other stuff
PHP $_GET


Purpose: Sometimes we want the same page to have a
function that can do multiple outputs based on the input.
Ex: google search will work the same way, but will yield
different outcomes.
PHP $_GET


How does it works?
In the google example, they use the URL, to pass variables
that will be used in that page.
Wait… What?







Lets go back to the doMath function.
For the purpose of teaching, lets assume you have this
URL: (you hard code the rest of the url)
Lets break it down:
localhost/yourFile.php <- this is your file
? Question mark shows where variables begin
variableA <- the variable “varaibleA” has a value of 2
& <- this character concatenates multiple variables
variableB <- the variable “varaibleB” has a value of 3
Notice how you get the values
using get.
GET will always get values
from the URL
But without the URL it wont work :’(

Here is how to fix that, just check if the $_GET “isset()”:
<html>
<body>
<?php if(isset($_GET['variableA']))
{
$a=$_GET['variableA'];
}
else
{
echo "no input in URL, using variableA as 2<br>";
$a=2;
}
if(isset($_GET['variableB']))
{
$b=$_GET['variableB'];
}
else
{
echo "no input in URL, using variableB as 3<br>";
$b=3;
}
echo doMath($a,$b);
?>
</body>
<?php
function doMath($a,$b)
{
return $a+$b;
}
Now it works for both 
But, how can I change the values being
passed without hardcoding? 

We have Forms
<html>
<body>
<form action=“yourFile.php" method="get">
variable A: <input type="text" name="variableA" />
variable B: <input type="text" name="variableB" />
<input type="submit" />
</form>
<?php
Rest of the code from previous example…
?>
How does it work?
<html>
<body>
<form action=“yourFile.php" method="get">
variable A: <input type="text" name="variableA" />
variable B: <input type="text" name="variableB" />
<input type="submit" />
</form>
<?php
Will be obtained in
yourFile.php using
$_GET[‘variableName’]
Rest of the code from previous example…
?>
Input type submit is a button, after clicking the button it will go to the
specified file, with the specified values and execute.
What if I want to be mysterious and not
show anything in the URL?


You can use the POST function
Similar to $_GET[‘variable’], you use $_POST[‘variable’]
<form action=“yourFile.php" method=“post">
variable A: <input type="text" name="variableA" />
variable B: <input type="text" name="variableB" />
<input type="submit" />
…
…
<?php
if(isset($_POST['variableA']))
{
$a=$_POST['variableA'];
}
…
….
You get the idea, it will work, and wont show the variables in
the URL
Are they different?
GET
-variables shown at all times…
horrible if you use get to pass a
password
POST
-variables hidden… you want to use
this when submitting a password
-since it is shown in the URL, you can
bookmark a specific location on your
browser. Cool now you can send a
specific query of a website to a
friend.
-cant be bookmarked, useful if you
don’t want users to return to a
specific part of a site…like
submitting the payment. Since post
variables don’t stay there, when the
code tries to execute it will fail
because of the missing variables.
-URLs have a size limit!
2048 characters
-don’t have a limit, and they are
encrypted
PHP Include


What if you have a multiple functions, and you don’t want
to copy paste them on all your files.
Really simple:
PHP Include


WONT WORK if you put the include after you method
call.
Make sure all your includes are at the top of your code if
possible.
Ermm we got this far..?

We are missing:
forms with PHP (dynamic forms)
Sessions
SQL/databases
Javascript
Ajax

So basically another workshop  or 2..or more.





Download