如果MySQL Result Empty显示确认框

时间:2011-12-01 21:01:11

标签: php javascript mysql html

我正在使用PHP / JavaScript / HTML / MySQL的疯狂混合

$query = "SELECT * FROM faculty WHERE submitted = 0;";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
if($row != NULL) {  
   // Display a confirm box saying "Not everyone has entered a bid, continue?"
}
// If confirmed yes run more queries
// Else nothing

在完成其余查询之前,显示此确认框的最佳方法是什么?

4 个答案:

答案 0 :(得分:3)

    if($row != NULL) {  
    ?>
    <script>alert("not everyone has submitted their bid.");</script>
    <?php
    }

    <?php
    function jsalert($alert_message){
        echo "<script type='text/javascript'>alert('".$alert_message."');</script>";
    }

    if($row!=null){
        jsalert("Not everyone has submitted their bid.");
    }
    ?>

答案 1 :(得分:2)

你不能在一个连续的块中执行此操作,因为PHP的所有将在confirm之前执行(由于服务器与客户端)。

您需要将这些步骤分解为两个单独的步骤,并让客户端在它们之间进行调解:

part1.php

<?php
$query = "SELECT * FROM faculty WHERE submitted = 0;";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
if ($row != NULL) { ?>

<form id="confirmed" action="part2.php" method="post">
    <noscript>
        <label>Not everyone has entered a bid, continue?</label>
        <input type="submit" value="Yes">
    </noscript>
</form>

<script type="text/javascript">
    if (confirm("Not everyone has entered a bid, continue?")) {
        document.getElementById('confirmed').submit();
    }
</script>

<?
} else {
    include_once('part2.php');
}
?>

part2.php

<?php
    // assume confirmed. execute other queries.
?>

答案 2 :(得分:1)

$query = "SELECT * FROM faculty WHERE submitted = 0;";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);

if($row != NULL) {  
   // more queries here
} else {
   echo "<script>alert('Empty result');</script>";
}

答案 3 :(得分:0)

使用此代码进行操作,最终可以使用它。我知道你不是在寻找警报器,而是在寻找类似“是或否”的信息框。所以看看这个。

<?php
?>
<html>
<head>
<script type="text/javascript">
function displayBOX(){
    var name=confirm("Not everyone has entered a bid, continue?")
    if (name==true){
        //document.write("Do your process here..")
        window.location="processContinuing.php";

    }else{
        //document.write("Stop all process...")
        window.location="stoppingProcesses.php";
    }
}
</script>
</head>


<?php
$query = "SELECT * faculty SET submitted = 0;";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
if($row != NULL) {  
echo "<script>displayBox();</script>";
}
?>