Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trouble with brackets in arrays..

Status
Not open for further replies.

dreamclutch

Programmer
Oct 3, 2005
21
US
I'm having a bit of trouble with this one. I've got conflicting array brackets and was curious how to break it apart programmatically so that is functions correctly.

The problem exists at the form action below. I'm including javascript at the end of the post action so that it checks the javascript wordFilter function below. That's fine and dandy but the name fields which I'm retrieving ALSO have brackets.

Normally the onsubmit code would look like the following under the form action and would work:

onSubmit="return wordFilter('form1',['word9','word82', 'etc.'])";>

however the names that I need to use include brackets within the text field names, e.g. word[9] and word[82], etc. like below.

onSubmit="return wordFilter('form1',['word[9]','word[82]','etc[]'])";>

Thanks



PHP:
<script language="JavaScript">

var swear_words_arr=new Array("badword","badword","badword","badword","badword","badword","badword","badword","badword","badword","badword","badword");
var swear_alert_arr=new Array();
var swear_alert_count=0;

function reset_alert_count()
{
 swear_alert_count=0;
}

function wordFilter(form,fields)
{
	reset_alert_count();
	var compare_text;
	var fieldErrArr=new Array();
	var fieldErrIndex=0;
	for(var i=0; i<fields.length; i++)
	{
		eval('compare_text=document.' + form + '.' + fields[i] + '.value;');
		for(var j=0; j<swear_words_arr.length; j++)
		{
			for(var k=0; k<(compare_text.length); k++)
			{
				if(swear_words_arr[j]==compare_text.substring(k,(k+swear_words_arr[j].length)).toLowerCase())
				{
					swear_alert_arr[swear_alert_count]=compare_text.substring(k,(k+swear_words_arr[j].length));
					swear_alert_count++;
					fieldErrArr[fieldErrIndex]=i;
					fieldErrIndex++;
				}
			}
		}
	}
	var alert_text="";
	for(var k=1; k<=swear_alert_count; k++)
	{
		alert_text+="\n" + "(" + k + ")  " + swear_alert_arr[k-1];
		eval('compare_text=document.' + form + '.' + fields[fieldErrArr[0]] + '.focus();');
		eval('compare_text=document.' + form + '.' + fields[fieldErrArr[0]] + '.select();');
	}
	if(swear_alert_count>0)
	{
		alert("The form cannot be submitted.\nYou f**** cursed. Get ride of these words.:\n_______________________________\n" + alert_text + "\n_______________________________");
		return false;
	}
	else
	{
		return true;
	}
}
</script>

<?

//start of form
<form name="form1" action="/site/index.php" method="post" onSubmit="return wordFilter('form1',['word[9]','word[82]','word[146]','word[176]','word[486]','word[648]','word[805]','word[822]','word[864]','word[877]','word[931]','word[1049]','word[1237]','word[1537]','word[1711]','word[1759]','word[1769]','word[1928]','word[cbarrwentfar]'])";>

//the form fields
<tr><td>An emotion</td><td><input type="textbox" size="10" name="word[864]"></td></tr>
<tr><td>...etc. //othe form inputs with text name fields in the format of word[xxx]

//eventually the submit button
<input type="submit" name="submit">
</form>

?>
 
[tt]
[blue]<?
//rather somewhere on the server-side word is defined
$word=array("abc","def",...) //as long as required
?>[/blue]

script language="JavaScript">

var swear_words_arr=new Array("[blue]xxxx[/blue]","badword","badword","badword","badword","badword", "badword","badword","badword","badword","badword", "badword");
var swear_alert_arr=new Array();
var swear_alert_count=0;

function reset_alert_count()
{
swear_alert_count=0;
}

