Ich will einen Array, den ich in das JSON-Format bringe, aus einer php-Datei mit AJAX wie folgt auslesen:
Hier die PHP-Datei:
...das klappt aber leider nicht, es kommt nichts, wenn ich auf den Button drücke. Ich verstehe nicht, woran das liegen könnte.
HTML Code:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <link rel="stylesheet" type="text/css" href="css\mystyle.css"> <link rel="icon" href="favicon.ico" type="image/x-icon"/> <link rel="shortcut icon" href="favicon.ico" type="image/x-icon"/> </head> <body> <script> function init(){ var request; if (window.XMLHttpRequest) { request = new XMLHttpRequest(); // Mozilla, Safari, Opera } else if (window.ActiveXObject) { try { request = new ActiveXObject('Msxml2.XMLHTTP'); // IE 5 } catch (e) { try { request = new ActiveXObject('Microsoft.XMLHTTP'); // IE 6 } catch (e) {} } } var url = 'dbread.php'; request.open("GET", url, true); request.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); request.send(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { document.getElementById("demo").innerHTML = this.responseText; } }; } </script> <div id="demo"> </div> <input type="button" value="Klick" onclick="init()"> </body> </html>
PHP Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="css\mystyle.css">
<link rel="icon" href="favicon.ico" type="image/x-icon"/>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon"/>
<title>initial page</title>
</head>
<body>
<?php
$array = array(
'firstname' => 'Elke',
'lastname' => 'Mueller',
'street' => 'Hansstrasse',
'number' => '12',
'postal' => '12312',
'town' => 'Mannheim',
'country' => 'Germany',
'email' => '[email protected]',
'birthday' => '12.12.1212'
);
$myJSON = json_encode($array)
?>
</body>
</html>
Comment