How to "raise" classes in the bank using also confirm javascript

Asked

Viewed 46 times

0

Hello, I have the following code and I need to pick up a student’s class at the bank and if yes to confirm in JS: "climb" your class.

Ex: The student is 1A, I press the button to go up and he needs to go to 2A (but it won’t be just one, by pressing this button everyone will go up too).

The code as it is below enters the confirm but if clicked on OK, the page is blank because it does nothing but.

I understand that it has flaws but I can’t think of anything (seriously, help ;-;) and I would like someone to help me :v (so that’s just the idea of what I need)

<script>
    function up()
    {
        <?php
        include ('connection.php');
        
        $busca = $con->prepare("SELECT * FROM turmas;");
        $busca->execute();
        
        $linha = $busca->fetch(PDO::FETCH_ASSOC);
        
        if($linha['turma'].equals("1A"))
        {
            $sql = "UPDATE carterinha SET turma = '2A';";
            $stmt = $conn->prepare($sql);
            
            try
            {
                $stmt->execute();
                echo '
                    <script>
                        alert("Turmas upadas!");
                        location.replace("../../index.php");
                    </script>
                ';
            } catch(PDOException $e)
            {
                echo '
                    <script>
                        alert("Falha! Tente novamente!");
                        location.replace("../../index.php");
                    </script>
                ';   
            }
        }
        ?>
    }
</script>
<?php
    echo '
        <script>
        if(confirm("Deseja realmente upar as turmas?")) 
        {
            up();
            alert("Turmas upadas!");
            location.replace("../../index.php");
        } else 
        {
            alert("Up turma cancelado");
            location.replace("../../index.php");
        }
        </script>
    ';
?>

  • Hello java script is in the client part, and php on the server is not possible to mix the two so it is necessary to send through a post or get to the server with a form or via Javascript and on the save server.

  • I understand... Any suggestions of what I can do, at least in "climbing" the classes :v or making this confirm with php somehow?

  • It’s just, from what I’ve seen, you’re starting now, right? if yes a hint would be to start studying only php with html then add the simplest to understand java script.

1 answer

2


You are wrong about the form of communication Javascript -> PHP. The PHP is interpreted in the back-end (server), ie no code you write in PHP goes to the front-end (client). So you need to do the Javascript call the server to execute the PHP script you want. This call could be in AJAX for example, or simply redirect a user to the PHP page where the script is. I recommend you study this topic a little bit.

But I will give you a light in this case. You will create a separate file, which we will call up.php, and will put in it the code snippet that makes the UPDATE you want:

<?php
    include ('connection.php');

    $busca = $con->prepare("SELECT * FROM turmas;");
    $busca->execute();

    $linha = $busca->fetch(PDO::FETCH_ASSOC);

    if($linha['turma'].equals("1A"))
    {
        $sql = "UPDATE carterinha SET turma = '2A';";
        $stmt = $conn->prepare($sql);

        try
        {
            $stmt->execute();
            echo 'Turmas upadas!';
        } catch(PDOException $e) {
            echo 'Falha! Tente novamente!';   
        }
    }
?>

And now let’s have the main file, let’s call it index.php, let’s put in it the HTML and Javascript, which will call the up.php to perform the update you want.

<html>
<head>
    <title>Exemplo chamada AJAX</title>
</head>
<body>
    <button onclick="realizarChamada()">Chamar o PHP no servidor</button>
    <script type="text/javascript">
        function realizarChamada() {
            if(confirm("Deseja realmente upar as turmas?")) 
            {
                // Aqui vamos realizar a chamada AJAX que falei

                // Instancio um objeto da classe XMLHttpRequest
                var xhttp = new XMLHttpRequest();

                // Defino o que vou fazer com o retorno obtido do servidor
                xhttp.onreadystatechange = function() {
                    if (this.readyState == 4 && this.status == 200) {
                        // Aqui estou pegando o texto que eu dei echo lá no servidor e colocando no alert
                        alert(this.responseText);
                   }
                };

                // Abro a conexão com o arquivo desejado
                xhttp.open("GET", "up.php", true);

                // Envio a requisição
                xhttp.send();
            } else {
                alert("Up turma cancelado");
            }
        }
    </script>
</body>
</html>

This page has a button that when clicking, will execute the AJAX GET request on the server.

As I said I recommend researching on the subject. I leave a simple article below for study: Creating a register with PHP, Ajax and jQuery

Browser other questions tagged

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