July 11th, 2013, 03:23 PM
-
Capture listbox multiple select
I created a listbox that populates from a MySQL database. I would like to select multiple items from the box and, eventually, save it to another database table. I'm relatively new to PHP. I'm not sure what I need to do or where to put the code in the script.
Below is the code:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Attendance</title>
<meta name="generator" content="WYSIWYG Web Builder 9 Trial Version - http://www.wysiwygwebbuilder.com">
<style type="text/css">
<!datepicker stuff that doesn't work yet>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<script> $(function() {
$( "#datepicker" ).datepicker();
});
</script>
body
{
margin: 0;
padding: 0;
background-color: #FFFFFF;
color: #000000;
}
</style>
<style type="text/css">
a
{
color: #0000FF;
text-decoration: underline;
}
a:visited
{
color: #800080;
}
a:active
{
color: #FF0000;
}
a:hover
{
color: #0000FF;
text-decoration: underline;
}
</style>
<style type="text/css">
#wb_Form1
{
background-color: #FAFAFA;
border: 0px #000000 solid;
}
#Combobox1
{
border: 1px #A9A9A9 solid;
background-color: #FFFFFF;
color: #000000;
font-family: Arial;
font-size: 13px;
}
#Editbox1
{
border: 1px #A9A9A9 solid;
background-color: #FFFFFF;
color :#000000;
font-family: Arial;
font-size: 13px;
text-align: left;
vertical-align: middle;
}
#Button2
{
border: 1px #A9A9A9 solid;
background-color: #F0F0F0;
color: #000000;
font-family: Arial;
font-size: 13px;
}
</style>
</head>
<body>
<a href="http://www.wysiwygwebbuilder.com" target="_blank"><img src="images/builtwithwwb9.png" alt="WYSIWYG Web Builder" style="position:absolute;left:453px;top:735px;width:88px;height:31px;border-width:0;z-index:250"></a>
<div id="wb_Form1" style="position:absolute;left:107px;top:50px;width:269px;height:431px;z-index:4;">
<form name="Form1" method="post" action="" enctype="text/plain" id="Form1">
<!input type="text" id="datepicker" style="position:absolute;left:26px;top:32px;width:89px;height:19px;line-height:19px;z-index:0;" name="datepicker" value="">
<input type="text" id="datepicker" style="position:absolute;left:26px;top:32px;width:89px;height:19px;line-height:19px;z-index:0;">
<select name="Listview" multiple size="17" id="Combobox1" style="position:absolute;left:30px;top:111px;width:210px;height:277px;z-index:1;">
<?php
// Create connection
$con=mysqli_connect('127.0.0.1','root','cwvm4510','maestastkd');
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysql_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM student_information WHERE StudentStatus = 'Active' and RankGroup = 'BB'");
while($row = mysqli_fetch_assoc($result)){
echo "<option value=\"{$row['StudentID']}\">{$row['StudentID']}, {$row['LastName']}, {$row['FirstName']}</option>";
}
mysqli_close($con);
?>
<input type="submit" id="Button2" name="Submit" value=Submit style="position:absolute;left:27px;top:66px;width:90px;height:25px;z-index:2;">
</form>
</div>
</body>
</html>
July 12th, 2013, 06:40 AM
-
you are outputting option values but where to??
but aside from that your logic seems good.
PS> usual drill of not using mysql_query etc etc.. also dont display errors on the screen i.e. mysql_connect_error(). i know you are doing it locally.
July 12th, 2013, 08:07 AM
-
Originally Posted by paulh1983
you are outputting option values but where to??
but aside from that your logic seems good.
PS> usual drill of not using mysql_query etc etc.. also dont display errors on the screen i.e. mysql_connect_error(). i know you are doing it locally.
I had not put an output in at this point. I was going to send to the scren for now and eventually to a MySQL table. I'm sending the error to the screen for now so that I can see them in testing.
Still not really sure how to capture the listbox selections.
Bob
July 12th, 2013, 08:23 AM
-
Originally Posted by kickerfour
Still not really sure how to capture the listbox selections.
Bob
Google is your friend. Really.
July 12th, 2013, 09:45 AM
-
i cant see any <select> & </select> tags for starters..
apart from that what does your page output at this point? i dont see any problem with it?
July 12th, 2013, 10:55 AM
-
Originally Posted by kickerfour
I created a listbox that populates from a MySQL database. I would like to select multiple items from the box and, eventually, save it to another database table. I'm relatively new to PHP. I'm not sure what I need to do or where to put the code in the script.
http://stackoverflow.com/questions/2...ect-box-in-php
<select name="Listview[]" multiple size="17" id="Combobox1" ...
Then when PHP picks up the Listview parameter, it'll be an array of values you can loop through.
Side note:
Multi select form fields suck, IMO. Even if a use knows how to use them, it's inconvenient to have to scroll and CTRL-click your options. Then if you fatfinger and normal click on one item, all the other things you've chosen deselect.
If you have a smallish list of items then I'd recommend just using a series of checkboxes instead.
There are fancier Javascript/CSS-based alternatives to multi-select boxes for longer lists, but obviously that'd be a bit more work.
Last edited by dmittner; July 12th, 2013 at 10:58 AM.
July 12th, 2013, 03:00 PM
-
After some serious time reading and testing I found this:
original
<form name="Form1" method="post" action="value.php" enctype="text/plain" id="Form1">
corection:
<form name="Form1" method="post" action="value.php" enctype="" id="Form1">
Post error? Use Get? Not sure but it's working fine now.