473,553 Members | 3,205 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inserting selection box elements into a database

6 New Member
Hello,

I realise there are a lot of topics related to this problem but many of what I have found has run cold or unresolved. What I have is an addressbook clone where there are groups which have members. I have this formatted in a way where the groups are in a drop down box. Once a group is selected, the two selection boxes below are refreshed with those who are members or non members of the selected group. The user has the ability to transfer those contacts from the members box to the non members box, and vice versa. PHP, html and MySQL are being used.

I also have three tables:
contacts: containing account details including contactID as its PK
groups: containing the group description including groupID as its PK
groupmembers: containing the previous two PKs as FKs

After searching for resources, I've managed to get a javascript function that allows the transfer of contacts from one selection box to the other

[HTML]function move(nonMemBox, memBox)
{
var arrNonMemBox = new Array();
var arrMemBox = new Array();
var arrLookup = new Array();
var i;

// This for loop gets the length from the destination field
for (i = 0; i < memBox.options. length; i++)
{
arrLookup[memBox.options[i].text] = memBox.options[i].value;
arrMemBox[i] = memBox.options[i].text;
}
var nonMemLength = 0;
var memLength = arrMemBox.lengt h;

// This for loop gets the length of the source field
for(i = 0; i < nonMemBox.optio ns.length; i++)
{

arrLookup[nonMemBox.optio ns[i].text] = nonMemBox.optio ns[i].value;
// If if the item is selected and not empty
if (nonMemBox.opti ons[i].selected && nonMemBox.optio ns[i].value != "")
{
arrMemBox[memLength] = nonMemBox.optio ns[i].text;
memLength++;
}
else
{
arrNonMemBox[nonMemLength] = nonMemBox.optio ns[i].text;
nonMemLength++;
}
}

// This alphabetically sorts the array of the items
//arrFbox.sort();
//arrTbox.sort();
nonMemBox.lengt h = 0;
memBox.length = 0;
var c;

// now transfer the items stored in the arrFbox[] array
for(c = 0; c < arrNonMemBox.le ngth; c++)
{
var no = new Option();
no.value = arrLookup[arrNonMemBox[c]];
no.text = arrNonMemBox[c];
nonMemBox[c] = no;
}

// now send all the items to the destination field
for(c = 0; c < arrMemBox.lengt h; c++)
{
var no = new Option();
no.value = arrLookup[arrMemBox[c]];
no.text = arrMemBox[c];
memBox[c] = no;
}
}

function addMe()
{
theValues = document.combo_ box.member;

for(i = 0; i < theValues.lengt h; i++)
{
alert(theValues[i].value);
if(document.com bo_box.theNumbe rs.value == "" )
{
document.combo_ box.theNumbers. value = theValues[i].value;
}
else
{
document.combo_ box.theNumbers. value += "," + theValues[i].value;
}
}
return true;
}[/HTML]

The function responsible for refreshing the page with the drop down box are as follows:

[HTML]function reload(form)
{
var val=form.Group. options[memberAssignmen tForm.Group.opt ions.selectedIn dex].value;
self.location=' groupContacts.p hp?Group=' + val ;
}[/HTML]

The above function uses $Group = $_GET['Group'] to get the selected groupID. That is then used for the following code

