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

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;
    }

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.