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

No comments:

Post a Comment