-8
  1. html.php

how to call Delete($passid) function in button on Click.

    <?php
    function Delete($passid)
        {
            echo "Delete $passid";
            }   
    ?>



    <button name="Delete" id="Delete" onclick="Delete($row['ID'])" >Delete</button>
Tulsi
  • 151
  • 3
  • 15

4 Answers4

1

I am assuming this is what you're after? - Assuming you're trying to edit the DOM using javascript

JS

function Delete(passid)
{
    //Do something with passid variable
}

HTML

<button id="Delete" onclick="Delete(this.id)" >Delete</button>

(also removed name since you don't seem to be using it)

So when the button is clicked, it calls the javascript function "Delete" and passes the button's id to it (in this case "Delete")



If not, perhaps this? - Assuming you are trying to delete a row from a mysql database and you're sending the variable from your HTML to your php on the button's click

Assuming you have a database connection setup, and that there exists a table named 'table' that meets some 'condition' to get all rows matching this criteria's 'id' column value

connection setup should be in all php files that link to the database

(You can use the include function for this instead of writing it 100 times)

i.e. include("path/to/file/connection.php")

PHP populates HTML

<?php
    $query = mysql_query("SELECT * FROM table WHERE condition");
    while($row = mysql_fetch_array($query))
    {
        echo '<span>'.$row["id"].'</span>
              <button class="Delete" value='.$row["id"].'>Delete</button>';
    }
?>

JQuery

$(".Delete").click(function()
{
    var id = $(this).attr("id");
    $.ajax({
        type:"POST",
        url:"file.php",
        data:{id:id}
    });
});

PHP ---> file.php runs function

<?php
    if(isset($_POST["id"]))
    {
        $id = $_POST["id"];
        $query = mysql_query("DELETE FROM table WHERE id='$id'");
    }
?>

This would delete the database table row where id is set to the id that we pass it from the HTML button's value.

So this function does the following:

  1. Gets all ids from database table where the condition is met
  2. Populates the HTML page with a span tag showing us the id of the element next to the button that will delete the same element
  3. When a button is clicked, our jQuery click event captures it
  4. jQuery function gets clicked button's id and sends it to the ajax function
  5. Ajax function uses the post method to send the variable id to the document file.php
  6. file.php checks to see whether or not the variable id that was sent through the post method actually exists
  7. If the post variable id exists, it sets $id to it.
  8. Query called to delete a table row in our database where id is equal to $id (our initial button's id value generated by the table itself)
ctwheels
  • 21,901
  • 9
  • 42
  • 77
0
  1. You must use a form and submit button to POST data, then call this function with parameter from $_POST['passid'].

  2. Use a AJAX post data and process same option 1

Nho Huynh
  • 674
  • 6
  • 13
0
<form action="" method="post">
    <input type="hidden" name="delID"> <?php echo $row['ID']; ?>
    <input type="submit" value>
</form>
<?php
if($_POST)
{
 $id=$_POST['delID'];
  function delete($id)
  {
    echo $id;
  }
}
?>

else use ajax --->

0
<input type="button" onclick="Delete($row['ID'])" name="delete" value="Log In" />
<script>
function Delete(e) {
alert(e);
$.ajax({
    type: "POST",
    url: "script.php",
    data:{"delete":e},
    success: function(data) {
    if (data) {

       alert(data);
    }
    else {
        alert('Successfully not posted.');
    }
    }
   });
  }
</script>

in php

if(isset($_POST))
{
 $id=$_POST['delete'];
 echo "Delete ".$id;
 } 
Priyank
  • 3,778
  • 3
  • 29
  • 48