ajax og php: sende flere variabler, hjælp til udbygning af script
hej,jeg er netop begyndt at snuse lidt til ajax. Jeg har fundet følgende script som jeg har forsøgt at udbygge. Jeg ønsker at sende flere værdier med POST, og troede derfor løsningen var at indsætte de linier jeg har markeret med fed, men det fungerer ikke. Kan i hjælpe?
<html>
<head>
<title>PHP using AJAX</title>
<script type="text/javascript">
var time_variable;
function getXMLObject() //XML OBJECT
{
var xmlHttp = false;
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP") // For Old Microsoft Browsers
}
catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP") // For Microsoft IE 6.0+
}
catch (e2) {
xmlHttp = false // No Browser accepts the XMLHTTP Object then false
}
}
if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
xmlHttp = new XMLHttpRequest(); //For Mozilla, Opera Browsers
}
return xmlHttp; // Mandatory Statement returning the ajax object created
}
var xmlhttp = new getXMLObject(); //xmlhttp holds the ajax object
function ajaxFunction() {
var getdate = new Date(); //Used to prevent caching during ajax call
if(xmlhttp) {
var txtname = document.getElementById("txtname");
var age = document.getElementById("age");
xmlhttp.open("POST","testing.php",true); //calling testing.php using POST method
xmlhttp.onreadystatechange = handleServerResponse;
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlhttp.send("txtname=" + txtname.value); //Posting txtname to PHP File
xmlhttp.send("age=" + age.value);
}
}
function handleServerResponse() {
if (xmlhttp.readyState == 4) {
if(xmlhttp.status == 200) {
document.getElementById("message").innerHTML=xmlhttp.responseText; //Update the HTML Form element
}
else {
alert("Error during AJAX call. Please try again");
}
}
}
</script>
<body>
<form name="myForm">
<table>
<tr>
<td>Enter Name</td>
<td><input type="text" name="txtname" id="txtname" /></td>
</tr>
<tr>
<td>Enter age</td>
<td><input type="text" name="age" id="age" /></td>
</tr>
<tr>
<td colspan="2"><input type="button" value="Submit" onClick="ajaxFunction();" /></td>
</tr>
</table>
<div id="message" name="message"></div>
</form>
</body>
</head>
</html>
