0

This probably has some really obvious answer that I'm missing, but I just can't seem to figure it out. All the search functions from PHP and queries work. The only thing that doesn't work is that the data isn't being displayed properly in the text area translated. Below is the code.

document.getElementById("translated").innerHTML = xmlhttp.open("GET","ajax-result.php?result="+num,true);

After the innerHTML portion, whatever I add (from text to html) goes into the right place but right now the above code says it is undefined.

2
  • 1
    ...that's not how ajax works. Commented Jun 19, 2013 at 1:09
  • @Nile The function returns the right value from the php query. It just does not display it right. Commented Jun 19, 2013 at 1:13

3 Answers 3

1

xmlhttp.open does not return anything - that's why you get "undefined". Read http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp , paying particular attention to xmlhttp.onreadystatechange part.

Far more convenient way to do this is to use jquery ajax method: http://api.jquery.com/jQuery.ajax/

Sign up to request clarification or add additional context in comments.

1 Comment

Used xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("myDiv").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","ajax_info.txt",true); xmlhttp.send(); and it worked flawlessly. Thanks.
0

That's not how ajax works. You should have a callback function to be called when server responses

Something like

if (window.XMLHttpRequest) {
        xmlhttp=new XMLHttpRequest();
}
// setup callback function
xmlhttp.onreadystatechange=function(){
    if (xmlhttp.readyState==4 && xmlhttp.status==200){
        if(xmlhttp.responseText == 1){
            document.getElementById("translated").innerHTML = xmlhttp.responseText;
        }
    }
}
xmlhttp.open("GET",'your request to sever',true);

Comments

0

If you really want to save yourself some head ache, I'd familiarize yourself with jQuery.

Here is a full working example of what it appears it is you are attempting to accomplish.

The implementation of the input and button was simply to simulate the method in which a value is being specified and the function is being called.

<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
    </head>
    <body>
        <script type="text/javascript">
            function runAjax(num) {
                $.ajax({
                    'url': 'ajax-result.php',
                    'type': 'GET', // Can also be 'POST'
                    'data': {
                        'result': num
                    },
                    'success': function(result) {
                        $('#translated').html(result);
                        alert('success!');
                    },
                    'error': function(xhr, status, exception) {
                        alert('failed with status code: ' + status);
                    }
                });
            }

            $(document).ready(function() {
                $('button').click(function() {
                    var $input = $('input');
                    var num = $input.val();
                    runAjax(num);
                });
            });
        </script>

        <input type="text" name="num" value="123" />
        <button type="button">Click Me!</button>
    </body>
</html>

1 Comment

Thanks for this. I've been meaning to take a deeper look into jQuery.

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.