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
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
April 25, 2012 01:15PM
Hi Mac,

Yeah I was planning on using an 'if' statement to set checked or not checked. I was more stuck on how to pull the courses that the student registered for from the course_student and compare the cid's so as only to check the courses the student registered for.

In the end I figured out one way I can do this: I did the following and it works

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';]; $checked = "";   $comparesql = "SELECT cid FROM course_student WHERE sno = ';" . $_GET[';id';] . "';";     $compareresult = mysql_query($comparesql) or die ("Invalid query: " . mysql_error());       while ($row2 = mysql_fetch_array($compareresult)) {     $cidcompare = $row2[';cid';];     if ($cid == $cidcompare) { $checked = "checked"; }   }   ?>     <input type = ';checkbox'; name =';courseselect[]'; value ="<?php echo $cid; ?>" onclick="setItems(this)" <?php echo $checked; ?>/> <?php echo $cname; ?> <br/>     <?php   $checked = "";   }   ?>

I feel like this may be a very clumsy way though? But this was the only way I could think of doing it so far. Any suggestions would be appreciated because this course is my first time coding php so my code is still very beginner level and not very streamlined smiling smiley
avatar Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
April 25, 2012 04:36PM
I did something similar but i made use of;
Language: PHP
array_push() in_array()
in my own case.
avatar
Mac
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
April 26, 2012 06:43AM
Why do you start with the course table? The information is in the course_student table is there for a reason. Using the sno you get the cid's from this table, and then fetch the course names from the course table using the cid..
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
April 26, 2012 09:32AM
Well I used the course table because I am selecting every course and making a checkbox for it, and then use the course_student table to get only the courses that the selected student has registered for and only checking those...

so my form looks like this:


All the courses appear in a list of checkboxes, but only the ones the student registered for are checked and can then be edited.

Is this not the right way to do it? confused smiley
avatar
Mac
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
April 26, 2012 10:48AM
O OK. nothing wrong with that. I you remove all the white space from your code it is only a few lines smiling smiley

Have a look at this. Just a look smiling smiley http://www.wellho.net/solutions/mysql-left-joins-to-link-three-or-more-tables.html
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
April 26, 2012 11:05AM
Oh wow, I didn't know there were all different ways to join tables like that eye popping smiley. Thanks ^_^
avatar
Mac
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
April 26, 2012 01:22PM
It is really advanced stuff and points at how one should design tables. It falls outside the ambit of this course (we have to stop somewhere), and in the absence of such advanced offerings you do learn to apply some logic. And that is equally good.
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
April 30, 2012 01:27AM
Regarding 'Switch Statements'. I've backtracked a bit so that I can better understand the codes

Is there a way that one can pass more than one variable in a switch statement, for a username and password?

I've found this on the following site and it's what I'm trying to figure out:
http://www.dreamincode.net/forums/topic/9428-passing-an-array-into-a-switch-statement/
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
April 30, 2012 01:43PM
Allowing a student to edit information.

I have created a hidden field in my form which hold the id of the selected student.
And it update the column based on that given id.
This will probably apply for deleting.
avatar
Mac
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
May 02, 2012 08:01AM
franky71713913 Wrote:
-------------------------------------------------------
> Regarding 'Switch Statements'. I've backtracked a
> bit so that I can better understand the codes
>
> Is there a way that one can pass more than one
> variable in a switch statement, for a username and
> password?
>
The switch is there to switch between different individual variables - you should not be using a switch for this purposes.
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
May 03, 2012 10:29PM
Oh ok, thanks Mac
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
May 04, 2012 12:12PM
With help of regular expression, i've created an htaccess file.
Every call in my app goes via an index.php file
my htaccess sort of drives every call and ive appended the index page with the module, controller and action.
Language: PHP
RewriteEngine On RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST_FILENAME} -l [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^.*$ - [NC,L] RewriteRule ^index/([^/\.]+).html/?$ index.php?module=index&controller=index$action=$1 [L] RewriteRule ^([^/\.]+).html/?$ index.php?module=index&controller=index$action=$1 [L]   RewriteRule ^index/([^/\.]+).html/?$ index.php?module=index&controller=index$action=$1 [L] RewriteRule ^([^/\.]+).html/?$ index.php?module=$1 [L] RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?module=$1&controller=$2 [L] RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?module=$1&controller=$2&action=$3 [L] RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+).html/?$ index.php?module=$1&controller=$2&action=$3 [L] RewriteRule ^.*$ index.php [NC,L]