[HTML]<form name="memberAss ignmentForm" method="post" action="groupCo ntacts.php" onsubmit="retur n addMe();">
<input id="theNumbers " name="theNumber s" type="hidden" />
<table>
<tr>
<td><b>Group: </b><select name="Group" onchange="retur n reload(this.for m);">
<option value="select"> Select one</option>
<?
// Retrieves and displays groups from db
$query="select * from groups order by Name";
$result=$db->query($query );
$num_results=$r esult->num_rows;
for($i=0; $i<$num_results ; $i++)
{
$row=$result->fetch_assoc( );
echo '<option value=';
echo $row['groupID'];
echo '>';
echo $row['Name'];
echo '</option>';
}
$result->free();
?>
</select></td>
</tr><tr></tr>
<tr>
<td align="center"> <b>Non-members</b></td>
<td><p>&nbsp; </p></td>
<td align="center"> <b>Members</b></td>
</tr>
<tr>
<td><select name="nonMember " size="10" multiple style="width: 150px;" ondblclick="mov e(this.form.non Member,this.for m.member)">
<?
// Retrieves contact names that belong to selected group and displays in the right selection box
$query = "select * from contacts where contactID not in (select contactID from groupcontact where groupID = '".$Group."' )";
$result=$db->query($query );
$num_results=$r esult->num_rows;
for($i=0; $i<$num_results ; $i++)
{
$row=$result->fetch_assoc( );
echo '<option value=';
echo $row['contactID'];
echo '>';
echo $row['Surname'],', ', $row['Firstname'];
echo '</option>';
}
$result->free();
?>
</select></td>
<td><input type="button" name="Disable" value="&gt; " style="width: 20px;" onClick="move(t his.form.nonMem ber,this.form.m ember)"><br>
<br>
<input type="button" name="Enable" value="&lt;" style="width: 20px;" onClick="move(t his.form.member ,this.form.nonM ember)"><br></td>
<br>
<td><select name="member" size="10" multiple style="width: 150px;" ondblclick="mov e(this.form.mem ber,this.form.n onMember)">
<?php
// Retrieves contact names that are don't belong to selected group and displays in the left selection box
$query = "select * from contacts where contactID in (select contactID from groupcontact where groupID = '".$Group."' )";
$result=$db->query($query );
$num_results=$r esult->num_rows;
for($i=0; $i<$num_results ; $i++)
{
$row=$result->fetch_assoc( );
echo '<option value=';
echo $row['contactID'];
echo '>';
echo $row['Surname'], ', ', $row['Firstname'];
echo '</option>';
}
$result->free();
?>
</select></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="Save"></td>
</tr>
</table>
</form>
</body>
</html>[/HTML]

Now what I am trying to resolve is to submit the form where the contacts in their respective boxes and insert them into the database - specifically the groupmembers table, which would end up looking like...

groupID contactID
12 44
12 45
12 46
13 22

Another problem I'm having difficulty understanding is that when I select a group from the drop down box, it obviously refreshes. However, once it does the correct contacts are displayed but the drop down box reverts back to its original state eg. Select one

What am I missing that ensures the selected group is displayed once refreshed?

I have tried working with arrays on the selection box inputs but it breaks the move function. An answer to these two questions would be greatly appreciated. Apologies for the length of this question but I figured giving this much information would provide a better answer. Of course I'll be happy to clarify anything that is vague.

Thank you
Mar 11 '08 #1
4 2556
dlite922
1,584 Recognized Expert Top Contributor
to quickly select the value that was selected before the page refresh, simple follow this logic:

[PHP]
for ($i = 0; $i < count($someArra y); $i++)
{
if($someArray[$i] == $someID)
echo "<option value=$someArra y[$i] selected='selec ted'>$someArray[$i]";
else
echo "<option value=$someArra y[$i]>$someArray[$i]";
}
[/PHP]

I'll see if i have time to cover your other issues, anybody else feel free to jump in.
Mar 11 '08 #2
sialater
6 New Member
[PHP]
for ($i = 0; $i < count($someArra y); $i++)
{
if($someArray[$i] == $someID)
echo "<option value=$someArra y[$i] selected='selec ted'>$someArray[$i]";
else
echo "<option value=$someArra y[$i]>$someArray[$i]";
}
[/PHP]
Thank you for the response. Regarding your suggestion. I am not using arrays to access the drop down box whatsoever so I'm confused as to how I would apply that logic. It's just querying the database to retrieve the results.

