javascript - Retrieve a specific row value from table HTML and submit it to PHP -


i'm populating table database , looks :

<form name = "form" role="form" action ="php/teilnehmen.php" method="post">     <fieldset>                         <table width="100%" class="table table-striped table-bordered table-hover" id="datatables-example">                             <thead>                                 <tr>                                     <th>id</th>                                     <th>studienfach</th>                                     <th>teilnehmer/in</th>                                     <th>teilnehmen</th>                                 </tr>                             </thead>                             <tbody> //<?php php code.....  $i =0; $sizeofstudienfaecherseperate =  count($studienfaecherseperate); ($i; $i < $sizeofstudienfaecherseperate; $i++) {  ?>                                         <tr class="odd gradex">                                     <td ><?php echo($i+1);?></td>         <td class="studienfach"><?php echo($studienfaecherseperate[$i]);?>  <input   type="hidden" name="param_studienfach" id="param_studienfach"   value="<?php echo($studienfaecherseperate[$i]);?>"> </input>         </td>                                     <td ><?php echo($teilnehmer[$i]);?></td>                                     <td width="10%">          <?php if ($teilnahmestatus[$i] =="0"){ ?>  <button type="submit" class="btn btn-success use-address"   name="teilnehmern"id="teilnehmen">teilnehmen</button>  <?php }else{?>  <button type="submit" class="btn btn-danger use-address" name="teilnahme-beenden"   id="teilnahme-beenden">teilnahme beenden</button>  <?php }?>                                     </td>                                 </tr>                                 <?php } ?>                         </tbody>                         </table>       </fieldset>                  <!-- /.table-responsive --> 

the table shown great, , problem when try submit second column value "param_studienfach" of specific row php webservice. gives me last value. know because i'm using same id in every row overwritten. tried using javascript return value of clicked row other questions in forum didn't work me. i'm using bootstrap table if helps.

edit 1 :

thanks @taplar answer managed find solution problem. used javascript retrieve data , ajax send post request. code used :

$(".use-address").click(function() {  var item = $(this).closest("tr")   // finds closest row <tr>                     .find(".studienfach")     // gets descendent class="nr"                    .text();         // retrieves text within <td> $.ajax({         type: "post",         datatype: "json",         url: "php/teilnehmen.php",         data: {param_studienfach:item},         success: function(data){             alert(item);         },         error: function(e){             console.log(e.message);         } });  }); 

my problem in alert "item" shows correctly in database saved following example :

item = (shows in alert a)

item = \n (it's saved in database spaces afeter \n)

i tried trim item before sending got same result

to item sent ajax i'm using line of code in :

$studienfach = null;  if(isset($_post['param_studienfach']))     $studienfach = $mysqli->real_escape_string($_post['param_studienfach']); 

edit 2:

i managed solve second problem doing :

$pos= strpos($studienfach, "\\"); $studienfachtemp = substr($studienfach, 0,$pos); trim($studienfachtemp); 

if there more elegent or correct way ! please post ! thank all.

<elem1>     <elem2 class="getme"></elem2>     <elem3></elem3> </elem1> 

quick contextual lookup reference. have click event bound on 'elem3' on page. when click want associated 'elem2', not of them. class can contextually element doing...

//'this' being elem3 clicked $(this).closest('elem1').find('.getme'); 

from element clicked, find shared 'elem1' parent of both 'elem2' , 'elem3' , find '.getme' belongs parent.

more reading material: http://learn.jquery.com/using-jquery-core/working-with-selections/


Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -