MySQL query from form's output - Help !

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
coool
Forum Commoner
Posts: 45
Joined: Wed Jul 11, 2007 5:51 pm

MySQL query from form's output - Help !

Post by coool »

Everah | Please use the correct tags when highlighting code: use the PHP or the Syntax... menu items


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: Select all

<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>
here's java functions:

Code: Select all

// -------------------------------------------------------------------
// 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;
	}
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

How does the HTML or Javascript you posted have anything to do with PHP?
coool
Forum Commoner
Posts: 45
Joined: Wed Jul 11, 2007 5:51 pm

Post by coool »

well instead of the items listed in the available fields list, I have (php code that is getting the fields from MySQL database table)

for simplicity I just made the code here only HTML.. with some java..

any help please on how can I get the data of the selectedFields list to use it in a mysql statement !! ..
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

When a form is posted in PHP, the data is sent in a superglobal array called $_POST. A form field with the name of 'username' would have the data in that field sent in the $_POST['username'] variable.
coool
Forum Commoner
Posts: 45
Joined: Wed Jul 11, 2007 5:51 pm

Post by coool »

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 ?
coool
Forum Commoner
Posts: 45
Joined: Wed Jul 11, 2007 5:51 pm

Post by coool »

I solved the problem..

thanks for the help :)
Post Reply