Bobby S
Bobby S

Reputation: 4116

If MySQL Result Empty display a confirm box

I'm using some crazy mixture of 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

What is the best way to have this confirm box display, before completing the rest of the queries?

Upvotes: 0

Views: 2015

Answers (4)

Jhourlad Estrella
Jhourlad Estrella

Reputation: 3670

$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>";
}

Upvotes: 1

Play with this code and you will get it to work eventually. I know you are not looking for just alertbox , instead you are looking for something like "yes or no" informational box. So check this out.

<?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>";
}
?>

Upvotes: 0

Jonathan Lonowski
Jonathan Lonowski

Reputation: 123473

You can't do this in 1 continuous block, as all of the PHP will execute before the confirm (due to server vs. client).

You will need to break these into 2 separate steps and have the client mediate between them:

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.
?>

Upvotes: 2

Joeri
Joeri

Reputation: 361

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

or

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

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

Upvotes: 3

Related Questions