Forum Moderators: open
xmlHttp.open("POST","whatever.php", true)
if(!xmlHttp.open("POST","whatever.php", true)) {alert("xmlHttp.open failed");}
function ajax() {
if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
{
xmlHttp.open("POST","whatever.php", true);
if(!xmlHttp.open("POST","whatever.php", true)) {alert("xmlHttp.open failed");}
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");
xmlHttp.onreadystatechange = handleServerResponse;
xmlHttp.send();
}
else setTimeout('ajax()',1000);
}
Event thread: click
Uncaught exception: ReferenceError: Undefined variable: whatever
Error thrown at line 55, column 2 in ajax() in [localhost...]
if(!xmlHttp.open("POST",whatever.php, true))
called from line 1, column 0 in <anonymous function>(event):
ajax()
another thing is, i have done the same thing before without using ajax, submitting a form using the $_POST array with php, now since i'm trying this with ajax i can forget about that right?
..and then those params sent with ajax are stored in the $_POST array that i can use in my php script, as $_POST['value1']= var1 etc?
'whatever.php' is not a valid internet address
whatever
whatever.php. However, in your code you posted:
function ajax() {
if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
{
xmlHttp.open("POST","whatever.php", true);
if(!xmlHttp.open("POST","whatever.php", true)) {alert("xmlHttp.open failed");}
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");
xmlHttp.onreadystatechange = handleServerResponse;
xmlHttp.send();
}
else setTimeout('ajax()',1000);
}
function ajax() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
xmlHttp.open("POST","whatever.php", true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Content-length", params.length);
xmlHttp.setRequestHeader("Connection", "close");
xmlHttp.onreadystatechange = handleServerResponse;
xmlHttp.send(''); //note we are still missing any parameters to send here... will address later below in example
} else {
setTimeout(ajax, 1000); //never pass strings to setTimeout (I know the tutorials show it often,
//but that's wrong, use functions or anonymous functions only)
}
}
I need to check for empty textfields and build a parameter string, all with javascript and then send that as the send method parameter in the form
"value1="+var1+"&value2="+var2 etc.. is that right?
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<title>Simple ajax form</title>
<script type="text/javascript">
function getXhr() {
var xhr = null;
try{//Mozilla, Safari, IE 7+...
xhr = new XMLHttpRequest();
if (xhr.overrideMimeType) {
xhr.overrideMimeType('text/xml');
}
} catch(e) {// IE 6, use only Msxml2.XMLHTTP.(6 or 3).0,
//see: http://blogs.msdn.com/xmlteam/archive/2006/10/23/using-the-right-version-of-msxml-in-internet-explorer.aspx
try{
xhr = new ActiveXObject("Msxml2.XMLHTTP.6.0");
}catch(e){
try{
xhr = new ActiveXObject("Msxml2.XMLHTTP.3.0");
}catch(e){}
}
}
return xhr;
}
function ajax(form) {
var xhr = getXhr(),
url = 'POST_Echoer.php',
value1 = form.field1.value,
value2 = form.field2.value,
requestID = new Date().getTime(),
//url query string variable values which may contain special characters,
//such as spaces, ?, etc. should be escaped, use encodeURIComponent:
query = 'value1='+ encodeURIComponent(value1) +'&value2='+ encodeURIComponent(value2);
query += '&requestID='+ requestID;
xhr.onreadystatechange = function () {
if (xhr.status == 200 && xhr.readyState == 4) {
alert(xhr.responseText.replace(/<br>/g, '\n'));
}
};
xhr.open('POST', url, true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.setRequestHeader('Content-Length', query.length);
xhr.setRequestHeader('Cache-Control', 'no-cache');
xhr.setRequestHeader("Connection", "close");
xhr.send(query);
}
</script>
</head>
<body>
<div>
<form name="someForm" action="POST_Echoer.php" method="post">
<p><input type="text" name="field1" value="Enter a value, or not..." size="25"><br>
<input type="text" name="field2" value="Enter another value, or not..." size="25"><br>
<input type="submit" name="submit" value="Submit" onclick="ajax(this.form); return false;">
</p></form>
</div>
</body>
</html>
<?php
//>POST_Echoer.php
$s = (count($_POST)) ? 'POST fields received:<br><br>' : 'No POST fields received!';
foreach ($_POST as $key => $value) {
$s .= $key.' => '.$value.'<br>';
}
echo $s;
?>
[edited by: astupidname at 9:19 am (utc) on Jun 8, 2011]
if (xhr === null) {
return null;
}
xhr.onreadystatechange = etcetera etcetera...
function ajax()
{
var xmlHttp = createXmlHttpRequestObject(); //create request object
var params = buildparams(); //query string
var url = 'whatever.php';
xmlHttp.open("POST",url, true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Content-length", params.length);
xmlHttp.setRequestHeader('Cache-Control', 'no-cache');
xmlHttp.setRequestHeader("Connection", "close");
xmlHttp.onreadystatechange = function() {
if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
{
var textResponse = xmlHttp.responseText;
document.getElementById('servcont').innerHTML = textResponse;
}
}//end function (onreadystatechange)
xmlHttp.send(params);
}
i was lucky enough to notice once only for about half a second before it disappeared
.....
.....
why does the textresponse disappear straight away after being loaded