This code overwrite every call you make and append the url with given module, controller, and action. For example
To add new student
http://localhost/registration/student/add

The index page has the following code
Language: PHP
//Use DS to separate the directories in other defines if (!defined(';DS';)) { define(';DS';, DIRECTORY_SEPARATOR); } //The full path to the directory which holds "app", WITHOUT a trailing DS if (!defined(';ROOT';)) { define(';ROOT';, dirname(__FILE__)); }   //Controls whether to display errors or not define(';DEVELOPMENT_ENVIRONMENT';, true);   if (!defined(';CORE_INCLUDE_PATH';)) { define(';CORE_INCLUDE_PATH';, dirname(__FILE__)); }   //Call the bootstrap file require (CORE_INCLUDE_PATH . DS . ';bootstrap.php';);

In the bootstrap, for every class created, i use the autoload function for it to be automatically loaded.
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
May 04, 2012 12:35PM
^^^ holy crap...

what are you doing in this course man...you are making me feel dumb tongue sticking out smiley
avatar
Mac
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
May 04, 2012 04:33PM
PHP freaks web site induced .. drinking smiley
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
May 07, 2012 11:50AM
77471466_Alwyn Wrote:
-------------------------------------------------------
> ^^^ holy crap...
>
> what are you doing in this course man...you are
> making me feel dumb tongue sticking out smiley

Initially i was critisized for putting the code in the forum, but now im glad some people are seeing the value in it.
Hey man im learning everyday.
It actually took me sometime to come up with that piece of code.
I also have a nice couple of functions in my bootstrap file, which set all my include files

Language: PHP
#set include path $path = array(ROOT . DS . ';model'; . DS, CORE_INCLUDE_PATH . DS . ';classes'; . DS, CORE_INCLUDE_PATH . DS); set_include_path(get_include_path() . PATH_SEPARATOR . implode(PATH_SEPARATOR, $path));

This is a nice piece because you dont have to use the include function for every page call, you can just create that page and it will be called automatically.
But you have to implement the autoload function, like

Language: PHP
/** Autoload any classes that are required */ function __autoload($className) { try{ if (file_exists(CORE_INCLUDE_PATH . DS . "classes" . DS . "{$className}.php") || file_exists(CORE_INCLUDE_PATH . DS . "{$className}.php") || file_exists(CORE_INCLUDE_PATH . DS . "model" . DS . "{$className}.php")) { require_once "{$className}.php"; } } }
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
May 07, 2012 12:26PM
Very well done man! Looking very interesting!
And something I'll be working towards, as of now I'll try to master the basics...as I don't want to try and learn everything to fast and end up knowing nothing.
But thanks for opening my eyes
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
May 07, 2012 03:49PM
77471466_Alwyn Wrote:
-------------------------------------------------------
> Very well done man! Looking very interesting!
> And something I'll be working towards, as of now
> I'll try to master the basics...as I don't want to
> try and learn everything to fast and end up
> knowing nothing.
> But thanks for opening my eyes


No problem man.
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
May 09, 2012 11:13AM
77570952-Sian Wrote:
-------------------------------------------------------
> Well I used the course table because I am
> selecting every course and making a checkbox for
> it, and then use the course_student table to get
> only the courses that the selected student has
> registered for and only checking those...
>
> so my form looks like this:
>
>
> All the courses appear in a list of checkboxes,
> but only the ones the student registered for are
> checked and can then be edited.
>
> Is this not the right way to do it? confused smiley


If i may ask, what method did you use to set and get the date of birth?
Thanks
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
May 09, 2012 12:28PM
What I did was make separate select boxes for day, month, year. Then I put them into a variable with a dash between each:

Language: PHP
$dob = $_POST[';bod_year';] . "-" . $_POST[';bod_month';] . "-" . $_POST[';bod_day';];

When I re-populate the form to edit the date of birth, I get this from the database and explode() it:

Language: PHP
$birthday = explode("-", $dob); $byear = $birthday[0]; $bmonth = $birthday[1]; $bday = $birthday[2];


