topacoBoy
topacoBoy

Reputation: 197

How to pass input values to ajax

Okay, I'm new to Ajax. My problem is that I'm not sure how to retrieve data which is in the <input> tag and send it to Ajax. I have tried searching on the internet, but most of the solutions are using jQuery Ajax, which is what I'm not looking for at the moment.

Here is my code.

I want to save this value so that my Ajax can read it...

<input id="IDValue" name="IDValue" value="<?php echo $row['exist']?>" >

This is my Ajax script...

 function message(){
    var ID=$(".IDValue").val();
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function(){
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
             document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
                            }
                        };
             xmlhttp.open("POST","retrieveMsg.php?q=" +ID,true);
             xmlhttp.send();
               }

Please help me, guys. The reason I am doing this method is because (My previous post) Send input value to php using ajax with result printed to div

Upvotes: 1

Views: 2183

Answers (2)

PRANAV
PRANAV

Reputation: 629

i am confused about your $row['exist'] returns value or not and what html control you used for id="txtHint". here i have provided demo which same as your code in some way...try and have an idea and make changes as per your requirement...

<html>
    <head>
    <script src="jquery.js"></script>
    </head>
    <body>
        <input id="IDValue" name="IDValue" value="<?php echo 'hello';?>"  >
        <textarea id="txtHint"></textarea>
        <input type="button" value="Click" onClick="message()"/>
    <script>
        function message(){
    var ID=$("#IDValue").val();
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function(){
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
             document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
                            }
                        };
             xmlhttp.open("POST","login.php?q=" +ID,true);
             xmlhttp.send();
               }
    </script>
    </body>
</html>

Upvotes: 0

kannan
kannan

Reputation: 711

Replace it

 var ID=$(".IDValue").val();

With

 var ID = document.getElementById("IDValie").value;

Upvotes: 4

Related Questions