lundi 18 février 2019

how to handle several submits when they ar not part of a form

For reading values of multiple checkboxes with several submit options, i use this code:

<form action="" method="post">
   <input type="checkbox" name="check_list[]" value="C/C++"><label>C/C++</label><br/>
   <input type="checkbox" name="check_list[]" value="Java"><label>Java</label><br/>
   <input type="checkbox" name="check_list[]" value="PHP"><label>PHP</label><br/>
   <input type="submit" name="delete" value="Delete"/>
   <input type="submit" name="move" value="Move"/>
   <input type="submit" name="copy" value="Copy"/>

</form>

Each submit should do a different action and my php looks like this:

if($_POST['delete']) {
  if(isset($_POST['check_list'])){//to run PHP script on submit
    if(!empty($_POST['check_list'])){
    // Loop to store and display values of individual checked checkbox.
        foreach($_POST['check_list'] as $selected){
        echo $selected."</br>";
        }
        // code for delete goes here
        echo 'Files are  deleted!';
    }
  }
}

if($_POST['move']) {
  if(isset($_POST['check_list'])){//to run PHP script on submit
    if(!empty($_POST['check_list'])){
    // Loop to store and display values of individual checked checkbox.
        foreach($_POST['check_list'] as $selected){
        echo $selected."</br>";
        }
        //code for moving files goes here
        echo 'Files are moved!';
    }
  }
}

if($_POST['copy']) {
  if(isset($_POST['check_list'])){//to run PHP script on submit
    if(!empty($_POST['check_list'])){
    // Loop to store and display values of individual checked checkbox.
        foreach($_POST['check_list'] as $selected){
        echo $selected."</br>";
        }
        // code for copy goes here
        echo 'Files are copied!';
    }
  }
}

This works fine for me. What i want to achieve: I want to put the submits on a complete different place on the website. Something like below:

<form action="" method="post">
   <input type="checkbox" name="check_list[]" value="C/C++"><label>C/C++</label><br/>
   <input type="checkbox" name="check_list[]" value="Java"><label>Java</label><br/>
   <input type="checkbox" name="check_list[]" value="PHP"><label>PHP</label><br/>
</form>

<!-- some code goes here -->

<input type="submit" name="delete" value="Delete"/>
<input type="submit" name="move" value="Move"/>
<input type="submit" name="copy" value="Copy"/>

How can i make this work?

By the way: i am using ajax for the post action




Aucun commentaire:

Enregistrer un commentaire