Trying to get PHP to return the value of a button. Not sure what's went wrong but it's returning these errors:
"Notice: Undefined index: data in /storage/ssd4/271/3416271/public_html/ajaxTest.php on line 2 Notice: Undefined index: data in /storage/ssd4/271/3416271/public_html/ajaxTest.php on line 5"
When I print_r($get) it returns an empty array. The HTML code looks fine so I imagine I messed up the AJAX?
(thank you in advance for any help given!)
Here's my HTML, js and PHP for reference:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="ajaxTest.js"></script>
<link rel="stylesheet" type="text/css" href="style.css" />
<title>AJAX test</title>
</head>
<body>
<div id="area">
<h2>Sending Data to the Server</h2>
<form action="" method="GET">
<button type="submit" name="data" value="1" onclick="getData('ajaxTest.php', 'moreText')">1</button>
<button type="submit" name="data" value="2" onclick="getData('ajaxTest.php', 'moreText')">2</button>
</form>
<br />
<p id="moreText">The fetched message should appear here.</p>
</div>
</body>
</html>
var XMLHttpRequestObject = false;
if(window.XMLHttpRequest) {
XMLHttpRequestObject = new XMLHttpRequest();
}
else if(window.ActiveXObject){
XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
}
function getData(dataSource, divID) {
if(XMLHttpRequestObject){
var obj = document.getElementById(divID);
XMLHttpRequestObject.open("GET", dataSource);
XMLHttpRequestObject.onreadystatechange = function() {
if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
obj.innerHTML = XMLHttpRequestObject.responseText;
}
}
XMLHttpRequestObject.send();
}
}
<?php
if ($_GET["data"] == "1") {
echo 'The server got a value of 1';
}
if ($_GET["data"] == "2") {
echo 'The server got a value of 2';
}
?>