function wordFilter(form,fields)
{
reset_alert_count();
var compare_text;
var fieldErrArr=new Array();
var fieldErrIndex=0;
for(var i=0; i<fields.length; i++)
{
[red]//[/red]eval('compare_text=document.' + form + '.' + fields + '.value;');
[blue]compare_text=document.forms[form].elements[fields].value;[/blue]
for(var j=0; j<swear_words_arr.length; j++)
{
for(var k=0; k<(compare_text.length); k++)
{
if(swear_words_arr[j]==compare_text.substring(k,(k+swear_words_arr[j].length)).toLowerCase())
{
swear_alert_arr[swear_alert_count]=compare_text.substring(k,(k+swear_words_arr[j].length));
swear_alert_count++;
fieldErrArr[fieldErrIndex]=i;
fieldErrIndex++;
}
}
}
}
var alert_text="";
for(var k=1; k<=swear_alert_count; k++)
{
alert_text+="\n" + "(" + k + ") " + swear_alert_arr[k-1];
[blue]//why right-hand=side is a focus but assign to compare_text? so I may not understand[/blue]
[red]//[/red]eval('compare_text=document.' + form + '.' + fields[fieldErrArr[0]] + '.focus();');
[blue]document.forms[form].elements[fields[fieldErrArr[0]].focus(); //you might have to watch timing problem[/blue]
[/blue]//same ambiguity[/blue]
[red]//[/red]eval('compare_text=document.' + form + '.' + fields[fieldErrArr[0]] + '.select();');
[blue]document.forms[form].elements[fields[fieldErrArr[0]].select();[/blue]
}
if(swear_alert_count>0)
{
alert("The form cannot be submitted.\nYou fucking cursed. Get ride of these words.:\n_______________________________\n" + alert_text + "\n_______________________________");
return false;
}
else
{
return true;
}
}
</script>

<!-- [red]<?[/red] -->

//start of form
<form name="form1" action="/site/index.php" method="post" onSubmit="return wordFilter('form1',['[red]<? echo $[/red]word[9] [red]?>[/red]','[red]<? echo $[/red]word[82] [red]?>[/red]','[red]<? echo $[/red]word[146] [red]?>[/red]','[red]<? echo $[/red]word[176] [red]?>[/red]','[red]<? echo $[/red]word[486] [red]?>[/red]','[red]<? echo $[/red]word[648] [red]?>[/red]','[red]<? echo $[/red]word[805] [red]?>[/red]','[red]<? echo $[/red]word[822] [red]?>[/red]','[red]<? echo $[/red]word[864] [red]?>[/red]','[red]<? echo $[/red]word[877] [red]?>[/red]','[red]<? echo $[/red]word[931] [red]?>[/red]','[red]<? echo $[/red]word[1049] [red]?>[/red]','[red]<? echo $[/red]word[1237] [red]?>[/red]','[red]<? echo $[/red]word[1537] [red]?>[/red]','[red]<? echo $[/red]word[1711] [red]?>[/red]','[red]<? echo $[/red]word[1759] [red]?>[/red]','[red]<? echo $[/red]word[1769] [red]?>[/red]','[red]<? echo $[/red]word[1928] [red]?>[/red]','[red]<? echo $[/red]word[cbarrwentfar] [red]?>[/red]'])";>

//the form fields
<tr><td>An emotion</td><td><input type="textbox" size="10" name="[red]<?php echo $[/red]word[864][red]?>[/red]"></td></tr>
<tr><td>...etc. [red]<!-- [/red]//othe form inputs with text name fields in the format of word[xxx] [red] -->[/red]

//eventually the submit button
<input type="submit" name="[red]somenameotherthansubmit[/red]">
</form>

<!-- [red]?>[/red] -->
[/tt]
ps: edit in the box, watch my typos or misunderstandings.
 
You should not use // for comments outside of script section. Replace them with <!-- -->, I have not replaced all, only one or two! Do it by yourself.
 
The actualy field names below the post action are already parsed. I'm not sure why I included php brackets around that bit of code. The field names for the text fields are parsed from a previous script...the bit of php that uses the array.

When I submit I'm getting the following javascript error.

Line 76
Char 3
document.form1.word is null or not an object.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top