Mithun Ds
Mithun Ds

Reputation: 131

How to delete row from mysql using php and javascript

I am trying to delete the entire row using the php code.

But before that my page has a multiple rows ended with radio button before clicking delete button we have to select anyone of the radio button, then we can click on delete.

but what i want is, i have written a java-script code that gets the radio button value and after that i want to get that javascript variable value to php code and then execute the delete command..

Here is what i have tried

code javascript

<script>
function getResults() {
    var radios = document.getElementsByName("address");    
    for (var i = 0; i < radios.length; i++) {       
        if (radios[i].checked) {
        var a = radios[i].value
            alert(a);
            break;
        }
    }
</script>

php code:

  <?php
      $_GET['a']
      mysql_connect("localhost", "tiger", "tiger") or die (mysql_error ());
      mysql_select_db("theaterdb") or die(mysql_error());
      $strSQL =("DELETE FROM theater WHERE theater_name='"$_POST["a"]"'");
      $rs = mysql_query($strSQL);
      mysql_close();
    ?>
    }

But this is not working

i even tried this also.it is also not working

<?php
    if ($_POST['submit'])
    {
      $_GET['a'];
      mysql_connect("localhost", "tiger", "tiger") or die (mysql_error ());
      mysql_select_db("theaterdb") or die(mysql_error());
      $strSQL =("DELETE FROM theater WHERE address='".$_POST["a"]."'");
      $rs = mysql_query($strSQL);
      mysql_close();
      }
    ?>  

Upvotes: 1

Views: 6652

Answers (4)

Anto
Anto

Reputation: 610

you have to send the value from html assigning the javascript variable doesn't added in GET

you have to send the value something like

<script>
function getResults() {
    var radios = document.getElementsByName("address");    
    for (var i = 0; i < radios.length; i++) {       
        if (radios[i].checked) {
            var a = radios[i].value;
            header.location.href = "yourphp.php/?a=" + a; //changed
            break;
        }
    }
</script>

Php Code

<?php
      $t_name = $_GET['a']
      mysql_connect("localhost", "tiger", "tiger") or die (mysql_error ());
      mysql_select_db("theaterdb") or die(mysql_error());
      $strSQL =("DELETE FROM theater WHERE theater_name='" . $t_name . "'"); //changed Line
      $rs = mysql_query($strSQL);
      mysql_close();
    ?>

Upvotes: 1

Bharathi
Bharathi

Reputation: 539

You must send the where condition value from js into delete query..

<script>
    function getResults() {
        var radios = document.getElementsByName("address");    
        for (var i = 0; i < radios.length; i++) {       
            if (radios[i].checked) {
            var a = radios[i].value
                alert(a);
                document.yourformname.action="yourphp.php?a="+a;
                document.yourformname.submit();
                break;
            }
        }
    </script>

Upvotes: 3

Rituraj ratan
Rituraj ratan

Reputation: 10378

in js

    <script>
        function getResults() {
            var radios = document.getElementsByName("address");    
            for (var i = 0; i < radios.length; i++) {       
                if (radios[i].checked) {
                var apost = radios[i].value
                     //send set state request
                $.ajax({
                    type: "POST",// here you can use get/post  if use get then in php use $_GET['a'] or use post then in php $_POST['a'] 

                    url: "your php file name with path",
                    data: {a:apost},
                    beforeSend: function () {



               },
                    success: function (data, textStatus, XmlHttpRequest) {
if(data==1)// see php file code you will know how i use data==1
{
    // do your action                
 }
else{
// in you php there is someyhing error
}
     },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {

    // do you action
                   }
                });

                    break;
                }
            }
        </script>  

in PHP

   <?php

          mysql_connect("localhost", "tiger", "tiger") or die (mysql_error ());
          mysql_select_db("theaterdb") or die(mysql_error());
          $strSQL =("DELETE FROM theater WHERE theater_name='".$_POST["a"]."'");
          $rs = mysql_query($strSQL);
            echo 1;  // here i use 1 to get response if this is done successfully then in ajax we get 1 in data  
          mysql_close();

        ?>

Upvotes: 3

Kashyap
Kashyap

Reputation: 391

Hey you need to append php variable as below

 $strSQL =("DELETE FROM theater WHERE theater_name='".$_POST["a"]."'");

Upvotes: 1

Related Questions