[PHP]$query="select * from groups order by Name";
$result=$db->query($query );
$num_results=$r esult->num_rows;
for($i=0; $i<$num_results ; $i++)
{
$row=$result->fetch_assoc( );
echo '<option value=';
echo $row['groupID'];
echo '>';
echo $row['Name'];
echo '</option>';
}[/PHP]
Mar 12 '08 #3
sialater
6 New Member
I have solved my refresh problem. It was so simple, it's criminal. I just needed to echo the inputs as selected in a loop. Thanks dlite922 for kickstarting the idea.
[PHP]
$query="select * from groups order by Name";
$result=$db->query($query );
$num_results=$r esult->num_rows;
for($i=0; $i<$num_results ; $i++)
{
$row=$result->fetch_assoc( );
if($row['groupID'] == $Group)
{
echo '<option selected value=';
echo $row['groupID'];
echo '>';
echo $row['Name'];
echo '</option>';
}
else
{
echo '<option value=';
echo $row['groupID'];
echo '>';
echo $row['Name'];
echo '</option>';
}
}[/PHP]

One problem solved. Another one to go.
Mar 13 '08 #4
sialater
6 New Member
No one has a solution?
Mar 24 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
2120
by: pmud | last post by:
I have an ASP.NET web application using C# code. I am trying to insert values from a web form into an SQL database. I am using SQL COMMAND object for this. I need to know HOW TO INSERT THE RADIO BUTTON LIST SELECTION entered by the user in the web form into tha database. for eg. for inserting values from a text box into the database we write...
15
3294
by: lawrence | last post by:
Is this the correct way to test for a method before I use it? createRange() is, I believe, an IE only method. function wrapSelectionInTag(selection, tag) { if (document.selection.createRange) { var range = document.selection.createRange(); if (range.parentElement() == element) range.text = '<' + tag + '>' + range.text + '<\/' + tag +...
1
615
by: ajk | last post by:
. Hi, All: I know how to insert files into a Word doc using C#. However, the program I've written to do this runs much too slowly. The "myObj".Application.Selection.InsertFile method executes at a snails pace. Here are the detais: I wrote a C# program that creates a new Word doc and then loops through a
2
6060
by: Clark Stevens | last post by:
Hi. This should be so easy, but I don't get it. Let say I have RichTextbox1 and I want to insert some text at the current insertion point, or at the beginning of selected text (if there is any). How is this done? Also, how do I delete the current selection? Thanks.
7
1453
by: Andy C Matthews | last post by:
Hi there, I'm building an Access database and using VBA to generate Microsoft Word mailings for customers. It's all going fine so far. However, a variables named ParcelID, a ten-digit string such as TQ03409954, needs to be split into SHEET ID (the first six characters) and PARCEL ID (the last 4) e.g. TQ0340 9954. Can anyone help me? I'm...
7
2157
subashini Thiyagarajan
by: subashini Thiyagarajan | last post by:
Hi , In ASP how to deactivate few fields while inserting a record to the database(access). i want to insert a record.e.g having 5 fields.it is not reqired to fill all the fields in a form as per my condition.upon first filed selection(drop down) remaining field should show or deactivate how to dooooooooooooooooooo? As per the dropdown...
2
3441
by: gruvin | last post by:
Hi. I need to insert an image element into text in an iframe in design mode. That part I can do: var sel = oEditor.eFrameDoc.selection; var range = sel.createRange(); range.pasteHTML('<img src="'+imagePath+'" border="0" />'); But I need a reference to that new IMG tag to do other stuff with it.
2
4958
by: santuvssantu | last post by:
Hi Friends, I have tried inserting an excel file to a word document.But the following code is inserting the junk information. So can you please help me out? Dim m_WordApp As New Word.Application Dim m_WordDoc As Word.Document m_WordDoc = m_WordApp.Documents.Open(CObj("C:\Documents and Settings\santu\My...
1
4587
by: javediq143 | last post by:
Hi All, This is my first post in this forum. I'm developing a CMS for my latest website. This CMS is also in PhP & MySQL. I'm done with the ADD section where the Admin can INSERT new records in Database but I'm stuck in the EDIT. I'm getting 2 problems over here. Below is the description: 1)The FIRST page will list all the records from the...
0
7570
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7493
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7775
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8008
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7526
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7854
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5411
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5133
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1992
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.