Display dialogs when successful

Asked

Viewed 196 times

1

How do I make a Dialog appear 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 of the dialog: http://materializecss.com/dialogs.html

2 answers

2

You are using PDO? Modify the code for something like this:

Classe Dao

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 receiving 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: http://www.codeshare.io/oP1vO

  • " PHP that receives the AJAX call " This would be on which page? Inside the form ? EDIT: I’ll try to put it in the controller.php because it takes the model information

  • EDIT: I noticed that it is returning successss and error. However no dialog is appearing in the index. Code completo http://www.codeshare.io/LZKq0

  • You’re not sending anything through ajax, assign a id to his form and place these lines in your ajax just below url: .., type: 'POST', data: $('#seuform').serialize().

  • Code updated: More where do I put href and which link? http://www.codeshare.io/LZKq0

  • You will need a php to receive the data from your form and instantiate the class Controller, this is the php that needs to be placed in the parameter url: of your js code, I’ll make an example here on my machine for you to have a complete view. 4 files will be used (index.php, Controller.php, Model.php and another.php file)

  • Sorry for the delay, I ran out of computer. But, where is the example?

Show 1 more comment

1

How did you use the try, everything at the end of the block will rotate if an exception is not triggered. To display the warning you linked at the end of the question, you can use the echo 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 {
        [...]
} 
  • But I am using MVC, ie it goes to another link when running. What do ?

  • After the $query->execute(); Does he go immediately to another place? Does he not finish executing the block? An alternative would be to put the message on this other link, but since I do not know how it works, there is no way to help you.

  • After filling in all the data is sent to another page, processes and back to the home page.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.