1

I am using plain javascript for Ajax request. when sending data by post method php throwing an error.

index.php

<html>

    <header>

        <script>
            function submit(){

                var userName = document.getElementById("username").value;
                var passWord = document.getElementById("password").value;


                var data = "username=" + userName + "&password=" + passWord;

                //send ajax request
                var xmlHttp = new XMLHttpRequest();
                xmlHttp.onreadystatechange = function()
                {
                    if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
                    {
                        console.log(xmlHttp.responseText);
                    }
                }
                xmlHttp.open("post", "validateuser.php");
                xmlHttp.send(data);
            }
        </script>

    </header>

    <body>

            <label>User Name : </label>
            <input type="text" name="username" id="username"/>

            <label>Password : </label>
            <input type="text" name="password" id="password"/>

            <button onClick="submit()"> Login</button>

    </body>

</html>

validateuser.php

<?php

    $userName = $_POST["username"];
    $password = $_POST["password"];

echo $userName . $password;
3
  • what is the message in console log of browser? Commented Feb 3, 2016 at 5:03
  • What does your browser console say? Does it show that it successfully posted to validateuser.php? Does it show the data value as posted? Commented Feb 3, 2016 at 5:03
  • its out put php error. sayin </span> Notice: Undefined index: username in C:\wamp\www\redstone\formsapp\validateuser.php on line <i>3</i> and </span> Notice: Undefined index: password in C:\wamp\www\redstone\formsapp\validateuser.php on line <i>4</i> Commented Feb 3, 2016 at 5:05

2 Answers 2

5

In Javascript ajax for post you need to add following line in your code:

xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

You can check documentation and example W3schools and developer mozilla

And for more reliable code just add following line to your php code

if(isset($_POST['username']) && isset($_POST['password'])){
     //your code
}
Sign up to request clarification or add additional context in comments.

1 Comment

Using jquery's $.ajax() for so long, I forgot about this. Nice catch! +1 (although I would recommend linking to developer.mozilla.org/en-US/docs/AJAX/Getting_Started instead of w3schools)
1

You need to set the headers on your xmlHttp object. Add the following line before xmlHttp.send(data); line :

xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.