Obtain PROMPT values with AJAX

2

Good evening.

How can I get the values that have been entered through a JS prompt to use that data in a database?

I understand that it is with AJAX, and I have developed this code, but it does not take any action on the code.

<!-- Cambio de la contraseña -->
<button type="submit" class="cambios" onclick="cambioContrasenia()">Cambiar la contraseña</button>

<script>
    function cambioContrasenia(){
        var ctr = prompt("Introduzca la nueva contraseña:");
        console.log(ctr);

        if(ctr != null){
            $.ajax({
                url: 'cambio_pass.php',
                data: {pass: ctr},
            }).done(function(data){
                console.log(data);
            });
        }
    }
</script>

I have the JQuery library included in my code, but it does not take any action. And this is the file that processes the information

<?php

session_start();

$link = mysql_connect('localhost','root','2ASIR') or die ("No se puede establecer conexión con el gestor de base de datos");

mysql_select_db('agromarquez') or die ("No se puede establecer conexión con la base de datos");

$username = $_SESSION['username'];

$contrasenia = $_GET['pass'];

mysql_query("UPDATE cliente SET contrasenia='$contrasenia' where username='$username'");?>

Thank you very much in advance.

    
asked by Luismi Márquez Gutierrez 24.05.2017 в 23:20
source

1 answer

0

Change the type of the button from "submit" to "button" (The submit is used to send data from a form, if you want to execute code in an event, you can use the button with click.

And add the JQuery library (in case it is not)

The code would look like this:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Cambio de la contraseña -->
<button type="button" class="cambios" onclick="cambioContrasenia()">Cambiar la contraseña</button>

<script>
    function cambioContrasenia(){
        var ctr = prompt("Introduzca la nueva contraseña:");
        console.log(ctr);

        if(ctr != null){
            $.ajax({
                url: 'cambio_pass.php',
                data: {pass: ctr},
            }).done(function(data){
                console.log(data);
            });
        }
    }
</script>

Greetings

    
answered by 24.05.2017 / 23:43
source