Welcome! Log In Create A New Profile

Advanced

Selecting a course name

Posted by afojonny 
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
Selecting a course name
May 13, 2013 12:57AM
Hey fellow programmers,

It's 12:50 in the AM and i'm still up, battling with my project...yawning smiley

A quick one: I'm trying to include a "Multiline List Box", something like a drop-down list, for the "Course name" area of the student_reg.php page.

Here's my code:

Language: PHP
<td> Course name: </td> <td > <select name="courses" style=width:"150px"> <option value="" selected>Select a course...</option> //================================ //SELECT LIST OF COURSES AVAILABLE //================================ <?php $query = "SELECT * FROM course"; $result = mysql_query($query) or die (mysql_error());   while ($row = mysql_fetch_array($result)){   echo "<option value=" . $row[';cname';] . ">"; echo "</option>";   } ?>   </select>

It's working fine except that the courses are not showing. It's printing out the rows but the names are not visible.

What could be wrong in the code?

Thanks in anticipation...

78026962 afojonny
avatar
Mac
Re: Selecting a course name
May 13, 2013 08:09AM
The name must be echoed between the option tags....
avatar Re: Selecting a course name
May 13, 2013 08:53AM
Try something like this:

Language: PHP
$sql = "SELECT cid, cname FROM course ORDER BY cname ASC"; $selResult = mysql_query($sql); echo"<option value = ';0';>Select Course</option>"; while($selRows = mysql_fetch_array($selResult)){ $id = htmlspecialchars($selRows["cid"]) ; $course = htmlspecialchars($selRows["cname"]); echo "<option value=';$id';>$course</option>"; }
Re: Selecting a course name
May 13, 2013 10:57AM
Thanks mac!

Thanks AlexB...it worked perfectly well. By the way, why do i need to format $id and $course with the htmlspecialchar?

78026962 afojonny
avatar Re: Selecting a course name
May 13, 2013 12:07PM
I read on one of the PHP forums that this will also help secure data coming from the outside of your site. You can use it without the htmlspecialchar, I did it as a test to see what happens.



afojonny Wrote:
-------------------------------------------------------
> Thanks mac!
>
> Thanks AlexB...it worked perfectly well. By the
> way, why do i need to format $id and $course with
> the htmlspecialchar?
Re: Selecting a course name
May 13, 2013 01:10PM
Awesome stuff! Thanks again AlexB.

78026962 afojonny
Sorry, only registered users may post in this forum.

Click here to login