Yes, you'd need AJAX to do this without refreshing.
Code: Select all
<script type="text/javascript" language="javascript">
//This actually makes an http_request object(the thign we use for ajax
//to the url you passed. This isn't a really cross browser method
//but should work in most, but you should definetly do some checking
//on that before you use this on anything important
function makeRequest(url)
{
var http_request = false;
// Pretty much everyone calls it this except, you know who
if (window.XMLHttpRequest)
{
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType)
{
http_request.overrideMimeType('text/xml');
// See note below about this line
}
}
else if (window.ActiveXObject)
{ // Yeah these guys..
try
{
//This is the newest version so we want that first
http_request = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
}
}
}
if (!http_request)
{
//:( NO AJAX OBJECT
return false;
}
//This is where the actual "ajax" takes place. What happens is
//Everytime the "status" changes of the http_request object
//the XMLHttpRequest class will call in this case alertContents with
//the http_request object passed
http_request.onreadystatechange = function() { alertContents(http_request); };
//This makes a simple GET request to the URL. Posts are possible, but
//a little more complicated
http_request.open('GET', url, true);
http_request.send(null);
}
function alertContents(http_request)
{
//the readyState is what state the object is in 1-4
//4 means it's finsihed executing and you can go on
//and process the response
if (http_request.readyState == 4)
{
//Javascript to handle your responseText now
document.getElementById.("MyFormElement").value = http_request.reponseText;
}
}
function submitform()
{
makeRequest("http://www.mysite.com/myphpscriptthatneedstodothings.php");
}
</script>
<form name = "myform" action="javascript:submitform();">
<textarea rows=10 colos=10 id="MyFormElement"></textarea>
</form>
Something like that.. That's not a really good AJAX example but it's a place to start.
Remember AJAX requires javascript which is annoying to a lot of people, so don't make
it a requirement. Also there's TONS of checks that need to occur for browser portability,
mostly when making the XMLHTTPRequest object. There's about a dozen different names,
different browser versions call it different things and it's a mess.
Here's a few good AJAX info
http://www.ajaxtutorial.net/ - not so much a tutorial, but has lots of links/resources
http://www.webpasties.com/xmlHttpRequest/