1

OK so I am trying this whole AJAX thing and I hope I am doing this correctly. Here's the code:

function checkDate(str) {
        if (str == "") {
            document.getElementById("dateMessage").innerHTML = "";
            return;
        } else { 
            if (window.XMLHttpRequest) {
                // code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            } else {
                // code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    document.getElementById("dateMessage").innerHTML = xmlhttp.responseText;
                }
            }
            xmlhttp.open("GET","scripts/checkDate.php?date="+str,true);
            xmlhttp.send();

        }
    }
    function registerBTN(str) {
        if (str == "") {
            document.getElementById("register").innerHTML = "";
            return;
        } else { 
            if (window.XMLHttpRequest) {
                // code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            } else {
                // code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    document.getElementById("register").innerHTML = xmlhttp.responseText;
                }
            }
            var enableBTN = <?php echo $_SESSION["date"] ?>
            if (enableBTN) {
                $("register").removeAttr("disabled");
            }else{
                $("register").attr("disabled", "disabled");
            }
        }
    }

So the PHP calls checkDate() just fine and it works.

Here's what I (think) I want to do.

1) In checkDate() just after the xmlhttp.send() I want to call registerBTN. I am not sure how to do that.

2) I cannot confirm if registerBTN works at all. It should enable or disable based on the PHP session variable which is controlled in checkDate.php

Please let me know if you need any further information from me.

Thank you for any help you can give!

Sebas
  • 21,192
  • 9
  • 55
  • 109
KeeganS
  • 13
  • 4
  • this: `var enableBTN = ` looks very suspicious. Check console for errors – Sebas Jan 19 '15 at 04:25
  • 1
    why not just echo an attribute in PHP inside the markup. add disabled when not set. – Kevin Jan 19 '15 at 04:27
  • I got the echo statement from: http://stackoverflow.com/questions/1808108/how-to-access-php-variables-in-javascript-or-jquery-rather-than-php-echo-vari – KeeganS Jan 19 '15 at 23:22
  • @Ghost I get that, but how do I make that dynamic based on the result from checkDate ? – KeeganS Jan 19 '15 at 23:23
  • @KeeganS what does the `registerBTN` supposed to do anyways? there is not enough context to go on, should it be working side by side with `checkDate()`? why not do them in the same request? – Kevin Jan 20 '15 at 01:54
  • @Ghost registerBTN adds or removes the disable attribute based on the $_Session variable which checkDate sets in the referenced php file. – KeeganS Jan 21 '15 at 02:55

0 Answers0