Welcome! Log In Create A New Profile

Advanced

PHP Four - Working with HTML Forms - PHP and HTML Checkboxes

Posted by 77469917-Dfreeman 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
February 13, 2012 11:09AM
Hi there again.

So when testing the checkboxes script. i see all the == are seperated by a space. which i've now changed. but when you choose books is it supposed to tell you which ones you picked ?
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
February 15, 2012 04:03PM
I have completed this section.
Golly, I tried to answer your question but now sure what you mean.
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
February 16, 2012 08:36AM
It's only suppose to keep the boxes you ticked, ticked when you press the submit button.
avatar Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
February 16, 2012 10:38AM
Language: PHP
<?php //when you load page, this set the default of the checkboxes i.e unchecked as stated below $me = ';unchecked';; $you = ';unchecked';; $us = ';unchecked';;   //this code deals with the method of how the form is submitted. if (isset($_POST[';submit1';])) { /* at this point, the code states that if by POST $me is the same value as tea; the checked box of me will return true */ if(isset($_POST[';me';])) { $me = $_POST[';me';]; if($me == ';tea';) { $me = ';checked';; } } /* similarly, the code states that if by POST $you is the same value as coke; the checked box of you will return true */ if(isset($_POST[';you';])) { $you = $_POST[';you';]; if($you == ';coke';) { $you = ';checked';; } } /* also, the code states that if by POST $us is the same value as cake; the checked box of us will return true */ if(isset($_POST[';us';])) { $us = $_POST[';us';]; if($us == ';cake';) { $us = ';checked';; } } } ?>

Language: HTML
/*<?php print $me or $you or $us; ?> in the body content retains the the actual action performed! If true (the boxes remain checked) else remain unchecked.*/ <body> <form name="form1" method="POST" action= "t.php">   <input type=';checkbox'; name=';me'; value=';tea'; <?php print $me; ?>> tea<br> <input type=';checkbox'; name=';you'; value=';coke'; <?php print $you; ?>> coke<br> <input type=';checkbox'; name=';us'; value=';cake'; <?php print $us; ?>> cake<br> <br> <input type=';submit'; value=';Choose varieties to remember'; name=';submit1';></p> </form>   </body>
try to review this short example and read the comments in the code. Am sure it will help in its own little way. Section completed. Cheers!
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
February 19, 2012 12:06PM
I have completed this section, php4 - Working with HTML forms.

For the last exercise where it asks "Suppose your web site has only 5 users. Create a HTML form to check if a visitor is one of the 5 users. Display a suitable message" I tried out using both if / else and switch statements. They both work the same way, but what would be better to use for something like this in practice? To use if/else or switch? Because if they can both do the same thing is it just a matter of preference?


Here is my code

IF / ELSE

Language: PHP
<?PHP if (isset($_POST[';Submit1';])) {   $user1 = "user1"; $user2 = "user2"; $user3 = "user3"; $user4 = "user4"; $user5 = "user5";   $username = $_POST[';username';];     if ($username == $user1){ print(';Welcome user1';); } else if($username == $user2){ print(';Welcome user2';); } else if($username == $user3){ print(';Welcome user3';); } else if($username == $user4){ print(';Welcome user4';); } else if($username == $user5){ print(';Welcome user5';); }   else { print(';You are not a valid user';); }   } else { $username =""; }   ?>


and SWITCH

Language: PHP
<?PHP if (isset($_POST[';Submit1';])) {   $username = $_POST[';username';];     switch ($username) {   case "user1": print("Welcome user1"); break;   case "user2": print("Welcome user2"); break;   case "user3": print("Welcome user3"); break;   case "user4": print("Welcome user1"); break;   case "user5": print("Welcome user1"); break;   default: print("You are not a valid user");   }   } else { $username =""; }   ?>
avatar
Mac
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
February 20, 2012 07:27AM
77570952-Sian Wrote:
-------------------------------------------------------
> I have completed this section, php4 - Working with
> HTML forms.
>
> For the last exercise where it asks "Suppose your
> web site has only 5 users. Create a HTML form to
> check if a visitor is one of the 5 users. Display
> a suitable message" I tried out using both if /
> else and switch statements. They both work the
> same way, but what would be better to use for
> something like this in practice? To use if/else or
> switch? Because if they can both do the same thing
> is it just a matter of preference?
>

http://www.ashishpaliwal.com/blog/2009/08/if-else-vs-switch-%E2%80%93-which-is-better/
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
February 20, 2012 02:23PM
hm.... I'd say switch looks nice and neat when you have many "IF's" to do.


The Switch is also very nice to read - specifically to check between the 5 users. An If Else would look messy.

So mainly you use IF (maybe for 1 to 3 or 4 checks) , but when it is a longer list of things to check (like the 5 users) a Switch is more readable and also easier for you to find errors later on when looking at your code.


My view..
smiling smiley
But I know that over time it will become clear
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
February 20, 2012 02:36PM
I have read this code thoroughly.
But what if i leave two spaces before typing username, would your code work.
Trim function is very important.
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
February 20, 2012 07:30PM
I have completed this section.

