1

我正在尝试使用 php 代码删除整行。

但在此之前,我的页面有多行以单选按钮结尾,然后单击删除按钮,我们必须选择任何单选按钮,然后我们可以单击删除。

但我想要的是,我编写了一个获取单选按钮值的 java-script 代码,之后我想将该 javascript 变量值获取到 php 代码,然后执行删除命令..

这是我尝试过的

代码 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代码:

  <?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();
    ?>
    }

但这不起作用

我什至也试过这个。它也不起作用

<?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();
      }
    ?>  
4

4 回答 4

3

在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>  

在 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();

        ?>
于 2013-08-21T05:00:11.503 回答
3

您必须将 js 中的 where 条件值发送到删除查询中。

<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>
于 2013-08-21T05:05:34.237 回答
1

嘿,您需要附加 php 变量,如下所示

 $strSQL =("DELETE FROM theater WHERE theater_name='".$_POST["a"]."'");
于 2013-08-21T04:40:19.193 回答
1

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();
    ?>
于 2013-08-21T04:50:37.993 回答