0

I'm trying to make a simple login form in php and html and it just won't print the echo into my div container I must be using the load function incorrectly but I don't quite know how to fix it.

Here is my html/script code.

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<script src="js/jquery-3.1.0.min.js"></script>

<script type="application/javascript">
$(document).ready(function(){
    $("#cargar").click(function(){
        var u = $("#username").val();
        var v = $("#password").val();
        $.get("login.php",{username:u,password:v});
        $("#contenedor").load('login.php')
    });

});
</script>

<body>
<form>
  User name:<br>
  <input id="username" type="text" name="username" />
  <br>
  User password:<br>
  <input id="password" type="text" name="password"><br>
  <input id="cargar" type="button" value="cargar" />
</form>

<div id="contenedor"></div>

</body>
</html>

And here is my php

    <?php
    $usuario = $_REQUEST['username'];
    $contra = $_REQUEST['password'];

    if($usuario=="admin" && $contra=="admin"){
        echo "<h2>Bienvenido</h2>";}
        else{
            "<h2>Error</h2>";
            }
    ?>
Rafael
  • 55
  • 9
  • How have you verified that this isn't working? The client-side code currently doesn't actually *do anything* with the result from the server-side code. – David Sep 21 '16 at 19:53
  • you can only see the PHP response in your console / developer tools because you are not outputting it on the page's html itself. – low_rents Sep 21 '16 at 19:53
  • 1
    It should be `var v = $("#password").val();` (# is missing) – leotesta Sep 21 '16 at 19:55
  • 1
    You are suppose to use `success-handler` of `$.get` – Rayon Sep 21 '16 at 19:55
  • I'm using WAMP to test it and I already added the "#" and it still won't return the echo. – Rafael Sep 21 '16 at 19:59
  • Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – VLAZ Sep 21 '16 at 20:01
  • Use $.get("login.php", {username:u,password:v}, function (data) { alert(data);}) - but you should not be using a GET request to log someone in. This should be a POST to hide the password from being in the url. – Saman Sep 21 '16 at 21:54

0 Answers0