Web Hosting Talk







View Full Version : Javascript Problems....


adaml
12-17-2005, 06:08 PM
Hello!

Got some Javascript problems, im trying to get an array for form fields into my javascript but whenever i try it doesnt seem to work!

This is my field:
<input name="feature_attribute[]" type="text" id="feature_attribute[]" style="width: 98%" />

The form field is generate dynamically from my backend php.

My "Save" Button Javascript:
<a onClick="Save(document.feature_attribute)">Save!</a>

This is meant to just call the save function and pass in the values from the fields.

My Save function:
function Save(value)
{
alert('OK ' + value);
}


Now the javascript should bring up an alert with the values from the fields, but it doesnt work.... iv tried using value[0] - value[1] but i have no luck there either...


Does anyone have any ideas? Iv been stuck on this one for awhile now, if anyone can point me in the right direction i would be extremely grateful!

adaml
12-17-2005, 06:10 PM
When i click the Save button i get an alert box saying:

OK undefined

Dark Light
12-17-2005, 06:20 PM
Hello,

Not sure if you wanted to get the value of 'feature_attribute', but if you did, you might do something similar to this:


<script language="JavaScript" type="text/javascript">
function Save(value) {
alert('OK ' + value);
}
</script>
<form name="frm">
<input name="feature_attribute" type="text" id="feature_attribute" style="width: 98%" value="">
</form>


<a href="#" onClick="javascript:Save(document.frm.feature_attribute.value);">Save!</a>




The Alert is displaying Undefined because you are not defining what you want it to display, such as '.value'.

Hope that helps, I apoligise if it doesn't,
- Dark Light.

adaml
12-17-2005, 06:42 PM
I still get the Undefined error message! Any other ideas?

Thanks Dark Light!

GeorgeC
12-17-2005, 06:57 PM
Dark Light's example should work, and I just tested it in Firefox without problems. To acess the value of a form element, the standard way is:

<form id="myform">
<input type="text" name="mytext" />
</form>

//Javascript:
document.getElementById("myform").mytext.value

adaml
12-17-2005, 07:01 PM
Yes, but this is an array of fields not just on field

adaml
12-17-2005, 07:19 PM
What im trying to do is, allow someone to add a feature, when a feature gets added extra fields appear. These fields are for the attributes for the feature. Then when someone clicks save after filling in all the attribute fields. The javascript takes the field values and post's them to a php file. the php file then sessions the fields and then refreshs the current added feature div tag!

Does that make sense?

azizny
12-17-2005, 11:23 PM
Two things, you are missing the textbox Id identity, and also the numbers on the [] arrays:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<script>

function do_it(){
alert(document.getElementById("mytext[1]").value);
}
</script>
<body>
<form id="myform" name="myform">
<input name="mytext[1]" id="mytext[1]" type="text" value="dddd" />
<input name="mytext[2]" id="mytext[2]" type="text" value="dddd" />
</form>

<a onclick='javascript:do_it();'>sdfsd</a>



</body>
</html>




Peace,

adaml
12-18-2005, 09:08 AM
You see i need the numbers to be generated dynamically and then for the javascript to pick it up dynamically. As the names will change...

Dark Light
12-18-2005, 09:17 AM
Hello,

Not sure exactly what you mean, unless you mean using an Array to get the ID's of the inputs...


Similar to

<script>

// original code by azizny :: http://www.webhostingtalk.com/showthread.php?p=3554103


var quote= new Array(2)
quote[0]="mytext[0]";
quote[1]="mytext[1]";




function do_it(){



var x=0;
var display = "";
for (x=0; x<2; x++)
{

display = display + " " + quote[x] + ": " +document.getElementById(quote[x]).value + " - ";
//alert(display);
}

// alert(document.getElementById(FrmTxt).value);
alert(display);
}
</script>
<body>
<form id="myform" name="myform">
<input name="mytext[0]" id="mytext[0]" type="text" value="dddd" />
<input name="mytext[1]" id="mytext[1]" type="text" value="dddd" />
</form>

<a onclick='javascript:do_it();'>sdfsd</a>


If that was similar to what you meant, you could easily change that to suite your needs. Adding random numbers would be quite simple, and adding more Array values would again be easy. :)


Hope that helps, I apoligise if it doesn't,
- Dark Light.

adaml
12-18-2005, 05:22 PM
DarkLight you are write, only problem is i will not know the number of fields or the number of the field in the array. So i cannot set these values into the code! PHP Can handle them dynamically can Javascript?

Dark Light
12-18-2005, 06:16 PM
Hello,

Can you fully explain what you want to do, as I only have a small idea of what it is you want to do.
There is normally always a way of determining the number of fields and the number relating to a value in an array.

If you were to do this, how would you do it in PHP?
If it can be done in PHP, it probably can be done in JavaScript. ;)

Hope that helps,
- Dark Light.