list box with items for deletion passed from php array

S

student4lifer

Hello,

I want to display a list box that contains the elements from an array
(queried from database). There is a button next to the list box to
delete item(s) in the list box (and the same item(s) would be deleted
from database as well). It seems to me the functions for populating
and deleting items in the list box can be accomplished with
javascript. Could someone show me the best way to do this (not just
populating the list box but also for deleting the items afterwards)?
For starter, is dynamically 'echoing' the elements into a javascript
array (inside the <script language=javascript> tag) the right way to
populate the list box? TIA.
 
S

SAM

Le 12/25/08 10:15 PM, (e-mail address removed) a écrit :
Hello,

I want to display a list box that contains the elements from an array
(queried from database). There is a button next to the list box to
delete item(s) in the list box (and the same item(s) would be deleted
from database as well). It seems to me the functions for populating
and deleting items in the list box can be accomplished with
javascript. Could someone show me the best way to do this (not just
populating the list box but also for deleting the items afterwards)?
For starter, is dynamically 'echoing' the elements into a javascript

It has not to do that.
array (inside the <script language=javascript> tag) the right way to
populate the list box?

No, the php can (and must) populate directly the html.
(why to do that with JS with risk the JS will not then populate the
select ?)


<form id="myForm" action="uptodate.php">
<select name="menu">
<?php
// $options is the php array queried from database
foreach($option in $options) {
echo "<option value='$option'>$option</option>\n";
}
?>
</select>
<input type="hidden" name="deleted" value="">
<input type="submit" onclick="return deleter()"
value="Delete selected item">
<script type="text/javascript">
document.write('<input type="submit" value="UpToDate">');
</script>
</form>


JS (in head) :
==============

function deleter() {
var f = document.getElementById('myForm');
var s = f.elements['menu'].options;
var o = s[s.selectedIndex];
f.elements['deleted'].value += ','+o.value;
for(var i=s.selectedIndex; i<s.length-1; i++) {
s.value = s[i+1].value;
s.text = s[i+1].text;
}
s.length = s.length-1;
return false;
}


The file 'uptodate.php' will have to be analyzed by the php:
- if field 'deleted' is empty
it gets the variable 'menu' which is the item do delete.
- if it is not empty, its value will be the array of items to delete
 
B

Bart Van der Donck

I want to display a list box that contains the elements from an array
(queried from database). There is a button next to the list box to
delete item(s) in the list box (and the same item(s) would be deleted
from database as well). It seems to me the functions for populating
and deleting items in the list box can be accomplished with
javascript. Could someone show me the best way to do this (not just
populating the list box but also for deleting the items afterwards)?
For starter, is dynamically 'echoing' the elements into a javascript
array (inside the <script language=javascript> tag) the right way to
populate the list box? TIA.

This would be such a task where javascript is not needed. Here is a
CGI approach, with [mytable]![ID] and [mytable]![description]:

#!/usr/bin/perl
use strict;
use warnings;
use DBI;
print qq@Content-Type: text/html; charset=iso-8859-1\n\n@;
my $list;
my %conf = ( name => 'DB_NAME_GOES_HERE',
host => 'localhost',
user => 'DB_USER_GOES_HERE',
pass => 'DB_PASS_GOES_HERE' );
foreach (split(/&/,$ENV{'QUERY_STRING'})) {
$list.= $_.',' if ((s/^ID=//)=~/^\d$/);
}
if ($list ne '') {
chop $list;
my $db = DBI->connect('DBI:mysql:'.$conf{name}.':'.$conf{host},
$conf{user}, $conf{pass});
$db->do('DELETE FROM mytable WHERE ID IN ('.$list.')');
$db->disconnect;
}
print qq@<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Delete item(s) from list</title>
</head>
<body>
<form method="get" action="$ENV{SCRIPT_NAME}"
onSubmit="return confirm('Sure?')">
Delete from list (select more with CTRL/SHIFT + click)
<br>
<select size="20" multiple name="ID">
@;
my $db = DBI->connect('DBI:mysql:'.$conf{name}.':'.$conf{host},
$conf{user}, $conf{pass});
my $query = $db->prepare('SELECT ID, description FROM mytable
ORDER BY description ASC');
$query->execute;
while (my @a = $query->fetchrow_array) {
print qq{<option value="$a[0]">$a[1]</option>\n}}
$query->finish;
$db->disconnect;
print '</select><br>
<input type="submit" value="Delete">
</form>
</body>
</html>';

Hope this helps,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top