i tried something based on rob cherny's hijax example, i don't know if it's any good for you, since we don't know what you want to achieve...
it's a little long to read, but that's because ajax. i highlighted the interesting parts:
Code:
<!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=utf-8">
<title>empty</title>
<script type="text/javascript">
var hijaxObj=
{
keycode:0,
makeXHRObj:function()
{
var XHRObj=false;
if (window.XMLHttpRequest)
{
XHRObj=new XMLHttpRequest();
if (XHRObj.overrideMimeType) {XHRObj.overrideMimeType("text/xml");}
} else if (window.ActiveXObject)
{
try {XHRObj=new ActiveXObject("Msxml2.XMLHTTP");}
catch(e)
{
try {XHRObj=new ActiveXObject("Microsoft.XMLHTTP");}
catch(f) {alert("Your browser does not support Ajax!");}
}
}
return XHRObj;
},
xhrPost:function(x,q)
{
x.open("POST","hijax_mod.php5",true);
x.setRequestHeader("Content-Type",'application/x-www-form-urlencoded');
x.onreadystatechange=function()
{
if (x.readyState===4 && x.status===200)
{
window.document.getElementById("output").innerHTML=x.responseText;
}
};
x.send(q);
},
startHijax:function()
{
var country, form=window.document.getElementsByTagName("form")[0],
query, username, xhrObj=hijaxObj.makeXHRObj();
if (xhrObj)
{
form.onsubmit=function()
{
country=window.document.getElementById("country").value;
username=window.document.getElementById("username").value;
query="ajax=true&username="+encodeURI(username)+"&country="+encodeURI(country);
if (this.keycode === 13) query+="&enter=true"; //if the last pressed key was enter
hijaxObj.xhrPost(xhrObj,query);
return false;
};
}
form.onkeypress=function(event) //cannot be on the whole document, because of event bubbling
{
e = event || window.event;
this.keycode = e.which || e.keyCode;
}
}
};
window.onload=hijaxObj.startHijax;
</script>
</head>
<body>
<h1>Form:</h1>
<div id="output">
<form action="hijax_mod.php5" method="post">
<p>
<label for="username">Your Name:</label>
<input type="text" id="username" name="username">
</p>
<p>
<label for="country">Country:</label>
<input type="text" id="country" name="country">
</p>
<p><input type="submit"></p>
</form>
</div>
</body>
</html>
hijax_mod.php5:
Code:
<?php
$results=$_POST['username']." is from ".$_POST['country'];
if (!isset($_POST['ajax']))
{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Accessible Ajax Form Test.</title>
</head>
<body>
<?php
}
?>
<p>Here is my post --
<?php
if (isset($_POST['enter']))
{
$results=$results." - form submitted with enter";
} else if (isset($_POST['ajax']))
{
$results=$results." - form submitted with ajax";
} else
{
$results=$results." - form submitted without javascript";
}
echo $results;
?>
</p>
<?php
if (!isset($_POST['ajax']))
{
?>
</body>
</html>
<?php
}
?>
this should output:
"submitted without javascript", when js is off
"submitted with ajax", when js is on and it was submitted by clicking on the button
"submitted with enter", when js is on and it was submitted by hitting the enter key
any comments welcome...
Bookmarks