Jump to content

problem with array() while using the mail() function


firestarter30

Recommended Posts

I have been having some issues with the above functions and while the mail was sent , it was looking kinda weird  :-\

Im facing 2 problems:

1st: implode() , or print_r() didnt worked for me , i guess i should be using a foreach statement but i never stadied seriously the arrays.

2nd: Always a JSON parse error, or invalid JSON error comes out when the email() code exitsts...

 

Here is the code below

 

$name = trim(stripslashes(strip_tags($_POST['name'])));	  		
$email = trim(stripslashes(strip_tags($_POST['email'])));
$url = trim(stripslashes(strip_tags($_POST['url'])));
$title = trim(stripslashes(strip_tags($_POST['title'])));
$anchortext = trim(stripslashes(strip_tags($_POST['anchortext'])));
$description = trim(stripslashes(strip_tags($_POST['description'])));
$dropdown = $_POST['dropdown'];


$receiver = "[email protected]" ;
$email_body = array($name,$email,$url,$title,$anchortext,$description,$dropdown);
if (!empty($name) & !empty($email) && !empty($url) && !empty($title) && !empty($anchortext) && !empty($description) && !empty($dropdown) ){

// i guess all juice goes here 

$send = mail($receiver, 'Link Exchange Request', "$email_body", "From: {$email}");
  if ($send) {
        echo 'true'; 
    }

}

I would like the mail to be presented like

Name : blabla

Email : [email protected]

Url : blablablabla

and so on ..

 

Suggestions?

I usually let PHP to the work for me:

<?php
$tmp = array();
$missing = array();
foreach ($_POST as $k=>$v) {
   switch($k) {
      case 'name':
      case 'email':
      case 'url':
      case 'title':
      case 'anchortext':
      case 'description':
      case 'dropdown':
             if (strlen(trim(stripslashes(strip_tags($v)))) > 0) {
                 $tmp[] = ucwords($k) . ': ' . trim(stripslashes(strip_tags($v)));
             } else {
                 $missing[] = $k;
             }
             break;
   }
}
$receiver = "[email protected]" ;
if (empty($missing)) {
   $body = implode("\n",$tmp);
   $send = mail($receiver, 'Link Exchange Request', $body, "From: $email");
   exit (json_encode(array('ret'=>'true')));
} else {
   exit (json_encode(array('ret'=>'false',implode(',',$missing))));
}
?>

 

I'm assuming that you're getting a JSON error because this code is being called via AJAX.  The "echo 'true'" is not a proper JSON string. The exit statements in my code above are proper JSON strings.

 

Ken

Hmmmm

I go this error while using the array :

There was an SyntaxError: JSON.parse error due to a parsererror condition.

 

 

This is the ajax call with the error status massages to help you a little understand my logic

 

function submitForm(formData) {

$.ajax({	
	type: 'POST',
	url: 'feedback.php',		
	data: formData,
	dataType: 'json',
	cache: false,
	timeout: 5000,
	success: function(data) { 			

		$('form #response').removeClass().addClass((data.error === true) ? 'error' : 'success')
					.html(data.msg).fadeIn('fast');	

		if ($('form #response').hasClass('success')) {

			setTimeout("$('form #response').fadeOut('fast')", 5000);
		}

	},
	error: function(XMLHttpRequest, textStatus, errorThrown) {

		$('form #response').removeClass().addClass('error')
					.html('<p>There was an<strong> ' + errorThrown +
						  '</strong> error due to a<strong> ' + textStatus +
						  '</strong> condition.</p>').fadeIn('fast');			
	},				
	complete: function(XMLHttpRequest, status) { 			

		$('form')[0].reset();
	}
});	

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.