douchun5969 2014-02-25 10:24
浏览 24
已采纳

复选框以随机方式删除列表

I have a list of favorite cars which i have added to each favorite car a checkbox for letting the user to remove the favorite car from his favorite car list. The problem is that the checkbox is working in a different way: If I check any car (1st, second.. last or multiple cars) and after hit submit the car that will get removed is the last one added instead of removing the selected one. If I check multiple cars, happens same thing, removes only the last car added.

PHP

public function GetFavoriteCars() {
  include("inc/membersite_config.php");
  $email = $fgmembersite->UserEmail(); // this is how I take the e-mail of the 
  global $base_path;
  $FavoriteCars = $this->QueryResult("SELECT * FROM favoritecar WHERE email='$email'"); 
    if (count($FavoriteCars)) {
        $mystring='http://';
    echo '<form action="" class="deletebutton" method="post">';
    echo '<input type="submit" name="deletebtn" id="deletebtn" value="Submit">';
    echo '<div class="roster_slideri-login">';
        foreach ($FavoriteCars as $FavoriteCar) {
      $carlink = $FavoriteCar->favoritecarlink;
      echo '<div class="car-info-col-login">';
      echo '<input type="checkbox" name="checkbox" value="'.$carlink.'" class="checkbox-login">';
      $val=strpos($FavoriteCar->favoritecarimg,$mystring);
    if ($val !== false) {
      if($FavoriteCar->favoritecarimg!='') {
                echo '<a href="'.$base_path.'detail-page_'.$FavoriteCar->favoritecarlink.'">';
                echo '<img src="'.$FavoriteCar->favoritecarimg.'" alt="'.$FavoriteCar->favoritecartitle.'" width="160" height="120" />';
        echo '</a>';
                echo '<div class="name">'.substr($FavoriteCar->favoritecartitle,0,20).'</div>';
        echo '</div>'; //car-info-col-login
              }
            } else {
    echo '<a href="'.$base_path.'detail-page_'.$FavoriteCar->favoritecarlink.'">';
            echo '<img src="'.$base_path.'uploads/no-img.jpg" alt="'.$FavoriteCar->favoritecartitle.'" width="160" height="120" />';
    echo '</a>';
            echo '<div class="name">'.substr($FavoriteCar->favoritecartitle,0,20).'</div>';
            echo '</div>'; 
            } 
        }
    echo '</form>';
        if (isset($_POST["checkbox"])) {
      $this->QueryResult("DELETE from favoritecar WHERE email='$email' AND favoritecarlink='$carlink'");
      echo '<script type="text/javascript">alert("Car had been deleted");</script>';
        }
      echo '</div>'; // div roster_slideri-login
    }
}

Explaning:

$email = $fgmembersite->UserEmail(); - this is how I take the e-mail of the current logged in user. It will echo "email_of_logged_in_user@domain.com"

QueryResult is a custom function that looks like this. I usually use it for SELECTING purposes but it seams that is working for deleting purposes too.

abstract class DBDetails {

    protected $link = NULL;

    protected function connector() {
        global $DBHOSTNAME;
        global $DBUSERNAME;
        global $DBPASSWORD;
        global $DBNAME;
        $this->link = mysqli_connect($DBHOSTNAME, $DBUSERNAME, $DBPASSWORD, $DBNAME) or die("Can't connect to MySQL server on localhost");
    }

      protected function close() {
        mysqli_close($this->link);
      }
    }

     abstract class N2 extends DBDetails {
      public function QueryResult($strQuery) {
        $this->connector();
        $query = mysqli_query($this->link, $strQuery);
        $arr = array();
        if ($query) {
            while ($result = mysqli_fetch_object($query)) {
                array_push($arr, $result);
            }
        }
        $this->close();
        return $arr;
      }   
    }

Expected output

When I check the checkbox of a car, it should delete only that car. If I check the checkboxes of multiple cars, should delete the specific cars that I checked.

Please help, I am quite a noob in checkboxes. I have checked lots of questions from here, but did not find my answer.

  • 写回答

1条回答 默认 最新

  • doure5236 2014-02-25 10:50
    关注

    In this line :

      echo '<input type="checkbox" name="checkbox" value="'.$carlink.'" class="checkbox-login">';
                                   --------------
    

    When using multiple checkboxes with same name , you would need to include [] in the name :

      echo '<input type="checkbox" name="checkbox[]" value="'.$carlink.'" class="checkbox-login">';
                                   ----------------
    

    Then $_POST["checkbox"] will be an array and you can use foreach on it to get all the checked values .

    if( isset( $_POST["checkbox"] ) )
    {
        foreach( $_POST["checkbox"] as $value )
        {
            /* $value contains $carlink */
    
            echo $value;    // For test purpose
    
            /* Sanitize and use it to identify and delete the corresponding row */
        }
    }
    

    ( Rather than name="checkbox[]" it might be better to choose another name . )

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教