Java Script and PHP
Java Script and PHP
<script type=“text/javascript”>
document.write(“Hello World!”)
</script>
• But there are following most preferred ways to include JavaScript in your
HTML file.
• The interpreter checks each case against the value of the expression until
a match is found.
• If some of the data that had been entered by the client had been in the
wrong form or was simply missing, the server would have to send all
the data back to the client and request that the form be resubmitted with
correct information.
<script type="text/javascript">
function validateEmail()
{
var emailID = document.myForm.EMail.value;
atpos = emailID.indexOf("@");
return( true );
}
</script>
functions
• Platform independent
Prepared By Mebiratu B. 22-Feb-10 36
Basics of PHP
• PHP files end with .php
• PHP code is contained within tags
<?php ?> or
• HTML script tags: (This syntax is removed after PHP 7.0.0)
<script language="php"> </script>
• Comments
// for single line
/* */ for multiline
$a = “Arjun Bala”;
echo($a);
Prepared By Mebiratu B. 22-Feb-10 39
Variables (Cont.)
• Variable names inside strings replaced by their value
Example : $name = “Arjun Bala”;
$str = “My name is $name”;
• Type conversions
• settype function
• Type casting
• Concatenation operator
• . (period)
• Mostly script variables are available to you anywhere within your script.
• Note that variables inside functions are local to that function and a function
cannot access script variables which are outside the function even if they are
respectively
Prepared By Mebiratu B. 22-Feb-10 42
Example (Variables)
5 <!-- Demonstration of PHP data types -->
6
7 <html xmlns = "http://www.w3.org/1999/xhtml">
8 <head>
9 <title>PHP data types</title>
10 </head>
11
12 <body>
13 Assign a string to variable
14 <?php
$testString
15
16 // declare a string, double and integer
17 $testString = "3.5 seconds";
18 $testDouble = 79.2; Assign a double to variable
19 $testInteger = 12; Assign an integer to variable
$testDouble
20 ?> $testInteger
21
• Array can be sorted using this command, which will order them from the lowest to
highest
• If there is a set of string stored in the array they will be sorted alphabetically.
• The type of sort applied can be chosen with the second optional parameter $sort_flags
which can be
• rsort($array [, $sort_flags])
•Prepared
It will sort array
By Mebiratu B. in reverse order (i.e. from highest to lowest) 22-Feb-10 51
PHP Array Functions (Cont.)
• shuffle($array)
It will mix items in an array randomly.
• array_merge($array1,$array2)
It will merge two arrays.
• array_slice($array,$offset,$length)
returns the sequence of elements from the array $array as specified by
the $offset and $length parameters.
<?php
function functionName()
{
//code to be executed;
}
?>
Note: function name can start with a letter or underscore "_", but not a number!
header(“Location: index.php”);
header(“Content-Type: application/pdf”);
• "w+" (Read/Write. Opens and clears the contents of file; or creates a new file
if it doesn't exist)
• "a" (Write only. Opens and writes to the end of the file or creates a new file if
it doesn't exist)
• "a+" (Read/Write. Preserves file content by writing to the end of the file)
Prepared By Mebiratu B. 22-Feb-10 58
File Handling Example
text.txt
Hello World
Read File
<?php
$file = fopen("text.txt","a+");
$text = fread($file,filesize("text.txt"));
echo($text);
?>
Write File
<?php
fwrite($file," New Content");
$text = fread($file,filesize("text.txt"));
echo($text);
?>
Prepared By Mebiratu B. 22-Feb-10 59
Cookies in PHP
• HTTP cookies are data which a server-side script sends to a web client to
keep for a period of time.
• On every subsequent HTTP request, the web client automatically sends the
cookies back to server (unless the cookie support is turned off).
• The cookies are embedded in the HTTP header (and therefore not visible to
the users).
• Shortcomings/disadvantages of using cookies to keep data
• User may turn off cookies support.
• Users using the same browser share the cookies.
• Limited number of cookies (20) per server/domain and limited size (4k bytes)
per cookie
• Client can temper with cookies
<?php
$connect = mysql_connect(“localhost”,”root”,””);
?>
while($row = mysql_fetch_row($result))
{
echo($row[1] . “<br/>”);
}
OR
while($row = mysql_fetch_assoc($result))
{
echo($row[‘name’] . “<br/>”);
}
OR
while($row = mysql_fetch_array($result))
{
echo($row[0] . “ ”. $row[‘name’] . “<br/>”);
}
?> Prepared By Mebiratu B. 22-Feb-10 79
Thank You!!!!