Javascript functions and php foreach values

Javascript coding ..

Moderators: macek, egami, gesf

Post Reply
User avatar
Nullsig
php-forum Fan User
php-forum Fan User
Posts: 981
Joined: Thu Feb 17, 2011 6:52 am
Location: Racine, WI

Re: Javascript functions and php foreach values

Post by Nullsig » Tue May 08, 2012 6:55 am

The problem with your code is that you have the same ID for all of your check boxes, so only the first occurrence of that ID is being read by your JavaScript function.

Also, I have no idea what you are doing with the value of "check".

You need to change this portion of your foreach loop:

Code: Select all

echo "<td class='b'><input type='checkbox' id='chk_Bx' value=". $addrss_owner. " onClick='selectFunction()'></input></td>";
To this:

Code: Select all

echo "<td class='b'><input type='checkbox' value=". $addrss_owner. " onClick='selectFunction(this)'></input></td>";


Then you can change your JS function to this:

Code: Select all

function selectFunction(item){
var check = item.checked;
var val = item.value;
alert(val);
}

User avatar
freshnet
php-forum Active User
php-forum Active User
Posts: 359
Joined: Tue Feb 22, 2011 8:19 am
Location: Canada

Re: Javascript functions and php foreach values

Post by freshnet » Tue May 08, 2012 8:09 am

agreed, ids for elements should be unique to the page. If it's non-unique, use a class. Otherwise stick an incrementing value into your id if you have to use one.

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests