call a php function inside a div

Viewed 60

I want to make a call of this php logic inside a html div but when passing it as a function the logic breaks since it does not send an error message in case of entering the pass wrong and its confirmation at the time of performing the password change.

<?php
    
    require 'funcs/conexion.php';
    require 'funcs/funcs.php';
    
    $user_id = $mysqli->real_escape_string($_POST['user_id']);
    $token = $mysqli->real_escape_string($_POST['token']);
    $password = $mysqli->real_escape_string($_POST['password']);
    $con_password = $mysqli->real_escape_string($_POST['con_password']);
    

    if(validaPassword($password, $con_password))
    {
        $pass_hash = hashPassword($password);
        
        if(cambiaPassword($pass_hash, $user_id, $token))
        {
            echo "Contrase&ntilde;a Modificada <br> <a href='index_alumnos.php' >Iniciar Sesion</a>";
            } else {
            echo "Error al modificar contrase&ntilde;a";
        }
        } else {
        echo "Las contraseñas no coinciden <br> <a href='index_alumnos.php' >contacta a Academia</a>";
    }
?>  
1 Answers

If the echo happens before your actual div is drawn, the echo goes... right where it happens. Which isn't within your div.

One way of getting around this would be to put your error message into a variable and then deliver this variable into your div (whether it be through a return value, if it's a call, or some other means.)

Here's a simple example to illustrate this:

<?php
if(1 === 2) {
    //great, 1 is 2
} else {
    //oh no, an error
    $someErrorLine = '1 is not 2';
} ?>

<h1>Hi</h1>
<div><?= $someErrorLine ?></div>

You could also check if the variable exists, something like if(isset($someErrorLine)) {} and echo the div with it, or put the div within your variable.

Related