0

I have a simple script, in ajax, and I want to capture the return and process it according to the value:

if (xmlhttp.readyState==4) {

    if (xmlhttp.responseText == "not available") {
        document.write("not available");
    }

}

At the same time, I tried this, which worked:

if (xmlhttp.readyState==4) {

    document.write(xmlhttp.responseText);

}

What wrong am I doing?


Thank you for your reply. This is my current domain availability checking script which works great:

    <html>
    <head>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $('#dtype').change(function() {
                    var opt = $('#domain').val();
                    $.ajax({
                        type: "POST",
                        url: "testwhois.php",
                        data: 'd=' + opt,
                        success:function(data){
                            $('#txtHint'). html(data);
                        }
                    });
                });
            });
        </script>
    </head>
<body>
 <form>  Domain name : <input type="text" name="domain" id="domain"> <input type="radio" name="dtype" id="dtype" value="new">New <input type="radio" name="dtype" value="transfer">Transfer  <span id="txtHint"></span> </form>
</body>
</html>

However, I want to have two things in it:

  1. a preloader image (I have it) while the script is working in the place of 'txtHint' where the answer will be displayed.

  2. The answer is returned in the format of "not available" or available". I want to make the 'domain' field blank, when the answer is returned as "not available" with the html codes.

Thanks again.

halfer
  • 19,824
  • 17
  • 99
  • 186
Subhendu
  • 11
  • 1
  • 6
  • 2
    Go grab a library that has Ajax built-in, like jQuery. No one bothers writing it from scratch these days. – Diodeus - James MacFarlane Sep 26 '13 at 18:58
  • What exactly is the issue here? If your second version works, can't you just use that? – David Sep 26 '13 at 18:58
  • As @Diodeus said, download jQuery. The documentation of it is very simple and easy to use since it has lots of examples. – Victor Sep 26 '13 at 19:00
  • Well done, Subhendu. If the question is resolved, then please upvote all answers that were helpful to you, and select a correct answer to close the question. – cssyphus Sep 27 '13 at 16:05

1 Answers1

2

Forgive me if you know this already, but in case you don't:

Ajax posts data to an external php file, which processes the data it receives, and sends back an answer. It looks like this:

FILE #1:

<html>
    <head>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $('#Sel').change(function() {
                    var opt = $(this).val();
                    var someelse = 'Hello';
                    var more_stuff = 'Goodbye';
                    $.ajax({
                        type: "POST",
                        url: "receiving_file.php",
                        data: 'selected_opt=' + opt + '&something_else=' +someelse+'&more_stuff='+more_stuff,
                        success:function(data){
                            alert('This was sent back: ' + data);
                        }
                    });
                });
            });
        </script>
    </head>
<body>

<select id = "Sel">
    <option value ="Song1">default value</option>
    <option value ="Song2">Break on through</option>
    <option value ="Song3">Time</option>
    <option value ="Song4">Money</option>
    <option value="Song5">Saucerful of Secrets</option>
</select>

FILE #2: receiving_file.php

<?php
    $recd = $_POST['selected_opt'];
    echo 'You chose: ' . $recd;

The above example works. If you copy/paste it into two files, you will see AJAX in action. Of course, the server side scripting is in PHP, so your server needs to handle that. If necessary, download XAMPP and install it on your local computer. Place these two files in the htdocs folder and, in the browser address bar, type:

http://localhost/whatever_you_called_the_first_page.php

As you can see, it is much simpler to write AJAX code using jQuery. All that is needed is the jQuery library (usually in the header tags, as in code example above), and the AJAX code block.

Here are some other examples to study:

More complicated example

Populate dropdown 2 based on selection in dropdown 1

Community
  • 1
  • 1
cssyphus
  • 37,875
  • 18
  • 96
  • 111