Jump to content

[SOLVED] First simple AJAX test script fails


65bit

Recommended Posts

I'm trying to get my first AJAX action to work.  I'd like to fire a .php script on the server to send an email and don't need any response sent back to the calling page.  Maybe not too real world, but seems it should work?  :)

 

I plucked the mail portion for one of my pages where it's actually working, so don't think that's the issue.  And, my js alert boxes are popping up in dowork(), so I know it's making it that far.  I commented the last 2 lines of dowork() out, as it seems I don't need them for what I'm trying to do, but have tried it with them as well with the same results.  send_mail.php sits in the same folder as the .js

 

I'm new to php and web stuff as well, so unfortunately, my mis-understanding could be about anywhere.

 

Here's my my .js file:

 

var xmlHttp;
function GetXmlHttpObject()
{
  xmlHttp=null;
  if (window.XMLHttpRequest)
  {	
    // code for all new browsers
    xmlHttp=new XMLHttpRequest();
  }
  else if (window.ActiveXObject)
  {
    // code for IE5 and IE6
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  else if (xmlHttp=null)
  {
    alert("Your browser does not support XMLHTTP.");
  }
  return xmlHttp;
}

function doWork()
{
  httpObject = GetXmlHttpObject();
  if (httpObject != null) 
  {
    alert("xmlHttp has been set according to the browser type ...");
    httpObject.open("GET", "send_mail.php", true);
   	alert("after the http object calls the php script ... ");
    //httpObject.send(null);
    //httpObject.onreadystatechange = setOutput;
  }
}

 

and here's send_mail.php

 

<?
  mail("jdoe@comcast.net", 		
    "Test Message From PHP Script", 
    "AJAX has worked!!", 
    "From: jdoe@comcast.net" . "\r\n");	
?>

 

Any pointers are appreciated!

 

Thanks

Link to comment
Share on other sites

Interestingly, it only seems to send the email the first time the event happens. 

 

The alert boxes keep popping up, so doWork() is being called, but the .php won't send the email again after the first time unless I close the tab / browser and go back in.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.