Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Querying database via Javascript - AJAX? XmlHttp? 1

Status
Not open for further replies.

cmayo

MIS
Apr 23, 2001
159
US
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

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>
 
If you change the last param in your 'open' from true to false, the request will be synchronous instead of asynchronous.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Hi

Chuck said:
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.
If you care about your data integrity, better do all the checking right before the SQL operations.
Chuck said:
without the xmlHttp.onreadystatechange part, I'm not getting any data back in xmlHttp.responseText.
You should.
Code:
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,false)
xmlHttp.send(null)

if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
    // do something with the xmlHttp.responseText
}

Feherke.
 
Aw heck, hang on... I think I got it doing what I want. The 3rd argument to the xmlHttp.open method defines whether the method executes asynchronously. Setting it to 'false' forces it to synchronous.

xmlHttp.open("GET",url,false)

Doh!
 
Sheesh, you guys are FAST! Thanks very much for your replies.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top