skip to Main Content

I have a PHP page that shows me data from the database. I’m inserting a button that, if pressed, deletes the record. The problem is that if I push the button the action is done on all the records, because I can’t uniquely identify the click on the button

some code

foreach ( $associati as $associato ) {
    
    echo "<hr />";
    echo "Nome associato : ".$associato->Nome."</br>"; 
    echo "Codice Fiscale : ".$associato->CF."</br>"; 
    echo "<hr />";
    
        if(isset($_POST["numerazione"])){ 
                 echo "Hello world"; //Query for delete 
                  }
    ?>
    
    <form method="POST" action="">
           <input type="submit"
               name="numerazione" 
               value="Elimina utente" 
               onclick="return confirm('Are you sure?')"
       
       />
         </form>
         <?php
    
}

How can I do to uniquely identify the button?

3

Answers


  1. Chosen as BEST ANSWER

    Solution for this problem:

    if(isset($_POST["mod"]) and $_POST["id"]== $associato->ID ){ 
         echo "Hello world";
        
        
        }
    
    ?>
    
    
    <form method="POST" action="">
       <input type="hidden" name="id" value=<?php echo $associato->ID; ?>>
       <input type="submit"
           name="mod" 
           value="valore" 
           onclick="return confirm('Are you sure?')"
    
        />
    </form>
    

  2. Pass the unique information (e.g. $id or $associato->id or whatever the variable which can identify the record) when the form is submitted

    <form method="POST" action="">
    <input type=hidden name=id value=<?php echo $id; ?>>
           <input type="submit"
               name="numerazione" 
               value="Elimina utente" 
               onclick="return confirm('Are you sure?')"
       
       />
         </form>
    
    Login or Signup to reply.
  3. You can add a hidden field to each form that contains the unique identifier of the data, that means when you click the button, it will create a POST request, and in that POST request you can get the ID of the clicked record by doing $_POST[‘unique-id’], also make sure to populate the value of that hidden field using PHP

    <?php
    foreach ( $associati as $associato ) {
        
        echo "<hr />";
        echo "Nome associato : ".$associato->Nome."</br>"; 
        echo "Codice Fiscale : ".$associato->CF."</br>"; 
        echo "<hr />";
        
            if(isset($_POST["numerazione"])){ 
                $numerazione = $_POST["unique-id"];
                echo "Unique record is : ".$numerazione."</br>";
            }
        ?>
        
        <form method="POST" action="">
        <input type="hidden" name="unique-id" value="<?php echo $associato->CF; ?>" />
               <input type="submit"
                   name="numerazione" 
                   value="Elimina utente" 
                   onclick="return confirm('Are you sure?')"
           
           />
             </form>
             <?php
        
    }
    ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search