Display dialogs when it works

0

How do I display a Dialog when the script occurs correctly?

public function addDatabase($name, $collation) {
    // Checks whether all fields filled (Prevent future errors)
    if (!empty($name) AND !empty($collation)) {
        try {
            // If you want to change (Don't forget the ;)
            // http://dev.mysql.com/doc/refman/5.6/en/create-database.html
            $sql_query = 'CREATE DATABASE ''.$name.'' COLLATE ''.$collation.'';';

            // Prepare the query to execute
            $query = $this->db->prepare($sql_query);

            // Execute the query
            $query->execute();
        } catch (PDOException $e) {
            exit ('DB ERROR: '. $e->getMessage());
        }
    } else {
        if (empty($name)) {
            echo '$name está vazio';
        }
        if (empty($collation)) {
            echo '$collation está vazio';
        }
    }
} 

CSS / JS dialog: link

    
asked by anonymous 05.04.2015 / 03:43

2 answers

2

Are you using PDO ? Modify the code for something like this:

Dao Class

try {
    // If you want to change (Don't forget the ;)
    // http://dev.mysql.com/doc/refman/5.6/en/create-database.html
    //$sql_query = 'CREATE DATABASE ''.$name.'' COLLATE ''.$collation.'';';
    $sql_query = "CREATE DATABATE :name COLLATE :collation;";

    // Prepare the query to execute
    $query = $this->db->prepare($sql_query);

    $query->bindParam(":name", $name);
    $query->bindParam(":collation", $collation);

    // Execute the query
    $query->execute();
    $result = $query->rowCount();

    return $result > 0 ? true : false;
} catch() ....

PHP that receives the AJAX call:

$dao = new Dao();
$result = $dao->addDatabase("Foo", "Bar");

if ($result) {
    echo "success";
    exit();
} else {
    echo "error";
    exit();
}

Javascript / jQuery:

 $.ajax({
    url: 'ajax/script.php',
    ...
}).done(function(data) {
    if (data == 'success') {
        Materialize.toast('Sucesso!', 4000);
    } else {
        Materialize.toast('Ops, algo deu errado!', 4000)
    }
});

Example: link

    
05.04.2015 / 05:32
1

Since you used try , everything at the end of the block will run if an exception does not fire. To show the warning that you linked at the end of the question, you can use echo of PHP to print a JavaScript command:

public function addDatabase($name, $collation) {
    // Checks whether all fields filled (Prevent future errors)
    if (!empty($name) AND !empty($collation)) {
        try {
            // If you want to change (Don't forget the ;)
            // http://dev.mysql.com/doc/refman/5.6/en/create-database.html
            $sql_query = 'CREATE DATABASE ''.$name.'' COLLATE ''.$collation.'';';

            // Prepare the query to execute
            $query = $this->db->prepare($sql_query);

            // Execute the query
            $query->execute();

            // Dá um aviso para o usuário
            echo "<script>Materialize.toast('Sucesso!', 4000)</script>";
        } catch (PDOException $e) {
            // Caso aconteça algum erro, esse bloco é executado
            exit ('DB ERROR: '. $e->getMessage());
        }
    } else {
        [...]
} 
    
05.04.2015 / 04:10