Then I built the select box and made the day, month and year select the date from the database. For example, below is how I built the select box for Day:

Language: PHP
<select name="bod_day"> <option value="" selected>Select a day...</option> <?php   for ($day=1; $day<=31; $day++) {   if ($day == $bday) { $dselected = " selected"; } else { $dselected = ""; }   if ($day<10) { $day = "0" . $day; } ?> <option value="<?php echo $day; ?>"<?php echo $dselected; ?>><?php echo $day; ?></option> <?php } ?> </select>

And that's how I did it ^_^
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
May 09, 2012 03:02PM
77570952-Sian Wrote:
-------------------------------------------------------
> What I did was make separate select boxes for day,
> month, year. Then I put them into a variable with
> a dash between each:
>
>
Language: PHP
> $dob = $_POST[';bod_year';] . "-" . > $_POST[';bod_month';] . "-" . $_POST[';bod_day';]; >
>
> When I re-populate the form to edit the date of
> birth, I get this from the database and explode()
> it:
>
>
Language: PHP
> $birthday = explode("-", $dob); > $byear = $birthday[0]; > $bmonth = $birthday[1]; > $bday = $birthday[2]; >
>
>
> Then I built the select box and made the day,
> month and year select the date from the database.
> For example, below is how I built the select box
> for Day:
>
>
Language: PHP
> <select name="bod_day"> > <option value="" selected>Select a > day...</option> > <?php > > for ($day=1; $day<=31; $day++) { > > if ($day == $bday) { > $dselected = " selected"; > } else { > $dselected = ""; > } > > if ($day<10) { > $day = "0" . $day; > } > ?> > <option value="<?php echo > $day; ?>"<?php echo $dselected; ?>><?php echo > $day; ?></option> > <?php } ?> > </select> >
>
> And that's how I did it ^_^

Awesome 77570952-Sian

I've been wondering how to insert the leading zeros in front.
But instead of using if statement, i normally use the ternary operator like this

Language: PHP
($day<10) ? $day = "0" . $day : "";

As for the first option select, you have the default option selected and inside the option you have the choosen option selected again. Can you elaborate?
I use OOP to manage that, to populate the date of birth i use

Language: PHP
$dob = explode("/", $students->dob);
Language: HTML
<select name="day"> <option value="0" echo ($dob[0] == "0") ? " selected" : "";>Select Day</option>   for ($x=1;$x<=31;$x++):   <option echo ($dob[0] == $x) ? " selected":"";>echo $x;</option>   endforeach;     </select>     But anyway thanks man
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
May 09, 2012 03:25PM
Your way looks more efficient than mine smiling smiley But with regards to the select box, I left the default one selected because with the html it will get overwritten by the next option you make selected so I just left it as is. ..
Re: PHP Four - Working with HTML Forms - PHP and HTML Checkboxes
May 10, 2012 03:08PM
77570952-Sian Wrote:
-------------------------------------------------------
> Your way looks more efficient than mine smiling smiley But
> with regards to the select box, I left the default
> one selected because with the html it will get
> overwritten by the next option you make selected
> so I just left it as is. ..


No problem man.

I actually found nice functions you might find usefull

Language: PHP
#Check for Magic Quotes and remove them * */ function stripSlashesDeep($value) { $value = is_array($value) ? array_map(';stripSlashesDeep';, $value) : stripslashes($value); return $value; }   function removeMagicQuotes() { if (get_magic_quotes_gpc()) { $_GET = stripSlashesDeep($_GET); $_POST = stripSlashesDeep($_POST); $_COOKIE = stripSlashesDeep($_COOKIE); } }   #Check register globals and remove them function unregisterGlobals() { if (ini_get(';register_globals';)) { $array = array(';_SESSION';, ';_POST';, ';_GET';, ';_COOKIE';, ';_REQUEST';, ';_SERVER';, ';_ENV';, ';_FILES';); foreach ($array as $value) { foreach ($GLOBALS[$value] as $key => $var) { if ($var === $GLOBALS[$key]) { unset($GLOBALS[$key]); } } } } }

To call those,
Language: PHP
removeMagicQuotes(); unregisterGlobals();

Cheers man
Sorry, you do not have permission to post/reply in this forum.