www.webdeveloper.com
Results 1 to 7 of 7

Thread: confused on result of php mail ...

  1. #1
    Join Date
    Jul 2012
    Posts
    4

    confused on result of php mail ...

    Hi, I was trying to do this on my local wamp server...but to no avail...and getting some kind of error but it was in html code, when using "alert"..but after putting into a tag, it displays a tabel with the error message....

    just to clarify, the error is no longer a problem, I tested it on the actual server and it emailed just fine...but my question is...if my php code only had two echos...one for positive sending "success" and for an error "error"...then why I am I getting all this other junk in the result?

    The image of the result in the alert box is this: (code follows image):

    http://i179.photobucket.com/albums/w...lynchin/ex.png


    this is my php

    PHP Code:
    <?php
        
    if(isset($_POST['email']))
        {
            
    //send email
            
    $to "sales@vskonneformen.com.au";
            
    $subject "Enquiry from VSKONNE.au";
            
    $email $_POST['email'];
            
    $name $_POST['name'];
            
    $message $_POST['message'];
            
    $headers 'From:' $email;
            
    $headers.="Return-Path:<" $email ">\r\n";
            if(
    mail($to$subject$message'From:' $email));
            echo 
    "Success";
        }
        else 
            echo 
    "Error";
    ?>


    this is my js section that handles the mail
    Code:
    sendform = function(){
    	var name = $('input#name').val();
    	var email = $('input#email').val();
    	var message = $('#message').val();
    	var dataString = 'name=' + name + '&email=' + email + '&message=' + message;
    
    	$.ajax({
    	dataType:"text",
    		type:"POST",
    		url:"process.php",
    		data:dataString, 
    		success:function(data){
    		alert(data);
    			$('#popup_form form').css('display','none');
    			$('#popup_form').append("<div id='response'></div>");
    			$('#response').html("<h3>Contact Form Submitted!</h3>")
    						  .append("<p>We will be in touch soon.</p>")
    						  .hide()
    						  .fadeIn(1500, function(){
    								$('#popup_form')
    									.append("<img id='checkmark' src='images/check.png' />");
    							});
    			setTimeout(function() {
    				$('#checkmark').remove();
    				$('#response').remove();
    				$('#popup_form')
    					.fadeOut(500)
    					.css('z-index','-1000');
    				$('#overlay')
    					.fadeOut(500)
    					.css('z-index','-999');
    				$('input[type=text],input[type=email],textarea').val("");
    				},3000);
    		}
    	});
    	return false;  //so page doesn't reload
    };

  2. #2
    Join Date
    Sep 2011
    Location
    Bristol, England, United Kingdom
    Posts
    192
    Your $_POST was being checked against first, which would always have come true in the event that your form was correctly submitted.

    This makes no sense. It'll work exactly the same with or without your terminated if-condition I'm not sure if you typed the semi-colon by accident, but your "Success" line is not part of your if-statement, it's going to get executed no matter what the mail function returns:
    PHP Code:
    if(mail($to$subject$message'From:' $email)); 
    What you should have done is this:

    PHP Code:
    if(mail($to$subject$message'From:' $email))
    {
        echo 
    "Success"
    }
    else
    {
        echo 
    "Mail function failed.";

    This would then have caused your mail function to display a basic error on-page, but not the actual fail code of the function itself.

    I'm not familiar with jQuery, but I can see you're using AJAX calls from JavaScript. I am familiar with error messages showing up in alert dialogs because I'm a fan of AJAX myself, but I use raw JavaScript and not jQuery.

    The reason for this is your HTTP response text (the data sent back to JavaScript from PHP) happens to be the error from the mail function, which would otherwise have simply been printed out in the browser if you weren't using AJAX. This is one of the annoying things about AJAX, it's hard to debug because things don't always work as you expect them to.
    Last edited by George88; 07-12-2012 at 03:34 PM.

  3. #3
    Join Date
    Jul 2012
    Posts
    4
    Thanks for explaining that.... It's nice to know someone knows how it works...lol.

    So, how would I actually be able to override that message with my custom message? . ...so pretty much if it fails, I have a variable with the"error"in it.. I don't need to know the error, but read the echoed result from the php file...and then I can compare it ..if it says error display an error message .if success then a success message. .thanks for the help

  4. #4
    Join Date
    Sep 2011
    Location
    Bristol, England, United Kingdom
    Posts
    192
    If you want to override the error reporting on a particular function, you will have to slightly alter your code. You can use the "at" operator to suppress error messages, but only when using an expression. The PHP.net site has a description: http://php.net/manual/en/language.op...rorcontrol.php

    Their rule of thumb is that if the construct of your choice is able to have its return value stored, you can suppress the errors it will produce.

    This means you would need to store your mail function return value in a variable like so:
    PHP Code:
    $send_mail = @mail($to$subject$message'From:' $email);

    if(
    $send_mail)
    {
        echo 
    "Success";
    }
    else
    {
        echo 
    "Failed.";

    I don't know for sure if this will actually work for the function in question, but I'm confident it should. There are always other options, like suppressing error reporting all together, which I don't recommend.

    Anyway, I don't know what your requirements are so I can't say whether this code will suffice, but I expect it to work as intended.
    Last edited by George88; 07-12-2012 at 06:41 PM.

  5. #5
    Join Date
    Jul 2012
    Posts
    4
    GREAT...thanks

    by the way...I can't remember off the top of my head...how do you actually get the result from the php though? I mean if it echos for example "success"...how from javascript or even jquery, do I recieve that info?

  6. #6
    Join Date
    Sep 2011
    Location
    Bristol, England, United Kingdom
    Posts
    192
    In your code? I don't know.

    As I said I don't use jQuery so I don't fully understand how your code is working. I'm assuing you've copied & pasted it since you don't know yourself.

    However, in raw JavaScript you would receive back the responseText object. The responseText object is a string object which contains within it all the data which was echoed from the PHP file. In essence, anything outputted by the PHP file can be considered responseText.

    If you can figure out for yourself where your responseText is being handled, you can store it in a variable, whereupon you will be able to check it for a string.

    I can only give you JavaScript code, so if you don't understand the concept of what I'm explaining, I won't be able to help you any further.

    PHP Code:
    // JavaScript code

        
    var xmlhttp "";
        var 
    get_php "";
     
        if (
    window.XMLHttpRequest// IE7+, Firefox, Chrome, Opera, Safari.
            
    xmlhttp = new XMLHttpRequest();
        else 
    // IE6, IE5.
            
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
     
        
    xmlhttp.onreadystatechange = function()
        {
            if(
    xmlhttp.readyState == && xmlhttp.status == 200)
            {
                
    // The "get_php" variable contains all data outputted by PHP.
                
    get_php xmlhttp.responseText;
                
    alert(get_php);
            }
        }
        
    xmlhttp.open("GET""ajax_php.php"true);
        
    xmlhttp.send(); 
    From the looks of your code, I'd say the raw JavaScript method for AJAX is actually simpler and easier than the JQuery method. If you're interested, I recently wrote an article on how to retrieve PHP data through JavaScript on my new web site. The code is very simple; I'd say much simpler than yours despite the jQuery.

    As for your question of how to tell in JavaScript whether or not a success or failure was outputted by PHP, that can be achieved with a simple match() statement or perhaps a very basic regular expression. Something like this should work:

    PHP Code:
    // JavaScript code

    // You'll have to fill this line in yourself, I don't know the jQuery.
    var your_response_variable some_response_text;

    // Search for success or failure.
    if(your_response_variable.match(/success/i))
    {
        
    alert("Success");
    }
    else if(
    your_response_variable.match(/failure/i))
    {
        
    alert("Failure");
    }
    else
    {
        
    alert("Unknown response");

    Inside the match function I am using a very basic regular expression. Anything inside the // will be the string checked for. The "i" tells the expression to ignore case.

    Hope that helps; I recommend using JavaScript over jQuery but if you're able to get it working, that's fine.

  7. #7
    Join Date
    Jul 2012
    Posts
    4
    Oh I remember now...thanks to you

    Yea I know Javascript too...I just used j query because it was less lines of code but I may just go back and change it...thanks!

Thread Information

Users Browsing this Thread

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

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