firestarter30 Posted March 23, 2011 Share Posted March 23, 2011 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? Link to comment https://forums.phpfreaks.com/topic/231527-problem-with-array-while-using-the-mail-function/ Share on other sites More sharing options...
firestarter30 Posted March 23, 2011 Author Share Posted March 23, 2011 UPDATE : JSON error corrected Link to comment https://forums.phpfreaks.com/topic/231527-problem-with-array-while-using-the-mail-function/#findComment-1191469 Share on other sites More sharing options...
kenrbnsn Posted March 23, 2011 Share Posted March 23, 2011 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 Link to comment https://forums.phpfreaks.com/topic/231527-problem-with-array-while-using-the-mail-function/#findComment-1191471 Share on other sites More sharing options...
firestarter30 Posted March 24, 2011 Author Share Posted March 24, 2011 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 https://forums.phpfreaks.com/topic/231527-problem-with-array-while-using-the-mail-function/#findComment-1191649 Share on other sites More sharing options...
firestarter30 Posted March 26, 2011 Author Share Posted March 26, 2011 @kenrbnsn I get the JSON parse error only when the mail function is inside the script any ideas why? Link to comment https://forums.phpfreaks.com/topic/231527-problem-with-array-while-using-the-mail-function/#findComment-1192518 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.