0

i have form in html file , it's file body

<form action="" method="post">       
    <input id="name" type="text" name="username" placeholder="username" required></br></br>
    <input id="password" type="password" name="password" placeholder="password" required></br></br>
    <p id="warn"></p></br></br>
    <input type="submit" value="Login" onclick="f()"></br>
</form>

<script>
function f() {
    var name = document.getElementById("name").value ;
    var password = document.getElementById("password").value ;
    var xttp = new XMLHttpRequest();
    xttp.onreadystatechange = function () {
        if (this.readyState == 4 && this.status == 200) {
            document.getElementById("warn").innerHTML = this.responseText ;             
        }
    };

    xttp.open("GET", "tst1.php?name="+name+"& password"+password, true);
    xttp.send();
}
</script>

and i have php file callled tst1.php

<?php 
    echo $_REQUEST["name"]+"...."+$_REQUEST["password"] 
?>

im trying to use ajax to show the entered value below them in p tag with id="warn" but it dose not show it

Raghav Garg
  • 3,601
  • 2
  • 23
  • 32
mike
  • 45
  • 5

2 Answers2

3

There are few things you need to change in your code, such as:

  • echo $_REQUEST["name"]+"...."+$_REQUEST["password"], + is not a concatenation operator, . is. So this statement should be like this:

    echo $_REQUEST["name"] . "...." . $_REQUEST["password"]; 
    
  • Missing = sign after password in this statement, xttp.open("GET", "tst1.php?name="+name+"& password"+password, true);. This statement should be like this:

    xttp.open("GET", "post.php?name="+name+"&password="+password, true);
    
  • Return false immediately after calling f() function, otherwise the page would refresh and you won't be able to see the output value here <p id="warn"></p>.

    <input ... onclick="f(); return false;">
    
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37
  • thanks for solving problem and sorry for stupid mistakes , could you plain explain me what that return false do ? dose it stop the form to be submitted or ... ? @Rajdeep Paul – mike Sep 02 '17 at 18:49
  • @mamad What `return false;` will do is, it will prevent the form from being submitted in the first place. That way you'll be able to see the output here, `

    `. Also, consider *accepting* the answer if it resolved your issue. [How to accept answer on Stack Overflow?](https://meta.stackexchange.com/a/5235)
    – Rajdeep Paul Sep 02 '17 at 18:55
  • sorry im new to web developing , what is common way to have a login page ? i want to check the entered values in my database if there is user show his homepage if not show error ? is it ajax good for this purpose ?can you plz give me some advice ? @ Rajdeep Paul – mike Sep 02 '17 at 19:31
  • @mamad You can start off with some [AJAX tutorials](https://developer.mozilla.org/en-US/docs/AJAX/Getting_Started). And on top of that, you would fine plenty of tutorials on Google on [how to implement a simple login page using JavaScript/jQuery and AJAX](https://www.google.co.in/search?q=implement+login+page+using+ajax&oq=implement+login+page+using+ajax&gs_l=psy-ab.3...2327.2327.0.2555.1.1.0.0.0.0.119.119.0j1.1.0....0...1.1.64.psy-ab..0.0.0.e7qLEaW9eqI&gws_rd=cr&dcr=0&ei=_AerWYGNK8GLvQSdm7bAAg). – Rajdeep Paul Sep 02 '17 at 19:35
0

First you have forgot = equal sign for password here

xttp.open("GET", "tst1.php?name="+name+"& password="+password, true);
xttp.send();

Also you should first check request data by isset like this

$name = isset($_REQUEST["name"]): $_REQUEST["name"] : ""; 

Also change submit button as button from submit to make ajax work

Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109