首页 > 解决方案 > 在 div 中调用 php 函数

问题描述

我想在 html div 中调用这个 php 逻辑,但是当将它作为函数传递时,逻辑中断,因为它不会发送错误消息以防输入错误并在执行密码更改时确认.

<?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>";
    }
?>  

标签: phphtml

解决方案


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.


推荐阅读