Welcome! Log In Create A New Profile

Advanced

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

Posted by 77469917-Dfreeman 
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
March 02, 2012 11:47PM
quit tricky chapter for me i keep forgetting <BR>
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
March 03, 2012 12:00AM
the radio buttons that i have done while i couldn't figure out the coding for the exercises i found easier than i expected, i have practiced it,
and I find it very interesting, as the javascript coding looks so much more complicated than what the php version of coding appears, i suppose for javascript to be used, it takes quite a few more characters than the amount php uses.
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
March 03, 2012 10:30PM
I completed the section.

All I wondered about was for what reason in HTML someone would use the GET method rather than POST method.
avatar Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
March 04, 2012 02:32AM
I believe the POST is better than the GET because it tends to secure your form from exposing information on the URL thereby encrypting data during operations. Check out this link [www.cs.tut.fi] and gain more knowledge about it.
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
March 04, 2012 11:01AM
Good website for progamming Q&A, if you haven't ever used it - [stackoverflow.com]
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
March 04, 2012 07:58PM
Finished,

This section was great, as a front end developer I never really understood how a form worked or what you could do with the data that the user inputted.
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
March 05, 2012 11:42AM
Done with this section
And all the lazy kidz like me who dont wanna read juss watch the tutorials with me at php academy

or


here
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
March 07, 2012 12:04PM
Just in case others have not noticed and are getting strange results... the tutorial (section 7 of PHP4) has an error - the line:

