0

So I'm trying to teach myself asp.net along with javascript. I downloaded a website template and converted it to an asp site. It has a contact form which the javascript called a php function to send an email. I can't use php to send email on my hosting site.

The template included a file MailHandler.ashx that I should be able to use to send mail with asp. I have done my research, and I believe I have this code in working order. My question is, where the javascript used to call the PHP file, how do I convert that to call this MailHandler file?

This is the relevant code calling my php file:

$.ajax({
    type: "POST",
    url:_.mailHandlerURL,   //this value is the path to the php file
    data:{
        name:_.getValFromLabel($('.name',_.form)),
        email:_.getValFromLabel($('.email',_.form)),
        phone:_.getValFromLabel($('.phone',_.form)),
        fax:_.getValFromLabel($('.fax',_.form)),
        state:_.getValFromLabel($('.state',_.form)),
        message:_.getValFromLabel($('.message',_.form)),
        owner_email:_.ownerEmail,
        stripHTML:_.stripHTML
},

I understand that this is invoking a post method to the php file, so how do I invoke a post method do my Mailhandler file?

This is my handler file:

public class Handler : IHttpHandler {
    public void ProcessRequest (HttpContext context) {

             //create mail client, create message, send email

    }
}
CSharpDev
  • 869
  • 1
  • 13
  • 22
  • 1
    Write the url of your handler page in `url` of `$.ajax`. This `_.mailHandlerURL` should be replaced by url of `ashx` page. – Jashwant Aug 03 '12 at 20:04
  • thats all i do? so the method will work exactly the same going to my handler file? – CSharpDev Aug 03 '12 at 20:05
  • 1
    This will do a post ajax request to your ashx page. In the `ProcessRequest` function , do your coding to send email. – Jashwant Aug 03 '12 at 20:10
  • ok thanks, That's what i've been trying and its not working. I guess I have issues somewhere else – CSharpDev Aug 03 '12 at 20:14

1 Answers1

1

Check this earlier post $.get, $.post, $.ajax, $(elm).load to .ashx page problem i believe your problem is the formatting - see the data: attribute.

You definitely need to set _.mailHandlerURL to the url of the ashx file.

I believe it should look more like this:

var dataParams = "name="+_.getValFromLabel($('.name',_.form));
    dataParams += ", email="+_.getValFromLabel($('.name',_.form));
        // etc for the rest 
$.ajax({
    type: "POST",
    url:_.mailHandlerURL,   //this value is the path to the php file
    data:dataParams
},

If this code is exactly as if you need to add a closing bracket after the last } in the java script and add a semi-colon instead of a comma:

$.ajax({
    type: "POST",
    url:_.mailHandlerURL,   //this value is the path to the php file
    data:dataParams
});
Community
  • 1
  • 1
Luke Baughan
  • 4,658
  • 3
  • 31
  • 54
  • that doesn't work either. I get an error, but it says "undefinederror" – CSharpDev Aug 03 '12 at 21:23
  • have updated my answer - you need a closing bracket and a semi colon - remove the last comma. – Luke Baughan Aug 03 '12 at 21:36
  • Well, I actually have more code after that. I did finally get an error though..The request filtering module is configured to deny a path in the URL that contains a hiddenSegment section. Must have something to do with permissions on my folder, or maybe the way my web server has things configured – CSharpDev Aug 03 '12 at 21:40
  • An error is at least a starting point - perhaps close this question and open a new one with error dump? – Luke Baughan Aug 03 '12 at 22:14
  • yeah..i think i can get it figured out now that i at least have errors – CSharpDev Aug 03 '12 at 23:28