Welcome to WebmasterWorld Guest from 18.206.15.215

Forum Moderators: open

Message Too Old, No Replies

Javascript update field from dynamic array

using php/javascript to build custom db query

     
9:29 pm on Jul 17, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:May 31, 2006
posts:116
votes: 0


Hello all,

I've been working on a script that will create an SQL statement for dynamic reports, and have hit a bit of a snag. To keep it simple, the basic premise is that I pull all of the available columns and their data types from the database, list them as selectable options, and then add some fields to narrow down selections based on data types.

For example, let's say that my DB has 3 fields that hold boolean data (true/false... actually 1/0). Their datatype represents that fact, so for each of them I add a row of fields to my form that allows the user to select true or false. Each row is dynamic, the number of boolean datatypes will change depending on the DB I'm reporting on. True or false is in a dropdown, and looks like this:

Return Records Where:
something is true
something_else is false
the_last_thing is true

At the end of each row I have a button that allows the user to add that qualifier to their query.

It's generated in php like this:

foreach($boolarray as $thiscolumn)
{
echo '<tr><td>'.$thiscolumn[name].' is </td><td><select name="bools['.$c.']">';
echo '<option value="1">True</option>';
echo '<option value="0">False</option>';
echo '</select></td>';
echo '<input type="hidden" name="boolfield['.$c.']" value="'.$thiscolumn[name].'" />';
echo '<td><input type="button" name="booler" value="Add Qualifier" onClick="addQualifier('.$c.')"></tr>';
$c++;
}

Now, what I need JavaScript to do for me is add a line to a textarea each time someone clicks the button. In this way, the query is dynamically built and displayed for the user to verify.

Here's a little bit of what I have in my addQualifier().
(the whole thing is pretty big and handles most of the form)

where = where + " " + document.myform.boolfield[boolnum].value
+ " = " + document.myform.bools[boolnum].value;
document.myform.whereclause.value = where; // Populates the textarea

Obviously this isn't working... I get a document.myform.boolfield[boolnum] has no properties, so I'm guessing that's not the way to reference a dynamically built form.

I suppose I *could* use element[i] for this, but it seems awful cumbersome.

Any ideas?

10:03 pm on July 17, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 15, 2004
posts:2047
votes: 0


That's because the name of the input includes the square brackets.

<input name="blah[]" ..>

Is referenced like this, because the [] is illegal using "dot" notation

formRef.elements["blah[]"]

If there is more than one such named element, then a collection is returned, so:

formRef.elements["blah[]"][n]

It looks like you are putting something inside the brackets in the input's name. I don't understand the purpose behind that. Is it a PHP thing?

10:57 pm on July 17, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:May 31, 2006
posts:116
votes: 0


I was doing that in order to try to give JavaScript something to reference. I passed it along to the function, thinking I could use the brackets in dot notation. Seems I can't.

I didn't realize I could have a collection of form items and reference them that way through elements. That should work.

formRef.elements["bools"][n]

Kinda solves the whole problem if I can do it that way. I'll just get rid of the brackets and $c variable entirely. Thanks. :)