Guest User

Untitled

a guest
Oct 20th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. protected $allowAnonymous = array('actionPostLead');
  2.  
  3. public function actionPostLead()
  4. {
  5. $this->requireAjaxRequest();
  6.  
  7. // Initialize the $kv array and query var for later use
  8. $kv = array();
  9. $query_string = '';
  10.  
  11. //If there are POST variables
  12. if ($_POST) {
  13.  
  14. // For each POST variable as $name_of_input_field => $value_of_input_field
  15. foreach ($_POST as $key => $value) {
  16.  
  17. // Set array element for each POST variable (ie. first_name=Arsham)
  18. $kv[] = stripslashes($key).'='.stripslashes($value);
  19. }
  20.  
  21. // Create a query string with join function separted by &
  22. $query_string = join('&', $kv);
  23. }
  24.  
  25. // Check to see if cURL is installed ...
  26. if (!function_exists('curl_init')) {
  27. die('Sorry cURL is not installed!');
  28. }
  29.  
  30. // The original form action URL from Step 2 :)
  31. $url = 'https://webto.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8';
  32.  
  33. // Open cURL connection
  34. $ch = curl_init();
  35.  
  36. // Set the url, number of POST vars, POST data
  37. curl_setopt($ch, CURLOPT_URL, $url);
  38. curl_setopt($ch, CURLOPT_POST, count($kv));
  39. curl_setopt($ch, CURLOPT_POSTFIELDS, $query_string);
  40.  
  41. // Execute SalesForce web to lead PHP cURL
  42. $result = curl_exec($ch);
  43.  
  44. // close cURL connection
  45. curl_close($ch);
  46.  
  47. if ($result) {
  48. $this->returnJson(array('success' => true));
  49. } else {
  50. $this->returnErrorJson($e->getMessage());
  51. }
  52. }
  53.  
  54. $.ajax({
  55. type: 'POST',
  56. url: '/',
  57. data: data,
  58. dataType: 'json',
  59. success: function(response) {
  60.  
  61. formBlock.removeClass('submitting');
  62. var successMessage = $('.formBlock.on').data('success-message');
  63. showFormSuccessBanner(successMessage);
  64. hideForm(id);
  65. },
  66. error: function(err) {
  67. console.log("Error");
  68. console.log(err);
  69.  
  70. formBlock.removeClass('submitting');
  71. }
  72. });
Add Comment
Please, Sign In to add comment