/    Sign up×
Community /Pin to ProfileBookmark

MySQL query from form’s output – Help !

Hey guys,

I need to wright a MySQL query that take the items of selectedFields to be as my fields in the output table

I mean something like that: SELECT (all items of selectedField list) FROM table1

what should be instead of (all items of the selectedField list) .. !!??

[code]
<HTML>
<HEAD>
<TITLE>Form</TITLE>
<SCRIPT LANGUAGE=”JavaScript” SRC=”form.js”></SCRIPT>
</HEAD>
<BODY>
<style type=”text/css”>
#list { width: 200px;}
#button {width: 100px;}
</style>
<br/>
<h1>Field Selection</h1>
<form name=”fieldselectionform”>
<table width=”100%”>
<tr>
<td nowrap>Available Fields</td>
<td>&nbsp;</td>
<td nowrap>Selected Fields</td>
<td>&nbsp;</td>
<td rowspan=2 align=”center” valign=”bottom”>
<input type=”button” id=”button” value=”Clear Form” onClick=”moveAllOptions(document.forms[0]

[‘selectedFields’],document.forms[0][‘availableFields’]); “>
</td>
</tr>
<tr>
<td width=”20%”>
<select size=”20″ id=”list” multiple name=”availableFields” onDblClick=”moveSelectedOptions(this.form

[‘availableFields’],this.form[‘selectedFields’])”>
<option value=”item_1″>Item 1</option>
<option value=”item_2″>Item 2</option>
<option value=”item_3″>Item 3</option>
<option value=”item_4″>Item 4</option></select>
</td>
<td width=”20%” align=”center” valign=”center” nowrap>
<input type=”button” id=”button” name=”add” value=”>>” onClick=”moveSelectedOptions(document.forms[0]

[‘availableFields’],document.forms[0][‘selectedFields’]);”>
<br><br>
<input type=”button” id=”button” name=”remove” value=”<<” onClick=”moveSelectedOptions(document.forms

[0][‘selectedFields’],document.forms[0][‘availableFields’]);”>
</td>
<td width=”20%”>
<select size=”20″ multiple id=”list” name=”selectedFields” onDblClick=”moveSelectedOptions(this.form

[‘selectedFields’],this.form[‘availableFields’])”>
</select>
</td>
<td width=”20%” align=”center” valign=”center” nowrap>
<INPUT TYPE=”button” id=”button” VALUE=”Move Up” onClick=”moveOptionUp(this.form

[‘selectedFields’])”>
<BR><BR>
<INPUT TYPE=”button” id=”button” VALUE=”Move Down” onClick=”moveOptionDown(this.form

[‘selectedFields’])”>
</td>
</tr>
</table>
</form>
</BODY>
</HTML>
[/code]

here’s java functions:

[code]
// ——————————————————————-
// moveSelectedOptions(select_object_From,select_object_To)
// This function moves options between select boxes. Works best with
// multi-select boxes to create the common Windows control effect.
// Passes all selected values from the first object to the second
// object.
// You can also put this into the <SELECT> object as follows:
// onDblClick=”moveSelectedOptions(this,this.form.target)
// This way, when the user double-clicks on a value in one box, it
// will be transferred to the other (in browsers that support the
// onDblClick() event handler).
// ——————————————————————-
function moveSelectedOptions(from,to) {
// Move them over
if (!hasOptions(from)) { return; }
for (var i=0; i<from.options.length; i++) {
var o = from.options[i];
if (o.selected) {
if (!hasOptions(to)) { var index = 0; } else { var index=to.options.length; }
to.options[index] = new Option( o.text, o.value, false, false);
}
}
// Delete them from original
for (var i=(from.options.length-1); i>=0; i–) {
var o = from.options[i];
if (o.selected) {
from.options[i] = null;
}
}
from.selectedIndex = -1;
to.selectedIndex = -1;
}

// ——————————————————————-
// moveOptionUp(select_object)
// Move selected option in a select list up one
// ——————————————————————-
function moveOptionUp(obj) {
if (!hasOptions(obj)) { return; }
for (i=0; i<obj.options.length; i++) {
if (obj.options[i].selected) {
if (i != 0 && !obj.options[i-1].selected) {
swapOptions(obj,i,i-1);
obj.options[i-1].selected = true;
}
}
}
}

// ——————————————————————-
// moveOptionDown(select_object)
// Move selected option in a select list down one
// ——————————————————————-
function moveOptionDown(obj) {
if (!hasOptions(obj)) { return; }
for (i=obj.options.length-1; i>=0; i–) {
if (obj.options[i].selected) {
if (i != (obj.options.length-1) && ! obj.options[i+1].selected) {
swapOptions(obj,i,i+1);
obj.options[i+1].selected = true;
}
}
}
}

// ——————————————————————-
// selectAllOptions(select_object)
// This function takes a select box and selects all options (in a
// multiple select object). This is used when passing values between
// two select boxes. Select all options in the right box before
// submitting the form so the values will be sent to the server.
// ——————————————————————-
function selectAllOptions(obj) {
if (!hasOptions(obj)) { return; }
for (var i=0; i<obj.options.length; i++) {
obj.options[i].selected = true;
}
}

