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 = "example@gmail.com" ;
$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 : blabla@blabla.com

Url : blablablabla

and so on ..

 

Suggestions?

Link to comment
Share on other sites

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 = "example@gmail.com" ;
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

Link to comment
Share on other sites

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();
	}
});	

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.