Giovarco Giovarco - 2 months ago 12
Ajax Question

Uncaught ReferenceError: http is not defined

For the first time I'm studying HTML, CSS, PHP and Javascript, so consider myself a noob. First of all:


  • My browser is Windows 7 Google Chrome

  • I have disabled cache in Chrome and use Cache Killer extension

  • I use EasyPHP-DevServer-14.1VC11 to local host my server



The project I'm working for university is intended to manage a small database. What I would like to do is deleting a record with the (X) button in Scorciatoia (Shortcut).

Site + Console



The button HTML code is written dinamically, but each button looks like this:

<button type="button" onclick="cancella_record(25)">X</button>


That 25 is an ID. It is extracted with PHP through a query. Every ID is unique.

And there it is the javascript that is called:

<script>

// Credenziali
var nickname;
var password;

function cancella_record(ID) {

ottieni_nickname_password();

//document.getElementById("prova").innerHTML = nickname+" "+password;

aux_cancella_record(ID);
}

function ottieni_nickname_password() {

// Preparazione all'AJAX
var xhttp;
xhttp = new XMLHttpRequest();

// Poiché le variabili di sessioni non sono disponibili da javascript, bisogna richiederle al server
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var credenziali = this.responseText;
var arr = credenziali.split(",");
nickname = arr[0];
password = arr[1];
}
};

// Richiede nickname e password
xhttp.open("GET", "snippets/get_nickname_password.php", false);
xhttp.send();
}

function aux_cancella_record(ID) {

//document.getElementById("prova").innerHTML = nickname+" "+password;

// Preparazione all'AJAX
var xhttp;
xhttp = new XMLHttpRequest();

xhttp.onreadystatechange = function() {
document.getElementById("prova").innerHTML = "A";
if (this.readyState == 4 && this.status == 200) {
document.getElementById("prova").innerHTML = this.responseText;
}
};

http.open("POST", "snippets/scorciatoia_cancella_record.php", false);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("ID="+ID+"&nickname="+nickname+"&password="+password);

}




My ottieni_nickname_password() fuction works perfectly, but aux_cancella_record(ID) doesn't. According to Chrome console, the error lies in:

http.open("POST", "snippets/scorciatoia_cancella_record.php", false);


(Note: that false is intended to be so even in the other AJAX because I noticed I had some synchronization problem)

The error itself is:

Uncaught ReferenceError: http is not defined


I left some commented codes, so you could understand more or less what I tried to make it work (anyway nothing complex, just some chance to some InnerHTML to stamp my variables.)

Other files:

scorciatoia_cancella_record.php:

<?php
/*$ID = $_POST['ID'];
$nickname = $_POST['nickname'];
$password = $_POST['password'];

echo $ID." ".$nickname." ".$password;*/

echo "Scritta di prova";

/*require 'snippets\connetti_a_DB.php';
$conn = connetti_a_DB($username, $password);*/

?>


get_nickname_password.php:

<?php
session_start();
echo $_SESSION["nickname"].",".$_SESSION["password"];
?>


I hope I explained my situation nicely and you can help me somehow. One note: Security isn't so important in this project, however any advice is well-accepted.

Answer

The error you get clearly specifies what is you mistake. You are using http.open and http is not defined anywhere.

Solution is simple rename http to xhhtp in your code because you have defined

var xhttp;
xhttp = new XMLHttpRequest();

and not http