// ——————————————————————-
// moveAllOptions(select_object,select_object[,autosort(true/false)[,regex]])
// Move all options from one select box to another.
// ——————————————————————-
function moveAllOptions(from,to)
{
selectAllOptions(from);
moveSelectedOptions(from,to);
}

// ——————————————————————-
// hasOptions(obj)
// Utility function to determine if a select object has an options array
// ——————————————————————-
function hasOptions(obj) {
if (obj!=null && obj.options!=null) { return true; }
return false;
}

// ——————————————————————-
// swapOptions(select_object,option1,option2)
// Swap positions of two options in a select list
// ——————————————————————-
function swapOptions(obj,i,j) {
var o = obj.options;
var i_selected = o[i].selected;
var j_selected = o[j].selected;
var temp = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
var temp2= new Option(o[j].text, o[j].value, o[j].defaultSelected, o[j].selected);
o[i] = temp2;
o[j] = temp;
o[i].selected = j_selected;
o[j].selected = i_selected;
}
[/code]

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@replicaJul 12.2007 — I hope I figured out what you mean.

Here's my solution. I added the necessary ID's and elements into the HTML and stuck a bit of javascript before the form submit action. I think you'll figure it out.

[code=html]<HTML>
<HEAD>
<TITLE>Form</TITLE>
<SCRIPT LANGUAGE="JavaScript" SRC="form.js"></SCRIPT>
<style type="text/css">
.list { width: 200px;}
#button {width: 100px;}
</style>
</HEAD>

<BODY>
<br />
<h1>Field Selection</h1>
<form name="fieldselectionform" id="fieldselectionform" method="post" action="test.php">
<table width="100%">
<tr>
<td nowrap>Available Fields</td>
<td>&nbsp;</td>
<td nowrap>Selected Fields</td>
<td>&nbsp;</td>
<td rowspan=2 align="center" valign="bottom">
<input type="button" id="button" value="Clear Form" onClick="moveAllOptions(document.forms[0]['selectedFields'],document.forms[0]['availableFields']);" />
</td>
</tr>
<tr>
<td width="20%">
<select size="20" class="list" id="availableFields" multiple name="availableFields" onDblClick="moveSelectedOptions(this.form['availableFields'],this.form['selectedFields']);">
<option value="item_1">Item 1</option>
<option value="item_2">Item 2</option>
<option value="item_3">Item 3</option>
<option value="item_4">Item 4</option>
</select>
</td>
<td width="20%" align="center" valign="center" nowrap>
<input type="button" id="button" name="add" value=">>" onClick="moveSelectedOptions(document.forms[0]['availableFields'],document.forms[0]['selectedFields']);" />
<br />
<br />
<input type="button" id="button" name="remove" value="<<" onClick="moveSelectedOptions(document.forms[0]['selectedFields'],document.forms[0]['availableFields']);" />
</td>
<td width="20%">
<select size="20" multiple class="list" id="selectedFields" name="selectedFields" onDblClick="moveSelectedOptions(this.form['selectedFields'],this.form['availableFields']);">
</select>
</td>
<td width="20%" align="center" valign="center" nowrap>
<INPUT TYPE="button" id="button" VALUE="Move Up" onClick="moveOptionUp(this.form['selectedFields'])" />
<BR />
<BR />
<INPUT TYPE="button" id="button" VALUE="Move Down" onClick="moveOptionDown(this.form['selectedFields'])" />
</td>
</tr>
</table>
<script type="text/javascript">
function process_submit()
{
//a reference to the selectedField list
var o1 = document.getElementById('selectedFields');

//a reference to the hidden form element
var o2 = document.getElementById('selFields');

for( var x = 0; x < o1.length; x++ )
o2.value += o1[x].value += ':';

//cut off the last ':' character
o2.value = o2.value.substring(0, o2.value.length - 1);

//submit the form
document.getElementById('fieldselectionform').submit();
}
</script>
<input type="hidden" value="" name="selFields" id="selFields" />
<button onclick="process_submit(); return false;">Submit Form</submit>
</form>
</BODY>
</HTML>[/code]

then I just added this to parse what was in that hidden element
[code=php]<?php

$sql = '';

if( isset( $_POST['selFields'] ) )
{
$selectedFields = explode( ':', $_POST['selFields'] );

$selectList = '';
foreach( $selectedFields as $selectedField ) {
$selectList .= $selectedField . ', ';
}

$selectList = substr( $selectList, 0, -2 );

$sql = "
SELECT ". $selectList ."
FROM Table1
WHERE foo = bar
";
}

echo $sql; //do with it what you will
?>[/code]
Copy linkTweet thisAlerts:
@cooolauthorJul 13.2007 — I've tried that.. it doesn't work !


I did that.. the array is empty ! ..

what I need is just to save the content of the list inside a string with comma between each line of the content..

if you looked at the javascript code, you'll see that I'm already able to move all the items from one list to another... why can't I move these items to a string !! with a comma between the items..

for example..

if this is my list:

item 1

item 6

item 5

item 3

my string should look like this: item1,item6,item5,item3

items in this situation are my table's fields..

so I can use this string later in my query.. i.e. SELECT string FROM table1

I think it's just a simple javascript code..

I'm trying it's not working ! ...

any help - ideas - piece of code ?
Copy linkTweet thisAlerts:
@cooolauthorJul 16.2007 — I've solved the problem..

no worries ?
×

Success!

Help @coool spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...