Advertisement
Guest User

Untitled

a guest
May 10th, 2012
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. JSON returning null with jQuery $.ajax post type
  2. <form name="newsletterSignup" id="newsletterSignup" method="post">
  3.  
  4. <input type="text" name="firstName" id="firstName" type="text" autocomplete="off" placeholder="Enter your name" />
  5.  
  6. <input type="text" name="email" id="email" type="text" autocomplete="off" placeholder="Enter a valid email address"/>
  7.  
  8. <input type="submit" name="newsletterSubscribe" id="newsletterSubscribe" value="Sign me up now!" />
  9.  
  10. </form>
  11.  
  12. var BASE_URL = 'http://www.thegurucoder.com/';
  13.  
  14. $('#newsletterSignup').submit(function()
  15. {
  16.  
  17. var ajaxURL = BASE_URL + '_php/ajax/newsletterSubscribe.php';
  18.  
  19. $.ajax({
  20. type: 'POST',
  21. url: ajaxURL,
  22. data: { firstName:$('#newsletterSignup #firstName').val(), email:$('#newsletterSignup #email').val()},
  23. dataType: 'json',
  24. success: function(data){
  25.  
  26. alert(data);
  27.  
  28. if(data.redirect == 'true')
  29. {
  30.  
  31. window.location = data.url;
  32.  
  33. }
  34. else
  35. {
  36.  
  37. $(this).notify({prependTo:'#master', type:'negative', message:data.message, delay:3200, animation:'slide'});
  38.  
  39. }
  40.  
  41. }
  42. });
  43.  
  44. return false;
  45.  
  46. });
  47.  
  48. function newsletterSubscribe()
  49.  
  50. {
  51.  
  52. if(isset($_POST['newsletterSubscribe']))
  53. {
  54.  
  55. header('Content-Type: application/json');
  56.  
  57. $db = new Connection(DB_HOST, DB_USER, DB_PASS, DB_NAME);
  58.  
  59. $firstName = $_POST['firstName'];
  60. $email = $_POST['email'];
  61.  
  62. if($email != '')
  63. {
  64.  
  65. if(isDuplicateValue('subscribers', 'email', $email))
  66. {
  67.  
  68. echo json_encode(array(
  69. 'message' => 'You have already subscribed to this newsletter.',
  70. 'redirect' => 'false'
  71. ));
  72.  
  73. }
  74. else
  75. {
  76.  
  77. $parameters = array(
  78. 'table' => 'subscribers',
  79. 'fieldsAndValues' => array(
  80. 'firstName' => $firstName,
  81. 'email' => $email,
  82. 'addedDate' => datetime()
  83. )
  84. );
  85.  
  86. $db->insert($parameters);
  87.  
  88. echo json_encode(array(
  89. 'redirect' => 'true',
  90. 'url' => BASE_URL.'subscribed'
  91. ));
  92.  
  93. }
  94.  
  95. }
  96. else
  97. {
  98.  
  99. echo json_encode(array(
  100. 'message' => 'You did not enter a valid email address.',
  101. 'redirect' => 'false'
  102. ));
  103.  
  104. }
  105.  
  106. }
  107.  
  108. }
  109.  
  110. Request URL:http://www.thegurucoder.com/_php/ajax/newsletterSubscribe.php
  111. Request Method:POST
  112. Status Code:200 OK
  113. Request Headersview source
  114. Accept:application/json, text/javascript, */*; q=0.01
  115. Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
  116. Accept-Encoding:gzip,deflate,sdch
  117. Accept-Language:en-US,en;q=0.8
  118. Connection:keep-alive
  119. Content-Length:17
  120. Content-Type:application/x-www-form-urlencoded
  121. Cookie:PHPSESSID=f3015d3c6fa08e53fc562477da0f563f
  122. Host:www.thegurucoder.com
  123. Origin:http://www.thegurucoder.com
  124. Referer:http://www.thegurucoder.com/blog
  125. User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.63 Safari/535.7
  126. X-Requested-With:XMLHttpRequest
  127. Form Dataview URL encoded
  128. firstName:
  129. email:
  130. Response Headersview source
  131. Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0
  132. Connection:close
  133. Content-Encoding:gzip
  134. Content-Language:en-UK
  135. Content-Length:20
  136. Content-Type:text/html; charset=UTF-8
  137. Date:Thu, 05 Jan 2012 15:21:54 GMT
  138. Expires:Thu, 19 Nov 1981 08:52:00 GMT
  139. Pragma:no-cache
  140. Server:Apache/2.2.16 (Unix) mod_ssl/2.2.16 OpenSSL/0.9.8e-fips-rhel5 mod_bwlimited/1.4
  141. Vary:Accept-Encoding
  142. X-Powered-By:PHP/5.2.14
  143.  
  144. var first_name = $('#newsletterSignup > #firstName').val();
  145. var email = $('#newsletterSignup > #email').val();
  146. $.ajax({
  147. type: 'POST',
  148. url: ajaxURL,
  149. data: { firstName:first_name, email:email},
  150. dataType: 'json',
  151. ...
  152. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement