Hi all,
I'm working on a widget for a PHP project which will warn users when they're about to create duplicate database record. Rather than going through the rigamarole of checking for existing records after the form is submitted, I started looking for ways to check the database before allowing the form submission to proceed.
I'm way out of my depth here, but I found a bit of AJAX code (at bottom - at least I think it's AJAX) that seems to allow me to do what I need - call an external PHP script to query the database for dupes and return results to Javascript - but the asynchronous nature of AJAX is tripping me up... I need to run this check synchronously so I can abort the form submission if duplicates are found.
So maybe instead of AJAX, all I need is the GetXmlHttpObject functionality, but if I just run the
part, without the xmlHttp.onreadystatechange part, I'm not getting any data back in xmlHttp.responseText.
Sorry to be so clueless, and maybe I'm involving unnecessary technologies here, but could someone tell me where I'm going wrong?
Thanks,
Chuck
I'm working on a widget for a PHP project which will warn users when they're about to create duplicate database record. Rather than going through the rigamarole of checking for existing records after the form is submitted, I started looking for ways to check the database before allowing the form submission to proceed.
I'm way out of my depth here, but I found a bit of AJAX code (at bottom - at least I think it's AJAX) that seems to allow me to do what I need - call an external PHP script to query the database for dupes and return results to Javascript - but the asynchronous nature of AJAX is tripping me up... I need to run this check synchronously so I can abort the form submission if duplicates are found.
So maybe instead of AJAX, all I need is the GetXmlHttpObject functionality, but if I just run the
Code:
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
part, without the xmlHttp.onreadystatechange part, I'm not getting any data back in xmlHttp.responseText.
Sorry to be so clueless, and maybe I'm involving unnecessary technologies here, but could someone tell me where I'm going wrong?
Thanks,
Chuck
Code:
<script>
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
url=url+"&sid="+Math.random()
url="[URL unfurl="true"]www.somesite.com";[/URL]
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
// do something with the xmlHttp.responseText
}
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
//Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
</script>