dongqian1893 2016-12-20 20:48
浏览 29
已采纳

通过将其从javascript发送到php页面来删除所选行

i didn't find a similar post, i am trying to post an array from a javascript to a php page called delete.php, i want to delete a row whene clicking on the delete button situated in the last column Which is displayed by bye the php page select like this .

  while($row = mysqli_fetch_array($result))
 { 
 ?>
 <tr>
  <td><?php echo $row['code'] ;?></td>
  <td><?php echo $row['designiation'] ;?></td>
  <td><?php echo $row['diametre'] ;?></td>
  <td><?php echo $row['epaisseur'] ;?></td>
  <td><?php echo $row['prix'] ;?></td>
  <td><?php echo $row['etat'] ;?></td>
  <td><?php echo $row['id'] ;?><!--style="display:none;"-->

  <td><button type="button" class="btn btn-success btn-edit">Modifier     </button> | 
      <button type="button" onclick="pdelete();" class="btn btn-danger btn-delete">Supprimer  </button>
  </td>

 </tr>

as you see here the function pdelete() is on the onclick of the button and the id is btn-delete so i did like this

  function pdelete(){

    var _id = $('input[name="idrow"]').val();
    var _type = $('#typeid').val();
    var _pression = $('#pressionid').val();   
    var _code = $('input[name="code"]').val();
    var _designiation = $('input[name="designiation"]').val();
    var _diametre = $('input[name="diametre"]').val();
    var _epaisseur = $('input[name="epaisseur"]').val();
    var _prix = $('input[name="prix"]').val();
    var _etat = $('input[name="etat"]').val();

    $.post('delete.php', {postid:_id, posttype:_type, 
                          postpression:_pression, postcode:_code, 
                          postdesigniation:_designiation, 
                          postdiametre:_diametre, 
                          postepaisseur:_epaisseur, postprix:_prix, 
                          postetat:_etat}, 
          function(data){
            $('tbody').append(data);
          });
    }

    $(document).on('click','.btn-delete', function(){

        _trEdit = $(this).closest('tr');
        var _id = $(_trEdit).find('td:eq(6)').text();
        $('input[name="idrow"]').val(_id);

        if(confirm("Vous etes sur de supprimer cette ligne?")){
            $(this).closest('tr').remove(); 
            alert("suppression réussite .");
        }
});

the page delete.php is like this

$id_tube = $_POST['postid'];
$type_utilisation = $_POST['posttype'];
$pression_tube = $_POST['postpression'];
$code_tube = $_POST['postcode'];
$designiation_tube =$_POST['postdesigniation'];
$diametre_tube = $_POST['postdiametre'];
$epaisseur_tube = $_POST['postepaisseur'];                       
$prix_tube =$_POST['postprix'];
$etat_tube = $_POST['postetat'];


if(isset($pression_tube)){
    if($pression_tube=="pn4"){
        if($type_utilisation=="pehdtelecom" && $pression_tube=="pn4" ){ echo "verifier le type du Tube S.v.p";}else{

            $sql="DELETE from  pn4  where id='".$id_tube."'";    
            echo"$sql";  
            $result = mysqli_query($link, $sql);

            // echo"$id_tube";
            if($result){
                // do something here 

            } else{
                echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
            }
    }
}

it doesnt find the id and it did not pass through post. to put it in the condition of the sql request in the delete.php page , i have tried almost evrything a i found i am out of idea please help me to solve it to delete the row from the database also when clicking the delete button in the last column.

  • 写回答

1条回答 默认 最新

  • dongtan8122 2016-12-20 20:56
    关注

    First of all you don't need to send all this data to delete a row since you are going to use the "postid"

    Then I don't see where is you "input[name="idrow"]" on your html.

    What I suggest you to do is on the click funciton to insert the post id.

      while($row = mysqli_fetch_array($result))
     { 
     ?>
     <tr>
      <td><?php echo $row['code'] ;?></td>
      <td><?php echo $row['designiation'] ;?></td>
      <td><?php echo $row['diametre'] ;?></td>
      <td><?php echo $row['epaisseur'] ;?></td>
      <td><?php echo $row['prix'] ;?></td>
      <td><?php echo $row['etat'] ;?></td>
      <td style="display:none;"><?php echo $row['id'] ;?></td>
    
      <td><button type="button" class="btn btn-success btn-edit">Modifier     </button> | 
          <button type="button" onclick="pdelete(<?php echo $row['id']; ?> );" class="btn btn-danger btn-delete">Supprimer  </button>
      </td>
    
     </tr> <?php } ?>
    

    Then when you call the function you just get the id from it as

    function pdelete(postid){
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)