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 = "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? Quote Link to comment Share on other sites More sharing options...
firestarter30 Posted March 23, 2011 Author Share Posted March 23, 2011 UPDATE : JSON error corrected Quote Link to comment 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 = "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 Quote Link to comment 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(); } }); Quote Link to comment 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? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.