Jump to content

MySQL query from form's output - Help !


coool

Recommended Posts

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) .. !!??

 

<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> </td>
            <td nowrap>Selected Fields</td>
            <td> </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>

 

here's java functions:

// -------------------------------------------------------------------
// 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;
}

Link to comment
https://forums.phpfreaks.com/topic/59663-mysql-query-from-forms-output-help/
Share on other sites

try

if(count($_POST['selectedFields'])) $fields = implode(', ', $_POST['selectedFields']);


$sql =" SELECT $fields FROM table1";

and change part of form to

<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>

name must have [] on end then can have more then one value

you need to select values what you want to form pass

try

<HTML>
<HEAD>
<TITLE>Form</TITLE>
<SCRIPT LANGUAGE="JavaScript">
// -------------------------------------------------------------------
// 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;
}
</SCRIPT>
</HEAD>
<BODY>
     <style type="text/css">
       #list { width: 200px;}
       #button {width: 100px;}
     </style>
      <br/>
      <h1>Field Selection</h1>
      <form name="fieldselectionform" method="POST" onsubmit="selectAllOptions(document.forms[0]

['selectedFields[]'])">
        <table width="100%">
          <tr>
            <td nowrap>Available Fields</td>
            <td> </td>
            <td nowrap>Selected Fields</td>
            <td> </td>
            <td rowspan=2 align="center" valign="bottom">
            	<input type="submit"><br />
              <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>
<?php
if ($_POST) {
echo 'You select: ';
echo implode($_POST['selectedFields'],', ');
} else echo 'nothing selected'
?>
</BODY>
</HTML>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.