Link to home
Start Free TrialLog in
Avatar of crescue
crescue

asked on

How to get a value from a generated PHP table

Hi, I am populating a table using php. How can I get the value of 'JOB' from the table where the user clicks the checkbox. I have tried to add a class like previously suggested, but I cannot make it work.  I am attaching my code

while($row = $result->fetch_assoc()) {
        printf("<tr><id='traba'><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td><input class='boxtocheck' type='checkbox' id='editar' name='editar' value='".e."' ><td></tr>",$row["fecha"], $row["job"],$row["tipo"],$row["valor"],$row["dinero"],$row["editar"]);
        $total=$total+$row["valor"];
        $total2=$total2+$row["dinero"];
        $jobs=$jobs+1;
    }
} else {
    echo "0 results";
}

Open in new window



<script>    
$(document).ready(function()
{
$('.boxtocheck').change(function() 
  {
    if(this.checked === true)
    {
// I am trying to see if the correct value is posted        y1 = document.getElementById('traba').value;
//                                                                                           console.log(y1);
         alert('Load page to edit');
    }
  });   
});    
</script>

Open in new window

Avatar of Swatantra Bhargava
Swatantra Bhargava
Flag of India image

Made the following changes and see the output

printf("<tr><id='traba_'".e."' '><td>%s</td><td class='job_".e."''>%s</td><td>%s</td><td>%s</td><td>%s</td><td><input class='boxtocheck' type='checkbox' id='editar' name='editar' value='".e."' ><td></tr>",$row["fecha"], $row["job"],$row["tipo"],$row["valor"],$row["dinero"],$row["editar"]);

<script>    
$(document).ready(function()
{
$('.boxtocheck').change(function()
  {
    if(this.checked === true)
    {
var selValue = this.value;
// I am trying to see if the correct value is posted        y1 = document.getElementById('traba'+selValue).value;
//                                                                                           console.log(y1);
         alert('Load page to edit');
    }
  });  
});    
</script> 

Open in new window

replace :
<input class='boxtocheck' type='checkbox' id='editar' name='editar' value='".e."' >

Open in new window

by :
<input class='boxtocheck' type='checkbox' id='editar' name='editar' value='".e."' data-job='" . $row["job"] . "' >

Open in new window

so :
<script>    
$(document).ready(function()
{
$('.boxtocheck').change(function() 
  {
    var job = $(this).data("job"); // you get the job
    if(this.checked === true)
    {
// I am trying to see if the correct value is posted        y1 = document.getElementById('traba').value;
//                                                                                           console.log(y1);
         alert('Load page to edit');
    }
  });   
});    
</script>

Open in new window

Avatar of crescue
crescue

ASKER

I guess I confused you by entering      value='".e."'   I left that value because I saw it when I google it on how to do it
My purpose is to get the value of 'JOB', and is always a different value based from the database

Tnx
Have you tried my solution?
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of crescue

ASKER

Great, that was exactly what I was looking for !!! Tnx for your solution Pluritechnician, but I was trying to give point to Chris Stanyon too because of his great description on the SOLUTION, but I couldn't find where to split points :-(

Tnx to BOTH experts !!!