-1

I've been trying to get this solved for 2 weeks now and still have no success.

JavaScript:

var quotenum = 0;
var xmlhttp = null;
var rt = "";

function ChangeQuote() {
    quotenum++;
    xmlhttp = null;
    //alert("quotenum= "+quotenum);
    if (quotenum === 0) {
        document.getElementById("quote").innerHTML = "";
        return;
    }
    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 === XMLHttpRequest.DONE && xmlhttp.status === 200) {
            rt = xmlhttp.responseText;
            //alert("quote= "+rt);
            alert("request number= " + xmlhttp.length);
            document.getElementById("quote").innerHTML = rt;
        }
    };
    xmlhttp.open("Get", "getquote.php?q=" + quotenum, false);
    //xmlhttp.open("GET", "getquote.php?XDEBUG_SESSION_START=netbeans-xdebug&q=" + quotenum, false);
    xmlhttp.send();

    //var thediv = document.getElementById("quote");   
    return false;
}

PHP:

error_reporting(E_ERROR | E_PARSE);
$q="";
$q = intval($_GET['q']);
$link=mysqli_connect("localhost","root","sequence","babylon");

if (mysqli_connect_errno()){
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$query="SELECT quotetext FROM quote where quotenum='".$q."'";
$show=mysqli_query($link,$query) or die ("Error");
while($row=mysqli_fetch_array($show)){
    echo $row["quotetext"];
}

Can anyone see anything wrong with this? Using WAMP I can see the correct result when I run the PHP file in a browser. I also try to use Jquery instead.

            <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

            <script>
                var quotenum = 0;
                // var xmlhttp = null;
                // var rt = "";
                function ChangeQuote() {
                    quotenum++;
                    $.ajax({
                        url: "getquote.php?q="+quotenum,
                        method: "get",
                        data: {
                            q: quotenum
                        }
                    }).done(function (data) {
                        alert(data);
                        document.getElementById("quote").innerHTML = data.quotetext;
                    });
                    return false;
                }
            </script>
Reggie
  • 47
  • 7
  • You are vulnerable to [sql injection attacks](http://bobby-tables.com) – Marc B Jul 25 '16 at 20:03
  • Not certain that DONE is a valid property. `if (xmlhttp.readyState === XMLHttpRequest.DONE && xmlhttp.status === 200)` I'd try xmlHttp.readyState === 4 `if (xmlHttp.readyState === 4 && xmlhttp.status === 200)` – Will Jul 25 '16 at 20:10
  • The alert in the block appears... alert("request number= "+xmlhttp.length); Although the value is undefined. I did try xmlHttp.readyState === 4 but it made no difference – Reggie Jul 25 '16 at 21:00
  • `xmlhttp.length` *would* be undefined; there's no such property. Did you mean `xmlhttp.responseText.length` (or its alias here, `rt.length`)? – Paul Roub Jul 25 '16 at 21:43
  • 2
    any errors/warnings in the [JavaScript console](http://webmasters.stackexchange.com/questions/8525/how-to-open-the-javascript-console-in-different-browsers)? – Paul Roub Jul 25 '16 at 21:43
  • Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/. This was showing in the Javascript console. – Reggie Jul 25 '16 at 21:58
  • Why aren't you using jQuery? Ajax requests are simple and fast with it + you have less code to write. http://api.jquery.com/jquery.ajax/ – Pero Jul 25 '16 at 22:15
  • OK I'm using JQuery now but all it does is return to last visited page and does not update the innerHTML of the document element which was my aim. – Reggie Jul 26 '16 at 14:28
  • [obligatory] you're vulnerable to SQL injection attacks. You might be liable. – Ryan Jul 26 '16 at 14:36
  • You should not have inline js with a `script` tag that has a `src` attribute. http://stackoverflow.com/questions/1056325/javascript-inline-script-with-src-attribute – Clyde Lobo Jul 26 '16 at 14:36
  • 1
    https://xkcd.com/327/ – Clyde Lobo Jul 26 '16 at 14:37
  • How are you calling `ChangeQuote`? Sounds like you're submitting a form and canceling the Ajax request. – Quentin Jul 26 '16 at 15:51
  • I call it by clicking the following
    How am I cancelling the ajax request?
    – Reggie Jul 26 '16 at 15:56
  • It looks like what I'm trying to do is not possible. My web page is created in Netbeans using Java Server faces framework and Glassfish server.In order for my ajax code to work I would need a PHP servere as well. – Reggie Jul 28 '16 at 19:19
  • So far I have not found a way to include a PHP server in the NetBeans Project. If anyone has any suggestions I would be grateful. Thanks to all who tried to help me. – Reggie Jul 28 '16 at 19:22

1 Answers1

0

The only noticable error I see is you inlining your js with the script tag that has a src attribute.

HTML 4.01 Specification:

The script may be defined within the contents of the SCRIPT element or in an external file. If the src attribute is not set, user agents must interpret the contents of the element as the script. If the src has a URI value, user agents must ignore the element's contents and retrieve the script via the URI.

Clyde Lobo
  • 9,126
  • 7
  • 34
  • 61