Javascript functions and php foreach values

Javascript coding ..

Moderators: macek, egami, gesf

Pavilion
New php-forum User
New php-forum User
Posts: 2
Joined: Tue May 08, 2012 5:00 am

Javascript functions and php foreach values

Postby Pavilion » Tue May 08, 2012 5:05 am

Hello Folks:

I'm new on board and hoping you can help me with a problem. I've got an html table populated by a php foreach routine. One field in this table is an Input type=checkbox. The goal is to run a javascript function when the user checks the checkbox. But, since the checkbox is in a table populated by a foreach routine, I'm having some problems. Below is the applicable code for my table:

Code: Select all

<?php
foreach($results as $row)
{
        $bk_owner = $row['BookOwner'];
        $addrss_owner = $row['AddressOwner'];
        $f_name = $row['FName'];
        $l_name = $row['LName'];
        $email = $row['EmailAddress'];
        $d_phone = $row['DeskPhone'];
        $ext = $row['Ext'];
        $cell = $row['CellPhone'];
        $title = $row['Title'];

        echo "<tr>";
                echo "<td class='a'>".$l_name ."</td>";
                echo "<td class ='a'>".$f_name ."</td>";
                echo "<td class ='c'>".$email ."</td>";
                echo "<td class ='a'>".$d_phone ."</td>";
                echo "<td class='b'>".$ext."</td>";
                echo "<td class='a'>".$cell."</td>";
                echo "<td class='c'>".$title."</td>";
                echo "<td class='b'><input type='checkbox' id='chk_Bx' value=". $addrss_owner. " onClick='selectFunction()'></input></td>";
        echo "</tr>";
}
echo"</tbody>";
?>



Specifically, note this line:

Code: Select all

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


the value is set to $address_owner. This value is a primary key value and will allow me to run routines related to the record chosen when the checkbox is checked.

Following is the javascript code for selectFunction():

Code: Select all

function selectFunction(){
var check = document.getElementById("chk_Bx").checked;
var val = document.getElementById("chk_Bx").value;
alert(val);
}


At this point the javascript is very simple. All I'm trying to do is pick up applicable values for the record selected. But - I believe because the table is populated through a foreach loop, the javascript alert is returning the same primary key value for every record checked (or unchecked).

Also, if I alert out check, it continually returns false, even if I've checked the box and it should read true. I am assuming this is also related to the foreach table presentation.

How do I grab ONLY the primary key value, or checked true/false for the applicable record?

Thanks in advance:

Pavilion

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

Postby 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: 283
Joined: Tue Feb 22, 2011 8:19 am
Location: Canada

Re: Javascript functions and php foreach values

Postby 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.


Return to “JavaScript”

Who is online

Users browsing this forum: No registered users and 1 guest