...

View Full Version : passing php value to javascript error



madolia
12-21-2006, 07:43 AM
I have a big problem.

This bit of chunk creates text boxes based on user input.

for($i=0;$i<$cols;$i++)
{
echo" <td align=center width=20%>
<input type = text name=id[$i]>
</td>
";

However, when I want to use javascript, it says id[i] is not an object.

function addList()
{
var cols = <?php echo $cols; ?>;

for (i=0;i<cols;i++)
{
if (document.form.id[i].value == "")
{
document.form.id[i].value = document.form.showID.value;
break;
}
}
}

TripperTreats
12-21-2006, 12:16 PM
I think the problem is that in your php code, [$i] is interpreted as text. Try this instead:


echo "<td align=center width=20%>
<input type='text' name='id".[$i].">
</td>";

Does that work?

TripperTreats
12-21-2006, 12:18 PM
Sorry, I mean this;


echo "<td align=center width=20%>
<input type='text' name='id[".$i."]>
</td>";

madolia
12-22-2006, 02:30 AM
no it doesn't work.

What else should i do?

TripperTreats
12-22-2006, 02:57 AM
I think I've got it. The notation [i] usually refers to an array, so that when you use javascript to call document.form.id[i], it might be looking for the input called "id" in form, and then complaining that this isn't an array, so that you can't talk about document.form.id[46] or any other number you can think of.

You might try giving each input an id (as opposed to a name). Thus, try naming the inputs differently, like


echo "<td align=center width=20%>
<input type='text' id='hopethisworks".$i.">
</td>";Now their id's are hopethisworks1, hopethisworks2, etc. In your javascript code you can now try something like:


for (i=0;i<cols;i++) {
var thisInput = document.getElementById('hopethisworks'+i);
if (thisInput.value == "") {
thisInput.value = document.form.showID.value;
break;
}
}Let me know whether this works, and if not, what errors you get.

Arbitrator
12-22-2006, 03:06 AM
Maybe you should try these code sets?

for ($i = 0; $i < $cols; $i++) {
echo "<td>";
echo " <input type=\"text\" id=\"id" . $i . "\">";
echo "</td>";


function addList() {
var cols = <?php echo $cols; ?>;
for (var i = 0; i < cols; i++) {
if (!document.getElementById("id" + i).value) {
document.getElementById("id" + i).value = …;
break;
}
}
}
Notes:

I don’t know PHP, but I believe that the above PHP is correct.
Removed the outdated form‐based element referencing method in favor of the getElementById() referencing method. Changed the name attribute to the id attribute accordingly.
Removed deprecated attributes from table cell element. Those properties should be defined via CSS.
Declared the variable “i”.
Used a better method to test whether a value exists.
Added double quotation marks around attribute values as per good practice.
Replaced part of the JavaScript with an ellipsis since I don’t know what that part is referencing.


TripperTreats beat me to the post.

madolia
12-22-2006, 04:40 AM
Hi ! It worked.

but then, since i am not using "name" but use "id" instead, how do I identify the variables when I click next page?

what i mean is that if i use the name="test", on my next page, the variable is known as $test.

GJay
12-22-2006, 09:20 AM
you keep the name for referencing when it's submitted, but use the id for the javascript side of things.
Form inputs need ids so that the 'for' attribute of labels can be understood anyway, so it's good practice from a usability perspective.

Arbitrator
12-22-2006, 10:17 PM
Yeah, you can have the name attribute at the same time as the id attribute. Just make sure that they’re identical.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum