Monday, February 28, 2011

Create-new-function-PHP (Part 4)

Assalamu'alaikum and selamat sejahtera

For this post,we will learn on how to create a new function that can detect the space or blank space from application.Well,we already learn the trim() but for this function,we want to display some error message if the user did not put anything but still press the submit button.Here is the example:


<?PHP
$user_input = trim("Ali Abu ");
detect_blank($user_input);
function detect_blank($user_input) {
if ($user_input == "") {
print "Sorry,you forget to fill the textbox";
}
else {
print "The form is OK";
}
}

?>

ok,you run it..it print "The form is OK" ryte?so how about change this line,just assuming that $user_input is get the value from the form/application that fill by the user.So change this line:


$user_input = trim("Ali Abu ");
to this:


$user_input = trim("");
so,it print "Sorry,you forget to fill the textbox" ryte?

that's all for today!adios!

N/NOTE:new function for this hectic morning!

Sunday, February 27, 2011

Create-new-function-PHP (Part 3)

Assalamu'alaikum and selamat sejahtera

For this post,we will learn on how to past the variable that declared that outside the function to the our function,than that function will process it and come out with output.Here is the example:


<?PHP
 
$display_text = "Variable message";
display_message($display_text);

function display_message($display_text) {
print $display_text;
}


?>

or how about we apply some mathmatic:


<?PHP
$number = 2;
display_math($number);

function display_math($number) 
{
$number=$number+4;
print $number;
}

?>

it print 2 or 4 or 6?so,i leave that answer to you. How about you just try to create a function that can accept multiple variable.Here is the example of function that can accept more than one variable::

function display_subtraction($number1,$number2)

try it!




N/NOTE:any problem?just comment here ^^

Create-new-function-PHP (part 2)

Assalamu'alaikum and selamat sejahtera

For this post we will try some function to display the variable.Ok,here is the example:

<?PHP
$display_text = "variable Detetceted";
display_message( );
function display_message( ) {
print $display_text;
}

?>

Run it,it print error ryte?so we call it and already declared the variable "$display_text" and yet we still can not display that variable in the function.So,as the conclusion,the function must have its variable itself ,then it can display its variable.what i meant is like this:

<?PHP
display_message( );
function display_message( ) {
$display_text = "display message";
print $display_text;
}

?>

that all....
N/NOTE:how about we declare a variable inside the function.....then directly display that variable,i meant display it without calling the function..try it!taste it! 
 

Saturday, February 26, 2011

Introduction to String function PHP(part 2)

Assalamu'alaikum and selamat sejahtera

Here is the example of the string function in PHP.

str_replace()

$text = "The string function";
$THEwordTHATyouWANTtoCHANGEit = "string";
$REPLACEthatWORDwithTHIS = "strong";

print $text . "<BR>";
$text = str_replace($THEwordTHATyouWANTtoCHANGEit,
$REPLACEthatWORDwithTHIS
, $text);
print $text;

str_word_count()

$number_words = str_word_count("how many word?");
print $number_words;

strlen()

$length_of_string = strlen("This is how to count the length of string");

-note:it count the character and the space in the string.

substr()

$username = "my name is dotcom";
$check = substr($username, strlen($username) - 6);

if ($check == "dotcom" ) {
print "ends in dotcom";
}
else {
print "doesn't end in dotcom";

-note:strlen($username)-6 :PHP will take six character because we put(-6), if -4 then it take 4 character that behind this string.So,our string is this :my name is dotcom,then PHP will start to count at red,bold 'm' as one until six.So sixth charatcer is:d . Then,$check is equal to 'dotcom'.


N/NOTE:selamat mencuba!!!

 

Create-new-function-PHP

Assalamu'alaikum and selamat sejahtera

On this post,we will learn the new function.So,to create new function we just type "function" with name that we desire like this :

function name_that_you_desire(){

print "this is my function!";

}

so,take this code or make your own function and put it inside PHP tag:
<?PHP
function name_that_you_desire(){
print "this is my function!";

}
?>

then run it...nothing happen ryte?its just like the other function..that we need to call it.In this case,we call it:

name_that_you_desire();


<?PHP
name_that_you_desire();

function name_that_you_desire(){

print "this is my function!";

}

?>

or


<?PHP
function name_that_you_desire(){
print "this is my function!";

}
name_that_you_desire();

?>

N/NOTE:custom function...

Introduction to String function PHP(part 1)

Assalamu'alaikum and selamat sejahtera

Here is the example of String function in PHP
----->TRY IT!<----------

chr()


$email = "nuar" . chr(64) . "Email.com";
print $email;

ord()


$asciiNumber= ord("%");
print $asciiNumber

echo()


$Alternativedisplay = "the other option to display";
print $Alternativedisplay;
echo $Alternativedisplay;

similar_text()

$real_name ="John lennon";
$guess_name = "John lenon";

$check_answer = similar_text($real_name, $guess_name, $percentCorrect);
print($check_answer) . "<BR>";
print($percentCorrect . "% correct");


//tell the accuracy of $guess_name as compare to $real name in percentage 
 
str_repeat()

$extra_alias = str_repeat("@", 5);
print $extra_alias;


click here for more on ASCII number 
 

Friday, February 18, 2011

Escaping technique-PHP

Assalamu'alaikum and selamat sejahtera

Copy this one and run it:

<?PHP
$escape = 'Alex's pencil';
print $escape;

?>

it print error ryte?it is because PHP is confuse because this variable got three single quotes ,So,we need to remove one of  it but we still want to display "Alex's pencil" on the browser.So change this line :
$escape = 'Alex\'s pencil';

no error ryte? it is because,this variable got  this ' \ '.This ' \ ' is called escape(as i understand la..),so it tell the PHP to skip one character only after it.After ' \ ' is single quote ryte?So PHP skip it..so PHP said that variable got two single quote only!

N/NOTE:  $escape = "Alex\'s pencil"; use double quote also can maa,hahaha

How-to-join-text-in-PHP script (part 2)

Assalamu'alaikum and selamat sejahtera

Okey....we already learn the joining thing in last post ryte...if you did not remember,just click here.but in this post,we will do on array..here is the example:


$group = array("Alpha", "Beta", "Charlie", "Delta");


and then we join it using implode():


$group=implode(",", $group);


the concept is same with explode()  that is,it need two thing: the separator and the variable name(but in array).
then we print it:

print $group;

the code:

<?PHP
$group = array("Alpha", "Beta", "Charlie", "Delta");

$group=implode(",", $group);
 print $group;
?>


try this!

$group=implode("-", $group);

Or

$group=implode("#", $group);
 
Or

$group=implode(" ", $group);

N/NOTE:any question guys?

How-to-split-a-text--->PHP

Assalamu'alaikum dan selamat sejahtera

Selamt pagi!morning!ohayou!

So,for this post,we will learn on how to split a text in PHP ,here is the quick example:

$line="Group 1,200,250,150,100,700";

but we want print it to be like this:


Group 1
200
:250
150
100
700

So,here is the code:

<?PHP
$line = "Group 1,200,250,150,100,700";
$line = explode(",",$line);
print $line[0];
?>

okey,let see...we got the new code again..here is:
explode(",",$line);

 so,explode is function to split the variable...so,inside the round bracket,we got a comma and variable name.we know that variable name is used for reference but for comma is to tell the explode function,that in order to split the text,you must read until you find the comma,then the next character will be split.so far okay?
 another explanation...is order to use this function,you must have two thing...one separator,one is variable..so,in this case,we declare $line  like this:


$line = "Group 1  ,  200  ,   250  ,   150   ,   100  ,   700";

 
so,we got the variable and the separator is "," is comma because it separate the word "Group 1" from other number :200,250,150,100,700.so far okey?hahaha

but the browser print "Group 1" ryte?it is because when the variable "$line" is split (using explode() function,the $line will change to an array that why in order to print it,we must put "[ ]".So,to print all,we can use for loops or this method:


print_r($line);

Then change the declaration of $line to be like this:

$line = "Group 1,Jan:1,Feb:2,Mac:3,April:4,Total:5";

and then use print_r().Then try use for loops....test it!is really fun!haha

 N/NOTE:try it!

Thursday, February 17, 2011

How-to-know-what-type-of-browser-the-users-use

Assalamu'alaikum and selamat sejahtera

So,PHP also provide a function that detect the type of the browsers.Well,this function usually use by administrator or some organization,just to analysis what type of browser that mostly used.So,it is just a additional knowledge only.Who know,maybe this function can be very beneficial to us.

The code:

<?PHP
$browser = $_SERVER["HTTP_USER_AGENT"];
print $browser;
?>

N/NOTE:every knowledge....must been something useful to us. 

Wednesday, February 16, 2011

How-TO-search-a-letter-inside-a-string-variable-PHP

Assalamu'alaikum and selamat sejahtera

So,for this very nice morning,we will learn on how to find the position of a letter that inside a string variable.For example,give a string variable:"hi five",and then we want to know the position of 'h'.So,we use strpos(string_to_use, letter_to_find, start).Here is the code:


<?PHP
$slogan = "hi five";
$position = strpos($slogan, "h");
print $position;

?> 

So,start is optional either you want to use it or not.It will print '0' ryte?it is because PHP read the letter 'h' as '0',letter 'i' as '1' ,etc.PHP start with zero when to count.How about we change the letter 'h' to be 'H'...it print nothing ryte?it is because PHP can not find any 'H' in 'hi five'.Logically,'h' is just same with 'H'..lol..it is just different in lower case and upper case.Even a small different can make a different result in PHP.So watch out!Okey so for this case,we can use "===",triple equals,to print the false value(as PHP state that "there is no 'H' in 'hi five' ")  result.Here is the code:

<?PHP
$slogan = "hi five";
$position = strpos(
$slogan,'H');

if ($letter_position === false) {
print "Letter not found " ;
}
else {
print "Letter found";
}


?>


N/NOTE:finding someone??

How-to-shuffle-character-PHP

Assalamu'alaikum and selamat sejahtera

For this post,we learn on how to shuffle the character,here is the code:

<?PHP
$variable_name = 'pencil ';
$variable_name = str_shuffle($variable_name);
print $variable_name;
?>

str_shuffle()-will shuffle the variable value.

N/NOTE:how about change the value of  $variable_name-=12345,if you want to know more la....

Deal(trimming) with white blank-PHP

Assalamu'alaikum and selamat sejahtera

So,for this post,we will learn on how to deal with white blank on the input.Quite confusing right?okey,here is an example of event,sometime we input our name to the system like this " alexander " .So,from this quotation marks,we can see that there is an extra space at before and after the text.Besides,the PHP will count this extra space as part of the name,and of course you do not want your name got some extra space....ugh!its ugly lol,hahaha.here is the proof,,but before that,strlen() is use to count the length of the string....try this code:

<?PHP
$name_space=" alexander ";
$input_Count = strlen($name_space);
print $input_Count;
?>

it will print,11 ryte?but "alexander" got 9 letter ryte?.So,to settle this problem we just trim this name.trim()-it will tell the PHP,not to count the white blank as the letter.Here is the code:


<html>
<head>
<title>Lessons for Trimming space</title>


<?PHP

$name_space = trim(" alexander ");
$input_Count = strlen($name_space);
print $input_Count;


?>

</head>

<body>


</body>
</html>

  info
ltrim( )  -tell the PHP not to count space at beginning of a string
rtrim( )  -tell the PHP not to count space at end of a string

N/NOTE:hoooraay!!my name got no white blank anymore!!hahaha

Modify Case-PHP

Assalamu'alaikum and selamat sejahtera

So,for today we will learn on how to modify your string variable.For example,if we enter the input like this:"rock star" and we want the PHP to modify this input,to be like this :"Rock Star" or  "ROCK STAR".Okey,here is the code:

<html>
<head>
<title>Lessons for Changing Letter Case</title>


<?PHP

if (isset($_POST['Submit1'])) {

$full_name = $_POST['user_name'];
$full_name = ucwords($full_name);

print($full_name);

}

?>

</head>

<body>


<FORM NAME ="form1" METHOD ="POST" ACTION ="">

<Input type = 'text' Name ='user_name' value ="input name">
<P>
<INPUT TYPE = "Submit" Name = "Submit1" VALUE = "enter">
</FORM>

</body>
</html>

Here is the explainatio,so the new code for this lesson is:

$full_name = ucwords($full_name);

So,ucwords() function will take the $full_name(make sure that this variable's type is string) as its parametre,and modify tthis variable.For example,$full_name="rock star",so the ucwords($full_name) will modify $full_name as this variable has become the input for this function,finally,$full_name="Rock Star".

Besides,we also another function:


$full_ name=strtoupper($full_ name);
$full_ name=strtolower($full_ name);
$full_ name = ucfirst($full_ name);


test this function!if you want to know more...


N/NOTE:lol

Monday, February 14, 2011

Count-function-PHP

Assalamu'alaikum and selamat sejahtera,

Today,we will learn on how to count the element from your array using the code.So,the function is called count.Below is the example and some explanation.

the example:

<html>
<head>
<title>Count function</title>

<?PHP
$squad = array("alpha", "beta", "charlie", "delta");
$arr_count = count($squad);
print ("the number of element from your array=".$arr_count);
print("<br>");

for ($Number = 0; $Number < $arr_count; $Number++)
{print ("the element= ".$squad[$Number]."<BR>");}

?>
</head>
<body>
</body>
<html>

the explanation :

$arr_count = count($squad);

so we a variable to count the element in our array.In this case,we use "$arr_count" as a variable to count the element inside the array.So,it need to be declare that is equal to count($squad).So that,the count function will assign the value to $arr_count.


N\NOTE:try it!...need any help?please comment here...

Tuesday, February 8, 2011

Use random in an array-PHP


Assalamu'alaikum and selamat sejahtera

For this post,we will discuss on "array_rand( the name of your array,total of random keys that you want)" function.For example:

<?PHP
$number = array(1 => 16 ,  2 => 13 ,  3 => 44 ,  4 => 74 ,  5 => 50 ,  6 => 63);
$random_numbers = array_rand($number, 1);
print $random_numbers;
?>


PHP will get the key name (what is key name?refer on previous post ).So it only print the key name only.To get the random value of  an array ,change this line:

print $random_numbers;


to be like this:


print $number[$random_numbers];


The code:


<html>

<head>

<title>Randoms-Array</title>

<?PHP

$number = array(1 => 16, 2 => 13, 3 => 44, 4 => 74, 5 => 50, 6 => 63);

$random_numbers = array_rand($number,1);

print $random_numbers;


?>

</head>

<body>

</body>

</html>


So,that's all

N/NOTE:any confusing??

Sorting Array

Assalamu'alaikum and selamat sejahtera

This post will discuss on how to sort our array.So,before this we already learn the foreach loops,it will print all the key name and key value.So,sometimes,we may want to sort it.It very simple,we just use some special function that is:


sort($fname_of_your_array);
  
another sorting function:

rsort( ) – Sorts a array in reverse order
arsort( ) - Sorts the alphabet array in reverse order
krsort( ) - Sorts the Keys in an array in reverse order

here some proof for asort():
figure 1: Before using asort()
figure 1.1: After using asort()


The code:

<html>

<head>

<title>Foreach loops Array</title>

<?PHP

$squad_name = array();

$squad_name["alpha"] = "wing";
$squad_name["beta"] = "tail";
$squad_name["charlie"] = "head";
$squad_name["delta"] = "backup";

asort($squad_name);

foreach ($squad_name as $squad_name => $squad_code) {
print "Squad Name = " . $squad_name . " Squad Code = " . $squad_code . "<BR>";
}

?>

</head>

<body>

</body>

</html>

N/NOTE:LOL


Array-For each loops

Assalamu'alaikum and selamat sejahtera

So,what is for each loops?okey for this post we will discuss it and practice it on our code.Here is the code:


$squad_name = array( );
$squad_name["alpha"] = "one";
$
squad_name["beta"] = "two";
$
squad_name["charlie"] = "three";
$
squad_name["delta"] = "four";

foreach ($squad_name as $key_name => $key_value) 
{
print "Key = " . $key_name . " Value = " . $key_value . "<BR>";
}


So,we just concentrate on this line first:


foreach ($squad_name as $key_name => $key_value) 

 so,REMEMBER : foreach NOT for each. Okey,so you put the name of the array,in our case,the name of array is $squad_name.Next is "as $key_name =>$key_value". the key name for this array is alpha,beta,charlie and delta.So,the value of key_name,alpha is one.Do you get the idea?.Besides,you also came named it any name that like this:

foreach ($squad_name as $team_code => $team_number

N/NOTE:next post,we will discuss more on application of this loops

Friday, February 4, 2011

How to get value from array-PHP

Assalamu'alaikum dan selamat sejahtera

So,as previous post,i already give the PHP script ryte?so,this post,we will discuss on that script.Here is the script

<?PHP

$start = 1;
$times = 2;
$answer = array();

for ($start; $start < 11; $start++) {
$answer[$start] = $start * $times;
print (" answer = " . $answer[$start] . "<BR>");

}

?>:

So,just focus on this line:

$answer[$start] = $start * $times;
print (" answer = " . $answer[$start] . "<BR>");

As $start=1,then it will be look like this : $answer[$start]=$answer[1] and then we assign the value in the array,like this:

$answer[1] = 1 * $times;

and then,to print or get the value from the array,we just need print($answer[1]); ,as in our case,this line below will get the value from the array:

print (" answer = " . $answer[1] . "<BR>");

after that it will loop again ,and $start will update and it's value will be 2.

Instead use number to get the value from array.we also can use text,like this:

$group = array();

$group["alpha"]="Team One";
$group["beta"]="Team Backup";

and to get the value:

print($group["alpha"]);

that all for this post.


N/NOTE:any confusing?

Array-PHP

Assalamu'alaikum and selamat sejahtera

Okey,for this post,we will learn the array.What is array?click here for more.So,first of all i show to you on how declare the array in PHP.

the Declaration:

                  $Group = array( );

So,$Group is because i named it,well you can put any variable's name that you like.But,the important is,$Group already be an array because of "=array()".So,to assign the value to the array,look at this example below :

$Group = array();

$Group[]="Eagle";
$
Group[]="Alpha";
$
Group[]="Beta";
$
Group[]="Charlie";

beside we also can do like this:
 
 $Group = array("Eagle","Alpha","Beta","Charlie");

okey,as we assign this,PHP will assign this values to be like this:

$Group[0]=Eagle
$Group[1]=Alpha
$Group[2]=Beta
$Group[3]=Charlie

but,i want to be like this:

$Group[1]=Eagle
$Group[2]=Alpha
$Group[3]=Beta
$Group[4]=Charlie

well,its look common ryte?as usual,when we start to count ,we always start with one ryte?so,to make like this,follow this code below:

$Group = array();

$Group[1]="Eagle";
$
Group[2]="Alpha";
$
Group[3]="Beta";
$
Group[4]="Charlie";

or

 $Group = array(1=>"Eagle",2=>"Alpha",3=>"Beta",4=>"Charlie");

one more thing,if you want assign number instead of number,well here is the code:

 $Group = array(12,34,65);

or

$Group = array();

$Group[]=22;
$
Group[]=45;

so,we do not need the double quote,just straight away type what number that do you want to assign.To study more on the array.study this script:


<html>
<head>
<title>Array</title>
<?PHP

$start = 1;
$times = 2;
$answer = array();

for ($start; $start < 11; $start++) {
$answer[$start] = $start * $times;

print (" answer = " . $answer[$start] . "<BR>");
}
?>
</head>
<body>
</body>
</html>


N/NOTE:so far.....okay?

Thursday, February 3, 2011

Break while PHP looping

Assalamu'alaikum and selamat sejahtera

So,we already covered for,while and do..while loop.But as we learned that the PHP will still to loop until it reach the limit or the condition is does not true,ryte?.hOw about if we want to get out of the loop although PHP does not reach the limit or the condition still true.Okey,to do that,we must have some special code to done this task,luckily that special code is only one word that is break.Here is how the break is work:

$Interrupts = true;
$number = 1;

while ($number < =10) 
{
 

print("number = " + $number + "<BR>");
if ($Interrupts = = true) 

break;
 

$counter++;
}


Just try this code and run it.

N/NOTE:Explore it by yourself!see ya!

While Loop and Do...While Loop

Assalamu'alaikum and selamat sejahtera

After For loops ,we will learn the While loop and Do..While loop.For those who already learned the C programming or other language programming might does not need explanation on this loops.For the beginner,just click here for more explanation.

oKEy,the concept of this loop is quite simple:


while (condition) {
code that to be executed if the condition is true
}


For example

$number = 1;
while ($number =< 10) {
print (" number = " . $number . "<BR>");
$number++;
}


and for Do....While loop:

do
statement
while (condition)


for example:

$number = 1;

do
{
print (" number = " . $number . "<BR>");
$number++;

}
while ($number =< 10);

Is quite simple ryte?why don't u try these loop on the previous exercise.

The Code for do...while loop:
<html>
<head>
<title>Do...While loop</title>

<?PHP
$number=1;

do
{

print (" number = " . $number . "<BR>");
$number++;

}
while ($number <= 10);
?>

</head>

<body>
</body>

<html>


N/NOTE:lalalala

Exercise For Loop

Assalamu'alaikum and selamat sejahtera

After we learn the concept and practical of For loops.So...why dont us do some activity,this activity is called MathForLoops,the objective or cause is that we try to apply the math operation in For loops.Well as previous post,we already apply it on update , $counter and $number ,if u remember.Ok,for this post,we will do math table like times,minus,add and divide.The hint is refer the image below...i use add table,and u will do the other table.okey?hehe

Input:

Result:

Code:

<html>
<head>
<title>An Addition Table Programme</title>

<?PHP
    $add = 2;

if (isset($_POST['SubmitOne'])) {

    $initial = $_POST['txtInitial'];
    $final = $_POST['txtFinal'];
    $add = $_POST['txtAdd'];


    for($initial; $initial <= $final; $initial++) {
        $answer = $initial + $add;
        print $initial . " added by " . $add . " = " . $answer . "<BR>";
    }

}

?>

</head>

<body>


<FORM NAME = formOne Method = "POST" Action = "forloops.php">

Start Number: <INPUT TYPE = Text NAME  = txtInitial SIZE = 5 value ="1">
End Number: <INPUT TYPE = Text NAME = txtFinal SIZE = 5 value ="10">
Add By: <INPUT TYPE = Text NAME = txtAdd SIZE = 5 value = <?PHP print $add; ?> >
<P>
<INPUT TYPE = Submit Name = SubmitOne VALUE = "Do Add Table">
<P>

</FORM>
</body>

<html>

File name: forloops.php

N/NOTE:Try ur self!

For loops-PHP

Assalamualaikum and selamat sejahtera

loops?what it means?well for those who already learn the programming might already know what it is,and for beginner,click here.

make a file named ForLoop.php 
Here the code: 

<?PHP
$counter = 0;
$number = 1;

for($number; $number < 11; $number++
{
$counter = $counter + 1;
print $counter . "<BR>"; 

}
?>

Here a little explanation on this script

for(initial; limit; update){
}

initial
is where you tell PHP the initial value of your loop. In other words, which number to start the loop?So we used this:

$number = 1

Like all variables, you can make up your own name like $iterate,well it all up to you.
You must set or assign the value of $number before the loop begins, like this:

$number = 1
for($number; $number < 11; $number++) {


Or:

for($number = 1; $number < 11; $number++) {

The result is the same – the start number for this loop is 1

Limit

We have to tell PHP when to end your loop or the limit of the loop.So, we’re setting PHP to keep round the loop while the value of the variable $number is Less Than 11.

for($number; $number < 11; $number++) {

When the value of $number is 11 or higher, PHP will stop the loop.

Update

The loops need the update of starting value,in this case,we use $number,so $number need to be add if we set $number=1 or minus if we set $number=10 and the its limit is $number>1.So that,we can update the counter and display its update,thus make us believe the loops is working well.So to update,we usually like this:
  
$number++

the double plus symbol (++) =increment (increase the value by one).
It’s just a short way of saying this:
$number = $number + 1

the double minus symbol(--)=decrement (decrease the value by one)
It’s just a short way of saying this:
$number = $number - 1

So the story of this looping is like this:
“Starting/Initially/$number at a value of 1, keep going round and round while the start/$number value is less than and not equal to 11. Increase the starting/$number value by one each time round the loop.”
  
One more trick

$counter = $counter + 1;
print $counter . "<BR>"; 


or we can use like this:

 $counter++;
 print $counter . "<BR>"; 

 N/NOTE:that's all folk! 

Tuesday, February 1, 2011

Checkboxes-PHP

Assalamualaikum and selamat sejahtera

From previous,we already learn the radio button and for this post,we will learn the checkboxes.For the script of checkbox is quite similar with radio button.First we view on PHP script first:


<?PHP
$obj1 = 'unchecked';
$obj2 = 'unchecked';

if (isset($_POST['SubmitOne']))
 {

if (isset($_POST['obj1'])) {
$obj1 = $_POST['obj1'];
if ($obj1 == 'male')
{$obj1 = 'checked';}
}
 if (isset($_POST['obj2'])) {
$obj2 = $_POST['obj2'];
if ($obj2 == 'female')
{$obj2 = 'checked';}
}

}
?>

So,first we unchecked the obj1 and obj2.then we check to see if the Submit button was clicked:

if (isset($_POST['SubmitOne'])) {
}

Inside of this code,we still have another isset( ) function:

if (isset($_POST['obj1'])) {
}
this statement are checking if a checkbox was set it is because if we did not check if the checkboxes are set or not,we will deal with a lot of errors,worse is "Undefined" errors.

If the checkbox is ticked, it will return a value. So the isset( ) function will be true,then our code inside of the if statement gets executed:
if ($obj1 == 'male') {
$obj1 = 'checked';
}

.Here is the simple example...

"If the value inside of the variable called $obj1 is 'male' then change the status of obj1 to checked
The second of the if statements are the same – one for each checkbox on the form.

Ok,done for PHP script,ok for HTML form:

<FORM NAME ="form1" METHOD ="POST" ACTION ="CheckBoxes.php">

<Input type = 'Checkbox' Name ='obj1' value ="male"
<?PHP print $obj1; ?>
>Boy
<P>
<Input type = 'Checkbox' Name ='obj2' value="female"
<?PHP print $obj2; ?>
>Girl
<P>

<INPUT TYPE = "Submit" Name = "SubmitOne" VALUE = "Gender">
</FORM>

Its quite similar ryte with our previous post,ryte?

 <Input type = 'Radio' Name ='gender' value= 'male' <?PHP print $male_status; ?>>Male

Just a little change,that all..
Finally make the new file named CheckBoxes.php
The code:
<html>
<head>
<title>Lessons for Checkboxes</title>

<?PHP
$obj1 = 'unchecked';
$obj2 = 'unchecked';

if (isset($_POST['SubmitOne'])) {

if (isset($_POST['obj1'])) {
$obj1 = $_POST['obj1'];

if ($obj1 == 'male')
{$obj1 = 'checked';}
}

if (isset($_POST['obj2'])) {
$obj2 = $_POST['obj2'];

if ($obj2 == 'female')
{$obj2 = 'checked';}
}

}
?>


</head>

<body>

<FORM NAME ="form1" METHOD ="POST" ACTION ="CheckBoxes.php">

<Input type = 'Checkbox' Name ='obj1' value ="male"
<?PHP print $obj1; ?>
>Male
<P>

<Input type = 'Checkbox' Name ='obj2' value="female"
<?PHP print $obj2; ?>
>Female
<P>


<INPUT TYPE = "Submit" Name = "SubmitOne" VALUE = "Gender">
</FORM>

</body>
</html>


N/NOTE: good luck!question?

Radio buttons-PHP

Assalamualaikum and selamat sejahtera

Okey,for this we will learn about radio button.what is radio button?to know more on radio button..

So,first of all,make a file named RadioButton.php and here is the code :

<html>

<head>
<title>Lessons On Radio Buttons</title>
</head>

<body>
<Form name ="formOne" Method ="Post" ACTION ="RadioButton.php">
<Input type = 'Radio' Name ='gender' value= 'male'>Male
<Input type = 'Radio' Name ='gender' value= 'female'>Female
<P>
<Input type = "Submit" Name = "SubmitOne" Value = "Select a Radio Button">
</FORM>
</body>

</html>

A little explanation on this HTML script.ok,why i type  "value= 'male'>Male" or "value= 'female'>Female".just concentrate at ">Male" this one is for display.if we change it to Maleloolo,then it will display like this:
understand?ok shall we go to PHP script.Here is the code:


<?PHP
$male_status = 'unchecked';
$female_status = 'unchecked';


if (isset($_POST['SubmitOne'])) {

$selected_radio = $_POST['gender'];
if ($selected_radio == 'male') 
{
$male_status = 'checked';
}
 

else if ($selected_radio == 'female') 
{
$female_status = 'checked';
}
 

}
?>
Make sure the PHP script at head tag section.

Noticethis one : $male_status = 'unchecked' or $female_status = 'unchecked'.Okey for this one,this line will unchecked the value of radio button.quite blurry ryte.oke here is the example:

before we click the submit button.The blue point is at 'Male'.

after.what happen is the blue point is gone!so to keep it display even we already click the submit button.

Please,modify your HTML form like this (the bold and green is new code):

<Form name ="form1" Method ="Post" ACTION ="RadioButton.php">
<Input type = 'Radio' Name ='gender' value= 'male' <?PHP print $male_status; ?>>Male
<Input type = 'Radio' Name ='gender' value= 'female'<?PHP print $female_status; ?>>Female
<P>
<Input type = "Submit" Name = "SubmitOne" Value = "Select a Radio Button">
</FORM>

Here is the full code:

<html>
<head>
<title>Lessons On Radio Buttons</title>

<?PHP

$male_status = 'unchecked';
$female_status = 'unchecked';

if (isset($_POST['SubmitOne'])) {

$selected_radio = $_POST['gender'];

if ($selected_radio == 'male') {
$male_status = 'checked';
}
else if ($selected_radio == 'female') {
$female_status = 'checked';
}
}

?>

</head>
<body>

<Form name ="form1" Method ="Post" ACTION ="RadioButton.php">
<Input type = 'Radio' Name ='gender' value= 'male' <?PHP print $male_status; ?>>Male
<Input type = 'Radio' Name ='gender' value= 'female'<?PHP print $female_status; ?>>Female
<P>
<Input type = "Submit" Name = "SubmitOne" Value = "Select a Radio Button">
</FORM>

</body>
</html>



N/NOTE:try it!

Exercise on Text box-PHP

Assalamualaikum and selamat sejahtera

The exercise!yea ryte...So,here is the exercise for the text box:


Exercise 1
Add two text boxes and a Submit button to a HTML form.One text box for full name ,and another is for the surname.When the Submit button is clicked, print out the person's full name.

Exercise 2
So change it a little bit the script so that it will display the full name and surname in the textboxes.

Exercise 3
Suppose your web site has only 3 members. Modify or create the HTML form to check if a visitor is one of the 3 members. Display a suitable message.

N/NOTE:Good luck!

Keeping display the data that user entered

Assalamualaikum and selamat sejahtera

So,as we already learn in previous post,when we enter our username and click the submit button,the textbox will reset to its initial value which is "username".For example,

before we enter the username:


after we enter the username and clicked the login button,the textbox is reset to "username":

 
So,for this post we will learn on how to keep the our username in the textbox after we click the login button.
Its easy!hehehe,okey we just modify the PHP script like this (the bold n red text is the new code):

<?PHP

if (isset($_POST['SubmitOne']) ) {

$username = $_POST['username'];

if ($username == "master") {
print ("Welcome back, master!");
}
else {
print ("You're not a member...");
}}

else
{$username ="";}

?>

For the new code,else statement,we're just setting the value of $username for when the button is NOT clicked.For example, when the page is refreshed.

Just try it,like:
$username ="Username?";

but before run it,we will modify a little bit on HTML form (the bold n red text is the new code):

<body>
<FORM NAME ="formOne" METHOD ="POST" ACTION = "">
<INPUT TYPE = "Text" VALUE="<?PHP print $username ; ?>" NAME = "username">
<INPUT TYPE = "Submit"  Name = "SubmitOne"   VALUE = "Login">
</FORM>
</body>


N/NOTE:However, there are some security issues associated with textboxes.Soon we will learn on how to handle this in a later section

Setting ACTION-PHP

Assalamualaikum n selamat sejahtera

As we learn from previous post,we learn to submit our form data to the same PHP page.So,this post will show to you on how to submit on the other page.First thing first,we create another file,this one,we name it SecondForm.php..ok?hehe.Here is the code:

<html>
<head>
<title>THE SECOND FORM</title>
</head>
<body>

<Form name ="formOne" Method ="POST" Action ="submit2.php">
<INPUT TYPE = "TEXT" VALUE ="Username" Name ="Username">
<INPUT TYPE = "Submit" Name = "SubmitOne" VALUE = "Login">

</FORM>
</body>
</html>


Notice the bold and blue text,so for this post,we will apply the function of the ACTION.To know more on ACTION?so,make another file named: submit2.php.Yea,guess what,after we submit the form,we will send the data to submit2.php. Here is the code:

<?PHP
$username = $_POST['Username'];
if ($username == "master") {
print ("Welcome back, friend!");
}
else {
print ("You're not a member");
}

?>

So,we got two file: SecondForm.php and submit2.php


N/NOTE:try it!

Detecting the Clicked Submit Button

Assalamualaikum n selamat sejahtera

This post will discuss about more on previous post.So,as we can see that when we first load the page ,the text is already display like this:

"You're not a member" is display on first load,although we do not click the submit button yet.So,to solve this we just add this code:

if (isset ($_POST['SubmitOne']) )

on our script.A little explanation on that line,that isset() is a inbuilt function that use to checks if a variable has been set or not.In our case,we will checks the value of Submit button.So,if we first load or reload the page not refresh but reload it,then there is no value will be set for the Submit button.If we click the Submit button,then the PHP set the Submit button.Besides there is a reason why i type SubmitOne is it because the name of my Submit button is SubmitOne.

<INPUT TYPE = "Submit"  Name = "SubmitOne"   VALUE = "Login">

So change our code at PHP side to be like this:

<?PHP
if (isset ($_POST['SubmitOne']) )
{

$username = $_POST['username'];

if ($username == "master") {
print ("Welcome back, master!");
}

else {
print ("You're not a member...");
}

}
?>

The new code is in bold and red text.

N/NOTE:Miss one character,you re out!so check it!before u run it.Any question?