Tuesday, February 1, 2011

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?

Friday, January 28, 2011

LeARning Textbox :PHP

Salam,

so,how r u?fine?not so fine?emm..wanna some aspirin?i got some,heh.So,its quite long i doesn't post ,to be honest ,i'm quite forget what we learn the last post,doesn't a matter because this code below may remind us..huhu,just maybe or maybe not...whatever,hehe:

<html>
<head>
<title>The First HTML form</title>
</head>
<body>

<FORM NAME ="formOne" METHOD =" POST" ACTION = "">
 <INPUT TYPE = "TEXT"  VALUE ="username">
<INPUT TYPE = "Submit"  Name = "SubmitOne"   VALUE = "Login">
 </FORM>

</body>
</html>
u remember ryte? if not?just click here.So for this post we will concentrate on this line:

<INPUT TYPE = "Text" VALUE ="username">

change that line to this:

<INPUT TYPE = "Text" VALUE ="username" NAME = "username">


The NAME of our textbox is "username". u can use the other name like namewoo,lor or anything name that u like but for this post ,we will use "username".

So,add this code :

<?PHP
$username = $_POST['username'];
print ($username);
?>

on head section of ur html code like this:

<html>
<head>
<title>The First HTML form</title>
<?PHP
$username = $_POST['username'];
print ($username);
?>
</head>

so,just run it and ignore this error :

Notice: Undefined index: username in.....

just click the button "login",u should see like this:

Delete the text "username" from the textbox,and type any name that u like and click the button again. ur new text should appear above the textbox.So far okay?

So the $_POST[] is an inbuilt function that u can use to get POST data from a form. If ur METHOD = "GET" on ur form, then u should change ur line to this::

$username = $_GET['username'];

u type the  username,because NAME="username"...

<INPUT TYPE = "Text" VALUE ="username" NAME = "username">


so that why we type like this:  


$_POST['username'];

if NAME="namewoo" then   

$_POST['namewoo'];

as long ur method in ur form still METHOD="POST".


$username = $_POST['username'];

So PHP will look for a HTML form element with the NAME username. It then looks at the VALUE attribute for this form element. It returns this value for you to use and manipulate.

<INPUT TYPE = "Text" VALUE ="username" NAME = "username">


in this case the Value ="username",that why when we run the script,we can see word "username" inside the textbox.

so,we manipulate it by change this code to like this:

 <html>
<head>
<title>The First HTML form</title>

<?PHP
$username = $_POST['username'];


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

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

?>

</head>


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


</html>

 so,run the code.u should see like this:


then u type "master" inside the textbox,click button.u should see like this:


then the "master" automatically change to "username" because the text box is getting reset when the data is returned to the browser. The Value attribute of the text box is what is being displayed.

that all for today..
tq

N\Note :any question?lor

Monday, January 17, 2011

Submit button (HTML+PHP)

Hye every one?how r u?so for this post we will discuss more on submit button,this lesson will take short time to understand!trust me,hahaha

so,to send this script to somewhere, u just need a HTML Submit button:

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

we don't need to do add some special code or script or anything special with a Submit button, it is because  the submitting is already done by the PC,server. Easily to say that, we don't need think more about this submitting stuff. As long as SUBMIT has an ACTION set, then ur data will get sent somewhere.What i meant is before this line ,we must have this line first:

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

It is for to assign value to the METHOD and the ACTION.Besides,the NAME attribute is very helpful if we want to check either if the form is really submitted ,or the user just clicked the cancel or refresh button only.My Submit button is called "SubmitOne", but u can call it almost anything u like.


N/NOTE:is that clear?this post just explain which line is enable the submit button,that's all.