Language: PHP
if (isset($_POST['Submit'])) {

should be:

Language: PHP
if (isset($_POST['Submit1'])) {

i.e. should check the NAME attribute (Submit1) of the submit button, not the TYPE attribute.
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
March 07, 2012 01:03PM
In the radio buttons section - perhaps a good idea to check if a radio button has been selected when submit is pressed (else you get annoying "Undefined index: gender" message) i.e. check first if 'gender' has been set:
Language: PHP
<?PHP $male_status = 'unchecked'; $female_status = 'unchecked'; if (isset($_POST['Submit1'])) { if (isset($_POST['gender'])) { $selected_radio = $_POST['gender']; print $selected_radio; if ($selected_radio == 'male') { $male_status = 'checked'; } else if ($selected_radio == 'female') { $female_status = 'checked'; } }else {print("Please select an item!");}; }   ?>

Then at least you can give your user a meaningful message i.e "Please select an item!"
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
March 07, 2012 01:35PM
Section completed.
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
March 07, 2012 02:08PM
Completed the session and here is a link to a tutorial that will show you hw to submit the data into a data base and query the validity of the username from a database


click me!!
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
March 08, 2012 02:57PM
Another useful if else statements...

Language: PHP
<?PHP $male_status = 'innocent'; if (!empty($male_status)) : echo 'Available'; else : echo 'Not Available'; endif; ?>
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
March 08, 2012 09:52PM
Hi 77570952

Your code helped me with something I was struggling with,good one.
Thanxsmile
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
March 10, 2012 11:27AM
This is what I have done over the weekend.
Language: PHP
<HTML> <HEAD> <TITLE>Apache testing</TITLE> </HEAD> <BODY> <form action = "page.php" method="post"> username <input type = "text" name="username" /><br /> password <input type = "password" name="pass" /><br /> <input type="submit" value="log in" /> </form> </BODY> </HTML>

Language: PHP
<?php $username = $_POST['username']; $password = $_POST['pass'];   $user = 'shelley'; $pass = 1234; if (($username == $user) && ($password == $pass)) echo 'Successfull'; else echo 'Try again'; ?>

Creating a login form with the database.
Language: PHP
<HTML> <HEAD> <TITLE>Apache testing</TITLE> </HEAD> <BODY> Login <form action = "page.php" method="post"> username <input type = "text" name="username" /><br /> password <input type = "password" name="pass" /><br /> <input type="submit" value="log in" /> </form> <a href="register.php">Register</a> </BODY> </HTML>

Connecting information to the database.
Language: PHP
<?php $username = $_POST['username']; $password = $_POST['pass'];   $con = mysql_connect('localhost', 'root', ''); mysql_select_db('shelley', $con); mysql_query("INSERT INTO users (username, password) VALUES ('$username', $password)"); ?>
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
March 10, 2012 12:51PM
I have retrieved data from the database and compared it to the information provided by the form.
Language: PHP
<?php $username = $_POST['username']; $password = $_POST['pass'];   $con = mysql_connect('localhost', 'root', ''); mysql_select_db('shelley', $con); $RESULT = mysql_query("SELECT * FROM users WHERE username = '$username' AND password = $password"); $count = mysql_num_rows($RESULT); if ($count > 1) echo 'Welcome'; else echo 'Loser'; ?>
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
March 10, 2012 03:26PM
Ok, you've already hooked up the form with a database - I have to catch up, btw completed section 4, albeit slow and steady.
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
March 11, 2012 11:08AM
Completed this section.....and l am busy trying to figure out how to upload images using php ie profile pictures....
any help will be greatly appreciated
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
March 11, 2012 03:47PM
Guyz there is a site that will surely help you because it works for me.
The site is www.w3schools.com
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
March 12, 2012 09:38PM
mm. forms seem to make sense but boggles the mind ! Hope I can keep it all together
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
March 12, 2012 10:05PM
mind boggling ! Done
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
April 15, 2012 09:15PM
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
April 17, 2012 09:16AM
Hi

For me to allow for registration of multiply courses i have a multiply select html element to allow for that.
To insert into a database, i have glued all the courses with |.
For example,
Language: PHP
$courses = array('PHP', 'HTML', 'DATABASES', 'AJAX', 'JQUERY'); $courses = implode('|', $courses);
And i insert the courses variable into courses table.

Any quicker way i can do this?
avatar Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
April 17, 2012 09:57AM
I want some discussion on this by other students.
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
April 17, 2012 11:22AM
77592972_innocent Wrote:
-------------------------------------------------------
> Hi
>
> For me to allow for registration of multiply
> courses i have a multiply select html element to
> allow for that.
> To insert into a database, i have glued all the
> courses with |.
> For example,
>
Language: PHP
> $courses = array('PHP', 'HTML', 'DATABASES', > 'AJAX', 'JQUERY'); > $courses = implode('|', $courses); >
> And i insert the courses variable into courses
> table.
>
> Any quicker way i can do this?


I've gone the exact same route except I used tickboxes and not a multiple select.

I read all the ticked boxes into an array, and then implode it into a string.
Then feed my array containing my student, along with the courses string, into my function, exploding it, and adding the data to the course_student table.
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
April 18, 2012 01:18PM
Hi guys

I have created a nice way of recording how long a script tabke to execute.
From the start page i have this code,
Language: PHP
define('PAGE_PARSE_START_TIME', microtime());

At the end of every page i have the following,
Language: PHP
if (STORE_PAGE_PARSE_TIME == 'true') { $time_start = explode(' ', PAGE_PARSE_START_TIME); $time_end = explode(' ', microtime()); $parse_time = number_format(($time_end[1] + $time_end[0] - ($time_start[1] + $time_start[0])), 3); error_log(strftime(STORE_PARSE_DATE_TIME_FORMAT) . ' - ' . getenv('REQUEST_URI') . ' (' . $parse_time . 's)' . "\n", 3, STORE_PAGE_PARSE_TIME_LOG);   if (DISPLAY_PAGE_PARSE_TIME == 'true') { echo '<span class="smallText">Parse Time: ' . $parse_time . 's</span>'; } }

In this way, in every page it shows how long does a script take to execute.
I use the error_log set to 3 and it will append the file
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
April 18, 2012 01:21PM
I too have completed this section.thumbs up
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
April 22, 2012 07:52PM
Thanks Innocent and Alwyn,

I was just about to write a post about this but was looking first to see if anyone had a similar problem. I also wanted to use checkboxes to select multiple courses but was trying to figure out how to put all the selected values into the database, so I will try your method by feeding the selected courses into an array and see if that works for me ^_^ Thanks for posting that here, it helped!
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
April 23, 2012 07:57AM
77570952-Sian Wrote:
-------------------------------------------------------
> Thanks Innocent and Alwyn,
>
> I was just about to write a post about this but
> was looking first to see if anyone had a similar
> problem. I also wanted to use checkboxes to select
> multiple courses but was trying to figure out how
> to put all the selected values into the database,
> so I will try your method by feeding the selected
> courses into an array and see if that works for me
> ^_^ Thanks for posting that here, it helped!


Glad we could help man
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
April 25, 2012 12:08AM
So feeding the checked courses into an array and posting to the database worked really well for me for adding a new student. The problem I am having now is when you update a student's details. Because the form to edit the student's details must already be populated with the student's existing details when being edited, I have gotten stuck on how to make sure that the checkboxes for the courses the student has registered for are checked when editing.

I am using this code to build the checkboxes:

Language: PHP
<?php $coursesql = "SELECT * FROM course";     $courseresult = mysql_query($coursesql) or die ("Invalid query: " . mysql_error());     while ($row = mysql_fetch_array($courseresult)) {     $cid = $row['cid']; $cname = $row['cname'];   ?>     <input type = 'checkbox' name ='courseselect[]' value ="<?php echo $cid; ?>" onclick="setItems(this)" /> <?php echo $cname; ?> <br/>     <?php } ?>


So I know I need to query the course_student table to get all the courses that are related to the student being edited, but what would be a good way to then get the registered courses to be checked?
avatar Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
April 25, 2012 06:52AM
You can use and if statement

Language: PHP
if($value_in_db > 0) { //assuming you used 0 and 1 to indicate it is checked $checked='checked'; } echo "<input type....... $checked>; //
Sorry, you do not have permission to post/reply in this forum.