0

Hello friends.

I have a php form and using ajax/javascript function to do get request.

In form, there is a field name and checkbox, and In java script I have to get the fied name, check if the check box is clicked or not and add the paramater to url arguments, if checked.

echo "<tr><td>".$data[$j]['sname']."</td><td><input type=\"hidden\" name=\"field[$j]\" value=\"".$data[$j]['sname']."\"> <input type=\"checkbox\" id=\"val[$j]\"></input> </td>";

I dont know how many field will I have in my code, So i'm generating runtime name like field[0], field[1], and I'm expecting the same to read from java script like this.

var i=0;
        var arg="update=1&";
        while(document.getElementById(field[$i]) != NULL) {
                var chk = document.getElementById(val[$i])
                if( chk == 1)
                        arg+=arg+document.getElementById(field[$i])+"=1&";
                i=i+1;
        }

But things are not working as I would have wanted.
Javascript shows error that it can not read 'field'. I dont know why it goes to read 'field' and not 'field[0]',etc.

I may be doing something wrong in my PHP code or my java script.

Can anyone suggest what could be wrong?

Hemanshu

1
Contributor
3
Replies
16
Views
4 Years
Discussion Span
Last Post by hemanshurpatel
0
Edit:

    var i=0;
    var arg="update=1&";
    while(document.getElementById(field[$i]) != NULL) {
    var chk = document.getElementById(val[$i])
    if( chk == 1)
    arg=arg+document.getElementById(field[$i])+"=1&";
    i=i+1;
    }
0

I have done some changes.
In php form

echo "<tr><td>".$data[$j]['sname']."</td><td><input type=\"hidden\" name=\"field-$j\" value=\"".$data[$j]['sname']."\"> <input type=\"checkbox\" id=\"val-$j\"></input> </td>";

in Java script

        var obj=document.getElementById(objID);
        var i=0;
        var arg="update=1&";
        while(document.getElementById('field-'[i])) {
                var chk = document.getElementById('val-'[i])
                alery(chk);
                if( chk == 1)
                        arg+=arg+document.getElementById('field-'[i])+"=1&";
                i=i+1;
        }
        alert(arg);
        alert(obj.style.display);

arg is just update=1&.
Code is not going inside even though field-0,field-1 is set.

0

When I try to do following

alery(myform.field0.value)

myform, is the name of my form, and it work great.
But i dont know how many these firle0, field1,field2,etc will be.
thats determined runtime.

Can anyone help me in reading them in javascript?

I mean i have to keep reading myform.field0,myform.field1, myform.field2, etc variables, till as far as i can go

Hemanshu

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.