0

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.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
salah tabet
  • 119
  • 1
  • 1
  • 10
  • Did you print what are you sending as "postid" ? – Ideal Bakija Dec 20 '16 at 20:49
  • yes i did prit the sql request it shows : DELETE from pn4 where id='' no id . – salah tabet Dec 20 '16 at 20:53
  • With what you have posted here, it will never find an ID. `var _id = $('input[name="idrow"]').val();` <-- that is looking for `` – CptMisery Dec 20 '16 at 20:54
  • 1
    Your script is at risk of [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Have a look at what happened to [Little Bobby Tables](http://bobby-tables.com/) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly Dec 20 '16 at 20:54
  • 1
    You dont have an input field with the name of `idrow` `$('input[name="idrow"]')` `` columns in a table are not `` fields – RiggsFolly Dec 20 '16 at 20:56

1 Answers1

0

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){

}
Ideal Bakija
  • 629
  • 5
  • 14
  • the onclick function is called from anther file select.php where is the while how drow the table . how should i define $postid, forgive me i am new to javascript and php – salah tabet Dec 20 '16 at 21:05
  • Can you now understand the solution above ?! – Ideal Bakija Dec 20 '16 at 21:19
  • you are a genuis it works, but i still have an issu, i dont want to show the of th id so whene i put display:none on the style, it will not word so the id will get the value of the Action buttons ! what should i do please – salah tabet Dec 20 '16 at 21:30
  • Check out the new code and check out on some tutorial how to do these things. – Ideal Bakija Dec 20 '16 at 21:40
  • i am very sorry to disturb, but you seems understanding my problem well, whene i add a new row and willing to delete this row it wont be, becuse the last colum will not be the id but the action buttons, the table should be refreshed when adding to select again the data or there is another way ! – salah tabet Dec 20 '16 at 22:20
  • Salah please can you be more clear. Tell me what you want to do. I will try to helt you – Ideal Bakija Dec 20 '16 at 22:28
  • now it work nicely but under my table i have an inputs that add a new row to the table who have the buttons update and delete in the last column, when i delete at firs any row it works normal, but when i add a new row with my inputs the new row added whene i am trying to delete it, it wont be because the of the id of the new row wont be filled so whene i want to delete it wont reconize the new row – salah tabet Dec 20 '16 at 22:38
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/131097/discussion-between-ideal-bakija-and-salah-2019). – Ideal Bakija Dec 20 '16 at 22:40
  • i am in the discussion chat – salah tabet Dec 20 '16 at 22:52