Onclick function that calls PHP function

0

Talk galley blz? I am a beginner in development and I have a problem, I have a form (HTML) that sends data to a PHP file and registers it in the database.

What I need is: When you successfully record the data, display a custom alert on the "index" page (the one with the form) in JQUERY ( link ), I have tried in several ways but I have not been able to. Someone help me plzzz.

HTML:

<form action="cadastrar.php" method="post" class="form-inline" role="form">
                  <div class="form-group">
                    <label class="sr-only" for="email">Email address</label>
                    <input type="email" name="email" class="form-control" id="email" placeholder="Escreva o seu e-mail">
                  </div>
                  <button type="submit" class="btn btn-info">Cadastrar</button>
                </form>                         </div>

php:

$sql = "INSERT INTO users (email)
VALUES ('$email')";

if ($conn->query($sql) === TRUE) {
  $var =  $var = "<script language= 'JavaScript'>
  location.href='index.html'
  </script>";
  echo $var;
} 
else {
    echo "Esse email ja esta cadastrado";
}

$conn->close();

?>

PS: I shortened the code.

PS2: I know I could do with ajax, but I have no idea how.

    
asked by anonymous 17.08.2018 / 02:36

3 answers

1

With AJAX you would do so:

<form class="form-inline" role="form">
              <div class="form-group">
                <label class="sr-only" for="email">Email address</label>
                <input type="email" name="email" class="form-control" id="email" placeholder="Escreva o seu e-mail">
              </div>
              <button id="cadastrar" class="btn btn-info">Cadastrar</button>
            </form>                         

(I removed the form submit so that ajax does not change pages)

Then the jQuery code:

$('#cadastrar').click(function () {
    $.ajax({
        url: 'cadastrar.php',
        method: 'POST',
        data: {email: $('#email').val()},
        success: function (response) {
            if (response == 'true') { // voce deve dar um echo 'true' no PHP caso seja salvo
                //Coloque aqui seu código para invocar o alerta personalizado
            }
        }
    });
});
    
17.08.2018 / 03:23
0

Here's an alternative to AJAX, posted in another answer.

It is not a good practice to use location.href together with the PHP tag. For redirects you can use the header function.

PHP:

$sql = "INSERT INTO users (email) VALUES ('$email')";

if ($conn->query($sql) === TRUE) {
  $url = "index.html?success=true&msg=Sucesso";
} 
else {
  $url = "index.html?success=false&msg=Insucesso";
}

$conn->close();
header("Location: ${url}");

?>

In HTML you can use a code to check URL parameters, for example: success , msg etc.

HTML:

<form action="cadastrar.php" method="post" class="form-inline" role="form">
    <!-- Código aqui -->
</form>

<script>
window.location.search.substr(1).split("&").forEach( data => {
    [param, value] = data.split("=");

    if (param == "success" && value == "true") {
        alert("Código para abrir o 'dialog'")
    }
})
</script>
    
17.08.2018 / 03:29
-2

Hello, Try to make a require "index.html" inside if like this:

if ($conn->query($sql) === TRUE) {
    require "index.html"
} 
    
17.08.2018 / 03:06