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

    }
}
4
  • 1
    Write the url of your handler page in url of $.ajax. This _.mailHandlerURL should be replaced by url of ashx page. Commented Aug 3, 2012 at 20:04
  • thats all i do? so the method will work exactly the same going to my handler file? Commented Aug 3, 2012 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. Commented Aug 3, 2012 at 20:10
  • ok thanks, That's what i've been trying and its not working. I guess I have issues somewhere else Commented Aug 3, 2012 at 20:14

1 Answer 1

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
});
Sign up to request clarification or add additional context in comments.

5 Comments

that doesn't work either. I get an error, but it says "undefinederror"
have updated my answer - you need a closing bracket and a semi colon - remove the last comma.
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
An error is at least a starting point - perhaps close this question and open a new one with error dump?
yeah..i think i can get it figured out now that i at least have errors

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.