How to get data from a form from another page

-1

Hello everyone, I have the following question: I have a web application that I want when I log into the "index.html" home page to get the 'name' and 'email' and put it with jquery on another page with php, ajax and jquery 'paginaprincipal.html'.

example: index.html

<form name="form-login" id="MyForm">
        <input id="email" type="text" name="email" required>
        <input id="senha" type="password" name="senha" required>

        <button type="submit" id="btnLogin">Acessar</button>
   </form>

paginaprincipal.html

<div id="pega">
      Nome: <p id="nome"></p>
      Email: <p id="nome"></p>
</div>

note: before the main page I still have two previous pages

Example: I have a login index that works with ajax and php:

apagebetweenindexandmain

andfinallyIhavemypaginaprincipal.htmlwhereIwanttoreculperatethelogindataofindex.html

Ihopetohavebeenclearthankyouforbeingabletohelp...

Myphplogincode:

<?phprequire'./connection.php';$email=$_POST['email'];$senha=$_POST['senha'];$stm=$pdo->prepare('SELECT*FROMusuario_appWHEREemail=:emailANDsenha=:senha');$stm->bindParam(':email',$_POST['email'],PDO::PARAM_STR);$stm->bindParam(':senha',$_POST['senha'],PDO::PARAM_STR);$stm->execute();if($linha=$stm->fetch(PDO::FETCH_ASSOC)){echo"Bem vindo {$linha['nome']}";
} else {
    echo 'Erro ao efetuar login!';
}

My login js:

    $('#btnLogin').click(function () {

    var url = "http://localhost:/projeto/login.php";

    $.post(url, $('#MyForm :input').serializeArray(), function (data) {
        navigator.notification.alert(data, null, "MSG", "OK");
            alert(data);
            window.location.href = 'pagina_principal.html';
        });
    });


$('#MyForm').submit(function () {//PARA PAGINA NÃO CARREGAR NOVAMENTE
    return false;
});

My ajax to get the data:

    var reculpera = $("#MyForm").serialize();//form login #MyForm
$.ajax({

    type: 'POST',
    url: "http://localhost:/projeto/login.php",
    data: reculpera

}).done(function (data) {
    localStorage.setItem('nome', data.nome);
    $("#pegaemail").text(localStorage.nome);
//    alert(data.nome);

}).fail(function () {
    alert("erro");
});

result of my ajax to play on that line

$("#pegaemail").text(localStorage.nome);

Is #pegaemail output giving the message 'undefined' error in ajax?

    
asked by anonymous 12.09.2017 / 16:33

2 answers

1

You have to send the data to the server, and then send that data back to your main page. To do this, it is not possible to use pure html, you must use php or some other language on the server to process this information.

    
12.09.2017 / 16:37
1

You need to use BackEnd for this. I'll give you an example to get clearer.

EXEMPLO1 - PHP

<form id="form" method="POST" action="URL.PHP">
   <input type="text" name="email">
   <input type="password" name="senha">
</form>

<?php
   session_start();
   $email = $_POST['email'];
   $senha = $_POST['senha'];

   $sql = $pdo->prepare("SELECT * FROM usuarios WHERE email = ? AND senha = ?");
   $sql->execute(array($email, $senha));

   $ln = $sql->fetchObject();

   $_SESSION['nome'] = $ln->nome;
 ?>

Already on the homepage it would be just like this ...

<h1><?= $_SESSION['nome']; ?></h1>

But it's important that you study to understand. You can search on PHP + PDO.

Here are some links that can help you.

  • PDO
  • MYSQL
  • LOGIN WITH PHP AND PDO
  • I advise you to study more in the language, it will help you a lot.

    UPDATE:

    <?php
    
    require './connection.php';
    $retorno = array();
    
    $email = $_POST['email'];
    $senha = $_POST['senha'];
    
    $stm = $pdo->prepare('SELECT * FROM usuario_app WHERE email = :email AND senha = :senha');
    $stm->bindParam(':email', $_POST['email'], PDO::PARAM_STR);
    $stm->bindParam(':senha', $_POST['senha'], PDO::PARAM_STR);
    $stm->execute();
    
    if ($linha = $stm->fetchObject()) {
        $retorno['erro'] = false;
        $retorno['nome'] = $linha->nome;
    } else {
        $retorno['erro'] = true;
    }
    
    
    $('#btnLogin').click(function () {
        var url = "http://localhost:/projeto/login.php";
        $.ajax({
           type: 'POST',
           url: url,
           data: $('#MyForm').serialize(),
           dataType: 'json',
           success: function(data){
               var mensagem = data.erro ? 'NAO LOGADO' : 'LOGADO'
    
               if(data.erro){
                   navigator.notification.alert(mensagem, null, "MSG", "OK");
    
               }else{
                   navigator.notification.alert(mensagem, null, "MSG", "OK");
                   localStorage.setItem('nome', data.nome)
               }
    
           }
        })
    });
    
    $('#MyForm').submit(function () {//PARA PAGINA NÃO CARREGAR NOVAMENTE
        return false;
    });
    
        
    12.09.2017 / 16:46