mike
mike

Reputation: 55

php $_REQUEST[ ] dosnt work

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

Upvotes: 0

Views: 70

Answers (2)

Rajdeep Paul
Rajdeep Paul

Reputation: 16963

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

Upvotes: 3

Niklesh Raut
Niklesh Raut

Reputation: 34924

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

Upvotes: 0

Related Questions