"Onclick" function that calls PHP function

Asked

Viewed 359 times

0

Do you guys talk? I am beginner in development and I have a problem, I have a form (HTML) that sends data to a PHP file and registers in the database.

What I need is: When successfully recording the data, display on the "index" page (the one with the form) a custom alert in JQUERY (https://jqueryui.com/dialog/), I have tried several ways but could not. 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: Abbreviated the code.

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

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 took the Submit form so that the ajax goes without changing page)

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
            }
        }
    });
});
  • Thank you very much, a question that is even silly. This Jquery code I put in which part of the code? create a separate file? How will I "call" it in index after giving true in PHP?

  • in the HTML file you can include it within a <script> tag (jquery code here) </script> or create a separate file and import it into the script tag as well: <script src='arquivo_external.js'></script>.

  • As for the 'call it in index', note that the ajax code has a variable 'url' that is already directing to your php file. There in php Voce you have to make your logic of saving the email in the database, and when Voce gives an echo in PHP it will return this echo to your jquery code. (So only echo what is necessary)

0

Here is an alternative to AJAX, posted in another reply.

It is not good practice to use location.href along with the code PHP. For redirects you can use the function header.

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 the HTML you can use a code to check the parameters of the URL, 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>

-2

Hello, Try to make a require "index.html" inside the if thus:

if ($conn->query($sql) === TRUE) {
    require "index.html"
} 

Browser other questions tagged

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