SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Dec 2012
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [AJAX] The problem with sending emails (variable post)

    I have a problem that the script sends me an email but no data from the form fields. So the problem is with the sending POST variable. What am I doing wrong?

    I have two files:
    index.php
    Code:
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
    
            <form id="formail" action="" method="post">
    
              <?php echo $blad_wysylania; ?>
    
              <div id="wynik_wysylania"></div>
    
              <div><label for="e_nazwa">Podpis: </label><input id="e_nazwa" type="text" name="e_nazwa" /></div>
    
              <div><label for="e_mail">Twój e-mail: </label><input id="e_mail" type="text" name="e_mail" /></div>
    
              <div><label for="e_temat">Temat: </label><input id="e_temat" type="text" name="e_temat" /></div>
    
              <div><label for="e_tresc">Treść: </label><textarea id="e_tresc" name="e_tresc" rows="10" cols="30"></textarea></div>
    
              <div><input type="submit" id="e_wyslij" name="e_wyslij" value="Wyślij e-mail" /></div>
    
          </form>			
    
     
    
    			</div><!-- #content -->
    
    		</div><!-- #container -->
    
     
    
    <script type="text/javascript">
    
     
    
     
    
    $("#e_wyslij").click(function(){
    
    	var valid_x = '';
    
    	var name_x = $("#e_nazwa").val();
    
    	var mail_x = $("#e_mail").val();
    
    	var subject_x = $("#e_temat").val();
    
    	var text_x = $("#e_tresc").val();
    
    	if (name_x.length<1) {
    
    		valid_x += '<br />Błędny podpis.';
    
    	}
    
    	if (!mail_x.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i)) {
    
    		valid_x += '<br />Błędny Email.';
    
    	}
    
    	if (subject_x.length<1) {
    
    		valid_x += '<br />Błędny tytuł.';
    
    	}
    
    	if (text_x.length<1) {
    
    		valid_x += '<br />Błędna treść.';
    
    	}
    
    	if (valid_x!='') {
    
    		$("#wynik_wysylania").fadeIn("slow");
    
    		$("#wynik_wysylania").html("Error:"+valid_x);
    
    	}
    
    	else {
    
    		var datastr ='name_x=' + name_x + '&mail_x=' + mail_x + '&subject_x=' + subject_x + '&text_x=' + text_x;
    
    		//var datastr = { 'name_x': name_x, 'mail_x': mail_x, 'subject_x': subject_x, 'text_x': text_x }
    
    		$("#wynik_wysylania").css("display", "inline-block");
    
    		$("#wynik_wysylania").html("Wysyłanie wiadomości .... ");
    
    		$("#wynik_wysylania").fadeIn("slow");
    
    		setTimeout("send('" + datastr + "')",1000);
    
    	}
    
    	return false;
    
    });
    
     
    
    function send(datastr){
    
    	$.ajax({	
    
    		type: "POST",
    
    		url: "x_mail.php",
    
    		data: datastr,
    
    		cache: false,
    
    		success: function(html){
    
    		$("#wynik_wysylania").fadeIn("slow");
    
    		$("#wynik_wysylania").html(html);
    
        //$("#wynik_wysylania").html("Wysłane!");
    
    		//setTimeout('$("#wynik_wysylania").fadeOut("slow")',2000);
    
    		setTimeout('$("#wynik_wysylania").fadeOut("slow")',1000);
    
    	}
    
    	});
    
    } 
    
    </script>
    x_mail.php:
    Code:
    <?php
     $mail_x = $_GET['e_mail'];
    
     $name_x = $_GET['e_nazwa'];
    
     $subject_x = $_GET['e_temat'];
    
     $text_x = $_GET['e_tresc'];
    
     
    
    $to_x = "zxc@tlen.pl";
    
     $message_x ="You received  a mail from ".$mail_x;
    
     $message_x .="Text of the message : ".$text_x;
    
     
    
    if(mail($to_x, $subject_x,$message_x)){
    
     echo "mail successful send";
    
     }
    
     else{
    
     echo "theres some errors to send the mail, verify your server options";
    
     }
    
     
    
     
    
     ?>

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,198
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Your PHP code is currently using _GET when your Ajax request is using _POST, simply change all your $_GET's to $_POST's and the issue will be fixed.

  3. #3
    SitePoint Member
    Join Date
    Dec 2012
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, I forgot, before I changed the GET (in ajax too).Fixed the POST and still not working.

  4. #4
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,198
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    The other thing I just noticed is your array keys aren't the same as whats in your PHP. E.g.

    PHP Code:
    $mail_x $_GET['e_mail']; // In your JavaScript this is called mail_x not e_mail 

  5. #5
    SitePoint Member
    Join Date
    Dec 2012
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    THX VERY MUTCH!


Bookmarks

Posting Permissions

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