Results 1 to 7 of 7

Thread: confused on result of php mail ...

  1. #1
    Join Date
    Jul 2012

    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):


    this is my php

    PHP Code:
    //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";
    mail($to$subject$message'From:' $email));

    this is my js section that handles the mail
    sendform = function(){
    	var name = $('input#name').val();
    	var email = $('input#email').val();
    	var message = $('#message').val();
    	var dataString = 'name=' + name + '&email=' + email + '&message=' + message;
    			$('#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>")
    						  .fadeIn(1500, function(){
    									.append("<img id='checkmark' src='images/check.png' />");
    			setTimeout(function() {
    	return false;  //so page doesn't reload

  2. #2
    Join Date
    Sep 2011
    Bristol, England, United Kingdom
    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))
    "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
    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
    Bristol, England, United Kingdom
    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);


    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

    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
    Bristol, England, United Kingdom
    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 "";
    get_php "";
        if (
    window.XMLHttpRequest// IE7+, Firefox, Chrome, Opera, Safari.
    xmlhttp = new XMLHttpRequest();
    // IE6, IE5.
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    xmlhttp.onreadystatechange = function()
    xmlhttp.readyState == && xmlhttp.status == 200)
    // The "get_php" variable contains all data outputted by PHP.
    get_php xmlhttp.responseText;
    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.
    else if(
    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
    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



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.20074 seconds
  • Memory Usage 2,940KB
  • Queries Executed 15 (?)
More Information
Template Usage (34):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (1)bbcode_code
  • (6)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (7)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (7)postbit
  • (7)postbit_onlinestatus
  • (7)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates