1

I have this code, to simple update "cupom" from "0" to "1", but it isnt working with Chrome, with firefox it does work, any help/advice is welcome.

var req;
function val_impressao_js(cpf) {


if(window.XMLHttpRequest) {
req = new XMLHttpRequest();
}
else if(window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
}
var url = "val_impressao.php?cpf="+cpf;
req.open("GET", url, true);

req.onreadystatechange = function() 
    {
    if(req.readyState == 4 && req.status == 200) 
        {

            window.print();

        }
    }
req.send(null);
}

val_impressao.php

require "arqinc/conexao.php";
require "arqinc/funcoesbd.php";

    $cpf=$_GET['cpf'];
    $query=mysql_query("UPDATE cadcoo SET cupom=1 WHERE cpf_cadpessoafisica=$cpf AND cupom=0");

And by the way, this part isnt working too, it does not print the page.

if(req.readyState == 4 && req.status == 200) 
    {

        window.print();

    }
user1773801
  • 55
  • 2
  • 7

1 Answers1

0

I suggest the following script, as it can even work with IE7 and all the modern browsers.

window.onload = initAll;
var xhr = false;

function initAll() {
    document.getElementById("requestXML").onclick = makeRequest;
}

function makeRequest() {
    if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();
    }
    else {
        if (window.ActiveXObject) {
            try {
                xhr = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) { }
        }
    }

    if (xhr) {
        xhr.onreadystatechange = showContents;
        xhr.open("GET", "us-states.xml", true);
        xhr.send(null);
    }
    else {
        document.getElementById("updateArea").innerHTML = "Sorry, but I couldn't create an XMLHttpRequest";
    }
    return false;
}

function showContents() {
    if (xhr.readyState == 4) {
        if (xhr.status == 200) {
            var outMsg = xhr.responseText;
        }
        else {
            var outMsg = "There was a problem with the request " + xhr.status;
        }
        document.getElementById("updateArea").innerHTML = outMsg;
    }
}
  • There is nothing wrong with that code, it is from Lynda.com tutorial's exercise files, and It works like a charm. It may be due to your server/browser malfunctionality –  Feb 18 '13 at 17:10
  • Can be something due to Localhost ? – user1773801 Feb 18 '13 at 17:12
  • Possibly, If it is only with Chrome the problem, you probably should re-install chrome and see what happens. As, the only different method implemented in the script if for the older versions of IE, as they can not understand `XMLHttpRequest` apart from IE, all share the same method. Good luck –  Feb 18 '13 at 17:23
  • Disable chrome's web security and it should work. the following link shows how: http://stackoverflow.com/questions/3102819/disable-same-origin-policy-in-chrome – E_X Dec 07 '15 at 08:46