www.webdeveloper.com
Results 1 to 5 of 5

Thread: send autogenerated Email without form and stay on page

Hybrid View

  1. #1
    Join Date
    Jan 2013
    Posts
    3

    Question send autogenerated Email without form and stay on page

    Hello everyone,

    I know this is a widely covered subject, but I kept searching the Web and couldn't find a solution for my specific problem.

    What I am trying to do with jQuery/AJAX (as mentioned in the title):

    I DON'T want any sort of Form that the user can fill out.
    I just want a button (some HTML-Element) that when clicked sends an automatically generated mail to a predefined addess.
    Neither the content nor the subject or anything else needs to be "filled in" by the viewer/user.

    There will only be one JS-variable (a client-side script will insert the right value) that should be put somewhere in the content of the Email. (see "myVar" in Code)

    On clicking the button, the mail should be sent, the button should be replaced by a "Thanks whatever..."-message and that's IT! No redirecting to another page or whatnot.


    What I have so far for the JS Part: (in PHP-File)

    PHP Code:
    $script '
        <script>
            $("#notify").click(function(){
                $.ajax({
                    type: "POST",
                    url: "/.../sendEmail.php",
                    data: "' 
    "{ 'body': '" $email_content "' + myVar, 'to': '" $email_recipient "', 'from': '" $email_sender "', 'subject': '" $email_subject "' }" '",
                    dataType: "text",
                    statusCode: {
                        404: function() {
                            $("#notify").html("Could not contact server.");
                        },
                        500: function() {
                            $("#notify").html("A server-side error has occurred.");
                        }
                    },
                    error: function() {
                        $("#notify").html("A problem has occurred.");
                    },
                    success: function() {
                        $("#notify").html("Success!");
                    }
                });
                return false;
            });

    My relevant HTML part is simply
    Code:
    <p id="notify">Notify</p>
    .

    The sendEmail.php file:
    PHP Code:
    <?php

    $mailTo 
    $_POST['to'];
    $mailFrom $_POST['from'];
    $subject $_POST['subject'];
    $message $_POST['body'];

                
    mail($mailTo$subject$message"From: ".$mailFrom);
    ?>
    Obviously I made a mistake somewhere or forgot something because the "Notify" changes to "Success", but I receive no Email. (I am using my own Address for testing purposes.)

    For what it's worth: The Output (as shown in Sourcecode) seems to be correct with respect to syntax as well as Email adress etc.
    HTML Code:
    <script>
    	$("#notify").click(function(){
    		$.ajax({
    			type: "POST",
    			url: "/.../sendEmail.php",
    			data: "{ 'body': 'TEST', 'to': 'correct adress', 'from': 'correct adress', 'subject': 'Test' }",
    			dataType: "text",
    			statusCode: {
    				404: function() {
    					$("#notify").html("Could not contact server.");
    				},
    				500: function() {
    					$("#notify").html("A server-side error has occurred.");
    				}
    			},
    			error: function() {
    				$("#notify").html("A problem has occurred.");
    			},
    			success: function() {
    				$("#notify").html("Success!");
    			}
    		});
    		return false;
    	});
    </script>

    I still very much lack experience and would be very grateful for a solution, or even for a nudge in the right direction!

    If this problem has already been solved for someone on this site, I apologize. I didn't find it.

    Regards,
    ForceWare

  2. #2
    Join Date
    Jan 2013
    Posts
    4
    I would first try to send a very basic email from your site. I have no idea what your environment looks like, but you might need to configure the mail server if you have one. Are you using a hosting company or hosting the site yourself?

    Try to see if this code will deliver an email to you (change the to and from email addresses obviously)
    PHP Code:
    <?php 

    $mailTo 
    "email@toaddress.com";
    $mailFrom "email@fromaddress.com"
    $subject "test subject"
    $message "Test Body"

                 
    mail($mailTo$subject$message"From: ".$mailFrom); 
    ?>
    If it works, then your code is the problem and I suspect the ajax call.

  3. #3
    Join Date
    Jan 2013
    Posts
    3
    @ gvinson89

    Thanks for the reply!

    I am with a hosting company.

    Good idea with that test.
    Did it.
    Worked.
    Sending Emails that way works fine.

    So it looks like the AJAX part is missing something.

    Any help would be greatly appreciated!

  4. #4
    Join Date
    Jan 2013
    Posts
    3
    Yeah, for some reason my post was not published...

    Anyways...

    Thank you for the reply!
    I tested it. It worked, so the problem seems to be with the AJAX part.

    Any help would be greatly appreciated!

  5. #5
    Join Date
    Jan 2013
    Posts
    4
    You might try to include the $(document).ready() in your script. Try this:

    PHP Code:

    $script 

        <script type="text/javascript"> 
            $(document).ready(function()
            {
                $("#notify").click(function(){ 
                    $.ajax({ 
                        type: "POST", 
                        url: "sendEmail.php", 
                        data: "' 
    "{ 'body': '" $email_content "' + myVar, 'to': '" $email_recipient "', 'from': '" $email_sender "', 'subject': '" $email_subject "' }" '", 
                        dataType: "text", 
                        statusCode: { 
                            404: function() { 
                                $("#notify").html("Could not contact server."); 
                            }, 
                            500: function() { 
                                $("#notify").html("A server-side error has occurred."); 
                            } 
                        }, 
                        error: function() { 
                            $("#notify").html("A problem has occurred."); 
                        }, 
                        success: function() { 
                            $("#notify").html("Success!"); 
                        } 
                    }); 
                    return false; 
                }); 
            });
        </script>'
    ;
    print 
    $script

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles