junaidkaps Posted July 15, 2012 Share Posted July 15, 2012 Hi There! I have a process_form.php that 1. Checks all entries that were filled in the form and 2. Sends out the form entries as an e-mail via Pear Mail & SMTP. Unfortunately, I have no experience whatsoever in PHP and have simply pulled off a piece of code from Codecanyon. My process_form.php file does part 1 perfectly, but doesn't do part 2 and instead according to Firefox's console I receive the following error: [21:02:32.882] JSON.parse: unexpected non-whitespace character after JSON data @ http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js:2 Here is a portion of the code in question without parameters required for part 2: if (empty($this->error_list)) { if ($this->is_xhr) echo json_encode(array('status' => 'success')); $this->process(); } else { if ($this->is_xhr) echo json_encode(array('status' => 'invalid', 'errors' => $this->error_list)); else echo $this->error_list; } } } function process() { /** * SUCCESS!! * There were no errors in the form. Insert your processing logic here (i.e. send an email, save to a * database etc. * * All of the submitted fields are available in the $this->fields variable. * * * IMPORTANT - PLEASE READ: * 1. YOU MUST UNCOMMENT THE CODE FOR IT TO WORK. * - This means removing the '//' in front of each line. * - If you do not know what php comments are see here: http://php.net/manual/en/language.basic-syntax.comments.php * * 2. YOU CAN ENTER ANY EMAIL ADDRESS IN THE $from VARIABLE. * - This is the address that will show in the From column in your mail application. * - If your form contains an email field, and you want to use that value as the $from variable, you can set $from = $this->fields['name of your email field']; * * 3. FILE ATTACHMENTS * - As stated in the description on codecanyon, this code does not mail attachments. Google 'php html email attachments' for information on how to do this * * 4. REDIRECTING TO ANOTHER PAGE AFTER SUBMISSION * - This is an ajax enabled form, so you need to perform the redirection in main.js AND this php file. * a. Please see instructions in main.js for redirection. This is for users without JS. * b. Replace the relevant code below with the page you would like to redirect to. REMEMBER TO UNCOMMENT THE LINE FOR IT TO WORK. */ /*********************************************/ /* SAMPLE MAIL CODE */ /*********************************************/ // $msg = "Form Contents: \n\n"; // foreach($this->fields as $key => $field) // $msg .= "$key : $field \n"; // $to = '[email protected]'; // $subject = 'Form Submission'; // $from = '[email protected]'; // mail($to, $subject, $msg, "From: $from\r\nReply-To: $from\r\nReturn-Path: $from\r\n"); /************************************************************************************/ /* REDIRECTION CODE */ /* Only uncomment the line below if you want to redirect to another page */ /* when the form has been submitted */ /************************************************************************************/ // if (!$this->is_xhr) // header('Location: http://www.kbisksa.com/registration_thankyou.html'); } Using this piece of code I successfully get the "success" notification on my webpage. However as soon as I modify the process function i receive the JSON error listed above and nothing occurs. Here is what my code looks like when this happens: if (empty($this->error_list)) { if ($this->is_xhr) echo json_encode(array('status' => 'success')); $this->process(); } else { if ($this->is_xhr) echo json_encode(array('status' => 'invalid', 'errors' => $this->error_list)); else echo $this->error_list; } } } function process() { //Required Include: This is PEAR Mail include('Mail.php'); $mail =& Mail::factory("smtp", $smtp_params); $smtp_params["host"] = "mail.domain.com"; $smtp_params["port"] = "25"; $smtp_params["auth"] = true; $smtp_params["username"] = "[email protected]"; // you made in plesk $smtp_params["password"] = "mypass"; //you set this in plesk for the email account $to = '[email protected]'; $from ='[email protected]'; $subject = 'Form Submission'; $msg = "Form Contents: \n\n"; foreach($this->fields as $key => $field) $msg .= "$key : $field \n"; $result = $mail->send($to, $subject, $msg, "From: $from\r\nReply-To: $from\r\nReturn-Path: $from\r\n"); /** * SUCCESS!! * There were no errors in the form. Insert your processing logic here (i.e. send an email, save to a * database etc. * * All of the submitted fields are available in the $this->fields variable. * * * IMPORTANT - PLEASE READ: * 1. YOU MUST UNCOMMENT THE CODE FOR IT TO WORK. * - This means removing the '//' in front of each line. * - If you do not know what php comments are see here: http://php.net/manual/en/language.basic-syntax.comments.php * * 2. YOU CAN ENTER ANY EMAIL ADDRESS IN THE $from VARIABLE. * - This is the address that will show in the From column in your mail application. * - If your form contains an email field, and you want to use that value as the $from variable, you can set $from = $this->fields['name of your email field']; * * 3. FILE ATTACHMENTS * - As stated in the description on codecanyon, this code does not mail attachments. Google 'php html email attachments' for information on how to do this * * 4. REDIRECTING TO ANOTHER PAGE AFTER SUBMISSION * - This is an ajax enabled form, so you need to perform the redirection in main.js AND this php file. * a. Please see instructions in main.js for redirection. This is for users without JS. * b. Replace the relevant code below with the page you would like to redirect to. REMEMBER TO UNCOMMENT THE LINE FOR IT TO WORK. */ /*********************************************/ /* SAMPLE MAIL CODE */ /*********************************************/ // $msg = "Form Contents: \n\n"; // foreach($this->fields as $key => $field) // $msg .= "$key : $field \n"; // $to = '[email protected]'; // $subject = 'Form Submission'; // $from = '[email protected]'; // mail($to, $subject, $msg, "From: $from\r\nReply-To: $from\r\nReturn-Path: $from\r\n"); /************************************************************************************/ /* REDIRECTION CODE */ /* Only uncomment the line below if you want to redirect to another page */ /* when the form has been submitted */ /************************************************************************************/ // if (!$this->is_xhr) // header('Location: http://www.kbisksa.com/registration_thankyou.html'); } I'm using Pear mail as that is the only option I have with my host who mandates smtp authentication. Any help here would be most appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/265687-process_formphp-json-error/ Share on other sites More sharing options...
hakimserwa Posted July 15, 2012 Share Posted July 15, 2012 make sure the function process() { is closed after your function code Quote Link to comment https://forums.phpfreaks.com/topic/265687-process_formphp-json-error/#findComment-1361620 Share on other sites More sharing options...
junaidkaps Posted July 15, 2012 Author Share Posted July 15, 2012 Yep made sure but it still won't budge :-(. Quote Link to comment https://forums.phpfreaks.com/topic/265687-process_formphp-json-error/#findComment-1361719 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.