Response of an echo in a div ID

0

Good I'm doing a login and I would need the echo that responds to me that does not exist that user or password show me in a div inside the form instead of an echo.

resupuesta.php

   <?php
    require('conexion/conexion.php');
    session_start();
    // If form submitted, insert values into the database.
    if($_POST["funcion"]=="FUNCION"){
    // removes backslashes
      $username = stripslashes($_REQUEST['username']);
      //escapes special characters in a string
      $username = mysqli_real_escape_string($mysqli,$username);
      $password = stripslashes($_REQUEST['password']);
      $password = mysqli_real_escape_string($mysqli,$password);
    //Checking is user existing in the database or not
      $query = "SELECT * FROM Usuarios WHERE Username='$username'";
      $result = mysqli_query($mysqli,$query) or die(mysql_error());
      $rows = mysqli_num_rows($result);
      if($rows==1){
        $_SESSION['username'] = $username;
        $_SESSION['password'] = $password;
        echo "1";
        //header("Location: mi-cuenta.php");
      }else{
        ?>
        <div class='form'> 
             <h3>Username/password is incorrect.</h3>
             <br/>Click here to <a href='login.php'>Login</a>
        </div>
        <?
   }
 exit();
 }
?>

AJAX

<script>
    var post=$.post('respuesta.php',{
        funcion:'FUNCION'
    },function(msg){
        if(msg=="1"){
            window.lcoation="index.php";
        }else{
            $("#respuesta").append(msg);
            //alert(msg);
        }
    });
</script>

This is the HTML with the FORM

<div class="wrap-user">
  <div class="container">
    <div class="wrap-user__in">
      <aside class="wrap-user__aside">
      <header class="wrap-user__header wrap-user__header--inline-small">
         <a href="#" class="wrap-user__header__link">o BIEN CREATE AUNA CUENTA</a>
         <h1 class="wrap-user__header__title h5user2">Inicia Sesión</h1>
      </header>
         <p class="puser2">En Depildiodo protegemos todos los datos de forma segura (HTTP/SSL)</p>
      </aside>
      <main class="wrap-user__content">
        <div class="form">
        <h5 class="h5user">INICIA SESIÓN CON TU CUENTA</h5>
        <form action="" method="post" name="login">
            <label style="color: #737373;font-weight: 100;">EMAIL</label>
            <div class="form-group" style="margin-bottom: 28px;">
             <input type="text" class=" my-form-control" name="username" placeholder="[email protected]" required />
            </div>

            <label style="color: #737373;font-weight: 100;">CONTRASEÑA</label>
            <div class="form-group">
             <input type="password" class=" my-form-control" name="password" placeholder="minimo 4 cararcteres" required />
            </div>
            <div class="col-lg-12" style="text-align: left; margin-top: 5%; padding: 0px;">
              <input name="chec" class="chzxc" type="checkbox" id="chec" onChange="comprobar(this);"/>
            <label class="aceptatm">He leído y acepto la <a href="aviso_legal.php">Términos y Condiciones</a></label>
            </div>

           **<div id="respuesta">RESPUESTA</DIV>**

            <input name="submit" id="submit" type="submit" class="btnLogin" value="Iniciar Sesión" disabled />
                        </form>
                </div>
            </main>
        </div>
     </div>
    </div>

End of the php

<?php } ?>  **Esta llave si estando en funcionamineto???**
    
asked by Miguel 05.09.2018 в 07:16
source

2 answers

0

HTML

<div class="wrap-user">
    <div class="container">
        <div class="wrap-user__in">
            <aside class="wrap-user__aside">
                <header class="wrap-user__header wrap-user__header--inline-small">
                    <a href="#" class="wrap-user__header__link">o BIEN CREATE AUNA CUENTA</a>
                    <h1 class="wrap-user__header__title h5user2">Inicia Sesión</h1>
                </header>
                <p class="puser2">En Depildiodo protegemos todos los datos de forma segura (HTTP/SSL)</p>
            </aside>
            <main class="wrap-user__content">
                <div class="form">
                    <h5 class="h5user">INICIA SESIÓN CON TU CUENTA</h5>
                    <form name="login">
                        <label style="color: #737373;font-weight: 100;">EMAIL</label>
                        <div class="form-group" style="margin-bottom: 28px;">
                            <input type="text" class=" my-form-control" id="username" name="username" placeholder="[email protected]" required />
                        </div>

                        <label style="color: #737373;font-weight: 100;">CONTRASEÑA</label>
                        <div class="form-group">
                            <input type="password" id="password" class=" my-form-control" name="password" placeholder="minimo 4 cararcteres" required />
                        </div>
                        <div class="col-lg-12" style="text-align: left; margin-top: 5%; padding: 0px;">
                            <input name="chec" class="chzxc" type="checkbox" id="chec" onChange="comprobar(this);"/>
                            <label class="aceptatm">He leído y acepto la <a href="aviso_legal.php">Términos y Condiciones</a></label>
                        </div>

                        <div id="respuesta">RESPUESTA</div>

                        <input name="submit" id="submit" type="submit" class="btnLogin" value="Iniciar Sesión" disabled />
                    </form>
                </div>
            </main>
        </div>
    </div>
</div>

JS

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script>
$(document).ready(function(){
    $("#submit").click(function(e){
        e.preventDefault();
        var post=$.post('respuesta.php',{
            funcion:'FUNCION',
            username:$("#username").val(),
            password:$("#password").val(),
        },function(msg){
            if(msg=="1"){
                window.location="index.php";
            }else{
                $("#respuesta").html(msg);
            }
        });
    });
});
</script>

answer.php

<?php
require('conexion/conexion.php');
@session_start();

if($_POST["funcion"]=="FUNCION"){
      $username = stripslashes($_POST['username']);
      $username = mysqli_real_escape_string($mysqli,$username);
      $password = stripslashes($_POST['password']);
      $password = mysqli_real_escape_string($mysqli,$password);
      $query = "SELECT * FROM Usuarios WHERE Username='$username'";
      $result = mysqli_query($mysqli,$query) or die(mysql_error());
      $rows = mysqli_num_rows($result);
      if($rows==1){
        $_SESSION['username'] = $username;
        $_SESSION['password'] = $password;
        echo "1";
        //header("Location: mi-cuenta.php");
      }else{
        ?>
        <div class='form'> 
             <h3>Username/password is incorrect.</h3>
             <br/>Click here to <a href='login.php'>Login</a>
        </div>
        <?php
   }
 exit();
}
?>
    
answered by 05.09.2018 / 07:29
source
1

If you do not want to use ajax simply declare a variable:

$respuesta = "<div class='form'> 
     <h3>Username/password is incorrect.</h3>
     <br/>Click here to <a href='login.php'>Login</a></div>";

And then show it

<div id="respuesta"><?php echo $respuesta ?></div>
    
answered by 05.09.2018 в 07:39