Good point - 77592972 Innocent. Validation is very important when testing values from the user. I like the isset() function from php. Other languages use (variable == null) to check if variables have been set. Unfortunately space counts as a value and will validate true by the isset() function.
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
February 20, 2012 08:37PM
thank you for link Mac.
avatar Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
February 21, 2012 02:25PM
Regarding "How to validate checkboxes using javascript" (Tapan's script linked to in php4p11.html).

Language: HTML
<input type=';checkbox'; name=';chkboxes'; value=';1';> 1<br> <input type=';checkbox'; name=';chkboxes'; value=';2';> 2<br> <input type=';checkbox'; name=';chkboxes'; value=';3';> 3<br>

The form on the sample uses the same 'name' attribute on each checkbox input. Does this not cause problems when reading $_POST?

I tested it out and it looks like only the value of the last checkbox of those that share a name is posted to php. So if you were to check, for example, both 2 and 3, only '3' would in fact end up in $_POST['chkboxes'].

Am I missing something or is the form setup in the example not ideal?
avatar
Mac
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
February 21, 2012 03:03PM
Add two brackets after chkboxes to feed it into an array

Language: PHP
<input type=';checkbox'; name=';chkboxes[]'; value=';1';> 1<br> <input type=';checkbox'; name=';chkboxes[]'; value=';2';> 2<br> <input type=';checkbox'; name=';chkboxes[]'; value=';3';> 3<br>
avatar Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
February 22, 2012 10:33AM
mac Wrote:
-------------------------------------------------------
> Add two brackets after chkboxes to feed it into an
> array
>
>
Language: PHP
> <input type=';checkbox'; name=';chkboxes[]'; > value=';1';> 1<br> > <input type=';checkbox'; name=';chkboxes[]'; > value=';2';> 2<br> > <input type=';checkbox'; name=';chkboxes[]'; > value=';3';> 3<br> >

Cool. Good to know. Thanks smiling smiley
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
February 22, 2012 01:37PM
mac Wrote:
-------------------------------------------------------
> Add two brackets after chkboxes to feed it into an
> array
>
>
Language: PHP
> <input type=';checkbox'; name=';chkboxes[]'; > value=';1';> 1<br> > <input type=';checkbox'; name=';chkboxes[]'; > value=';2';> 2<br> > <input type=';checkbox'; name=';chkboxes[]'; > value=';3';> 3<br> >

Was just thinking the same thing grinning smiley
avatar Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
February 25, 2012 04:48PM
I've completed this section.
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
February 25, 2012 07:53PM
Referring to this link: www.ashishpaliwal.com. I'm trying to figure out the code language,

System.out.println;

I've seen this exact coding in javascript, what is the difference with "javascript" and "php"?
I'm busy with the first exercise, and i'm battling a bit to understand the coding required to construct to display both first name and surname, and to allow it to function correctly?

Regards
avatar Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
February 25, 2012 08:58PM
franky71713913 Wrote:
-------------------------------------------------------
> Referring to this link: www.ashishpaliwal.com. I'm
> trying to figure out the code language,
>
> System.out.println;
>
> I've seen this exact coding in javascript, what is
> the difference with "javascript" and "php"?
> I'm busy with the first exercise, and i'm battling
> a bit to understand the coding required to
> construct to display both first name and surname,
> and to allow it to function correctly?
>
> Regards


http://www.webmasterforums.com/php-development/15767-what-difference-between-php-javascript.html click on this link and read the post to know the difference between the two languages.
Read through this code in respect of your second question.
Language: PHP
<?php $First_Name = ';firstname';; $Surname = ';surname';; $Full_Name = $First_Name.'; ';. $Surname;   print (';My full name is';.'; ';.$Full_Name);   ?>
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
February 25, 2012 09:04PM
Thank you, 77553055 this is definitely helping me understand.smile
avatar Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
February 25, 2012 09:06PM
you welcome Franky. Cheers!smileys with beer
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
February 25, 2012 10:38PM
Cheers!! smileys with beer

I have found another web site that appears to have quite a lot on php: http://www.tizag.com/
It shows pages of the if/else statement, if statement, etc... with a bit of detail.

Regards
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
February 27, 2012 11:35AM
Hi all

There is another way other than switch and if else statement

Language: PHP
<?php $name = "innocent"; echo $name == "innocent" ? "I love php" : "Couldn\';t be better"; ?>

or

Language: PHP
<?PHP if (isset($_POST[';Submit1';])) {   $username = $_POST[';username';];   print ($username == "user1") ? "Welcome user1" : ""; print ($username == "user2") ? "Welcome user2" : ""; print ($username == "user3") ? "Welcome user3" : ""; print ($username == "user4") ? "Welcome user4" : ""; print ($username == "user5") ? "Welcome user5" : ""; } ?>

I stand to be correctly, just to illustrate the point.
avatar
Mac
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
February 27, 2012 12:15PM
Yes. There are many shortcuts in PHP that you willl discover as you go along.
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
February 27, 2012 09:25PM
Completed this section
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
February 28, 2012 08:53AM
this is so silly but i really have very limited time to explore php like most, just have time to work through course material, and make sure i understand it.
but i am finally at the end of PHP 4, and i played around with the code of a few examples.
and one was fun for i get to combine previous work a bit with new work

Language: PHP
<html> <head> <title>A BASIC FORM IN HTML</title> <?PHP   $username=$_POST[';username';];   if($username=="letmein"){ print("welcome back, friend!"); } else if($username==25){ print("bonus code"); } else{ print("You';re not a member of this site"); } ?>   </head> <body>   <FORM NAME="form1"METHOD="post"ACTION="basicForm.php">   <INPUT TYPE="TEXT" VALUE="username"NAME="username"> <INPUT TYPE="submit" Name="submit1" VALUE="Login"> `   </body> </html>

i just added an else if line of code into the example to play around with options
now i also get a response if enter a bonus code
drinking smiley sorry for adding such silly stuff, i really am just trying to contribute

maybe someone can give me a few extra interesting code to stuff in there to play around with different results
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
February 29, 2012 10:48PM
i've tried it like this:
What am I doing wrong? I've tried and tried it, but I am lost on what I am missing

Language: PHP
<html> <head> <title>Basic Form Exercise 1</title>   <?php   if(isset($_POST[';Submit2';])) {   $user = $_POST["username"];   $pass = $_POST["password"];   //* the list of users   $User1_name = ';seether';; $User1_passw = ';jones';; $user1 = $User1_name.'; ';. $User1_passw;   $User2_name = ';metallica';; $User2_passw = ';baker';; $user2 = $User2_name.'; ';. $User2_passw;   $User3_name = ';coldplay';; $User3_passw = ';adam';; $user3 = $User3_name.'; ';. $User3_passw;   $User4_name = ';rowland';; $User4_passw = ';blue';; $user4 = $User4_name.'; ';. $User4_passw;   $User5_name = ';moby';; $User5_passw = ';october';; $user5 = $User5_name.'; ';. $User5_passw;   // end of list //Begin validation code   if($username == "$user1"){ $validated = true; }   else if($username == "$user2"){ $validated = true; }   else if($username == "$user3"){ $validated = true; }   else if($username=="$user4"){ $validated = true; }   else if($username=="$user5"){ $validated = true; }   //end validation code   if($validated){ print("welcome, $user.;   else{   print("invalid username/password combination, please try again."); }   ?>     </head> <body>   <FORM name="form1" method="post" action="basicFormex1.php"> <input type="text" name="username" value="<?php print $username;?>"> <input type="password" name="password" value="<?php print $password;?>"> <input type="submit" name="Submit2" value="Display"> </FORM> </body> </html>
avatar
Mac
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
March 01, 2012 06:46AM
elseif and not else if.
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
March 01, 2012 06:00PM
I have completed this section. I found an interesting tutorial on the difference between the isset function and the empty function. Check it out at php-isset-vs-empty
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
March 02, 2012 01:41PM
Hi

I see an example from franky71713913 but what if someone using his own form to post data.
How would you make sure that someone doesn't his form to post data?

Thanks
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
March 02, 2012 09:18PM
I still seem to be receiving an error with the coding,
even after i have changed the else if to elseif, I have tried using other examples,

I am not sure what part of the form you are referring to 77592972_innocent,
but if you are talking about security of the form,
i think this site could have the answer you are referring to:


http://www.php.net/manual/en/security.variables.php

or:

http://www.php.net/manual/en/security.php

(if you want to see the list of security things)

I do hope that you find the answer to what your question is.
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
March 02, 2012 10:34PM
I have finally understood what i was doing wrong, i have put the brackets in the wrong place, and I have successfully got it to work. Here is a working simple validation of username & password:

<html>
<head>
<title>A BASIC HTML FORM with username & password validation</title>
Language: PHP
<?PHP   if (isset($_POST[';Submit1';])) { $username = $_POST[';username';];   $password = $_POST[';password';];   if ($username =="Letmein" && $password =="aeRials") { print ("Welcome back, letmein!"); }   else if ($username =="gEorgia" && $password =="vIx") { print ("Welcome back, gEorgia!"); }   else if ($username =="Pieter" && $password =="piErs") { print ("welcome back, pieter!"); }   else if ($username =="Paul" && $password =="plIers") { print ("welcome back, paul!"); }   else if ($username =="Greg" && $password =="spanNer") { print ("Welcome back, greg!"); }   else if ($username =="Gary" && $password =="lOcknut") { print ("Welcome back, gary!"); }   else { print ("You';re not a member of this site, please create a new profile!"); } }   ?>
</head>
<body>

<Form name ="form1" Method ="POST" Action ="basicFormex3.php">

<INPUT TYPE = "text" NAME ="username" VALUE ="<?php print $username ; ?>">
<INPUT TYPE = "password" NAME = "password" VALUE = "<?php print $password ; ?>">
<INPUT TYPE = "submit" NAME = "Submit1" VALUE = "Login">

</FORM>

</body>
</html>

And therefore, I have completed this section!
thumbs upsmile
Sorry, you do not have permission to post/reply in this forum.