PHP响应。第二个isset()调用不起作用

时间:2014-06-25 20:49:34

标签: php isset

我有一个简单的PHP代码,用于查询数据库并返回一个列表,其中包含一个新的 submit 按钮,该按钮将批准或不批准post

 if(isset($_POST['search'])){

while($row = mysqli_fetch_array($select)) {
  if($row['flag']==0) {
    echo "<br>";
    echo "<div class=\"dlcontainer2\">"; 
    echo "<div class=\"dlitem\">"; 
    echo "TourID: ";
    echo "<div class=\"dltourID\">{$row['tourID']}</div>"; 
    echo "Title: ";
    echo "<div class=\"dltitle\">{$row['title']}</div>"; 
    echo "City: ";
    echo "<div class=\"dlcity\">{$row['city']}</div>";
    echo "Description: ";
        echo "<div class=\"dldescription\">{$row['description']}</div>"; 
    //echo "<div class=\"dlapprove\">;
    echo "<input class=approve type='submit' name='submit' id=approve value='Approve'>"; 
    echo "</div>"; 
    echo "</div>"; 
    echo "<br>";

  }
}
}

就在下面,我再制作一个isset(),看看用户是否点击了 Approve 按钮。但它什么都不做:

if(isset($_POST['submit']) && !empty($_POST)) {
    echo "<script type='text/javascript'>alert('$message');</script>";
} else{
    echo "No true";
}

2 个答案:

答案 0 :(得分:0)

我认为你的问题不是isset()而是!empty($ _ POST)我不相信它自己的$ _POST会返回任何内容。

相反,也许可以尝试使用!empty($ _ POST ['submit'])来执行您的代码尝试的操作。

答案 1 :(得分:0)

首先,您可以使用此魔术Heredoc代替许多echo s

echo <<<"MyText"
<br>
<div class=\"dlcontainer2\">
    <div class=\"dlitem\">
        TourID: 
        <div class=\"dltourID\">{$row['tourID']}</div>
        Title: 
        <div class=\"dltitle\">{$row['title']}</div>
        City: 
        <div class=\"dlcity\">{$row['city']}</div>
        Description: 
        <div class=\"dldescription\">{$row['description']}</div>
        <div class=\"dlapprove\">
            <input class=approve type='submit' name='submit' id=approve value='Approve'>
        </div>
    </div>
<br>
MyText;

其次,您在第二个IF-Statement内使用了额外的不必要条件。

它应该是这样的:

if(isset($_POST['submit'])) { // or you can use $_POST['submit'] or !empty($_POST['submit']) or even $_POST
    echo "<script type='text/javascript'>alert('$message');</script>";
} else{
    echo "No true";
}