Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [JavaScript]Formularz kontaktowy
Forum PHP.pl > Forum > Przedszkole
danussiaaa
Mam problem z skryptem formularza kontaktowego.
  1. //forms,js
  2. ;(function($){
  3. $.fn.forms=function(o){
  4. return this.each(function(){
  5. var th=$(this)
  6. ,_=th.data('forms')||{
  7. errorCl:'error',
  8. emptyCl:'empty',
  9. invalidCl:'invalid',
  10. notRequiredCl:'notRequired',
  11. successCl:'success',
  12. successShow:'4000',
  13. mailHandlerURL:'bat/MailHandler.php',
  14. ownerEmail:'adres@webmastera.com',
  15. stripHTML:true,
  16. smtpMailServer:'localhost',
  17. targets:'input,textarea',
  18. controls:'a[data-type=reset],a[data-type=submit]',
  19. validate:true,
  20. rx:{
  21. ".name":{rx:/^[a-zA-Z'][a-zA-Z-' ]+[a-zA-Z']?$/,target:'input'},
  22. ".state":{rx:/^[a-zA-Z'][a-zA-Z-' ]+[a-zA-Z']?$/,target:'input'},
  23. ".email":{rx:/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i,target:'input'},
  24. ".phone":{rx:/^\+?(\d[\d\-\+\(\) ]{5,}\d$)/,target:'input'},
  25. ".fax":{rx:/^\+?(\d[\d\-\+\(\) ]{5,}\d$)/,target:'input'},
  26. ".message":{rx:/.{20}/,target:'textarea'}
  27. },
  28. preFu:function(){
  29. _.labels.each(function(){
  30. var label=$(this),
  31. inp=$(_.targets,this),
  32. defVal=inp.val(),
  33. trueVal=(function(){
  34. var tmp=inp.is('input')?(tmp=label.html().match(/value=['"](.+?)['"].+/),!!tmp&&!!tmp[1]&&tmp[1]):inp.html()
  35. return defVal==''?defVal:tmp
  36. })()
  37. trueVal!=defVal
  38. &&inp.val(defVal=trueVal||defVal)
  39. label.data({defVal:defVal})
  40. inp
  41. .bind('focus',function(){
  42. inp.val()==defVal
  43. &&(inp.val(''),_.hideEmptyFu(label),label.removeClass(_.invalidCl))
  44. })
  45. .bind('blur',function(){
  46. _.validateFu(label)
  47. if(_.isEmpty(label))
  48. inp.val(defVal)
  49. ,_.hideErrorFu(label.removeClass(_.invalidCl))
  50. })
  51. .bind('keyup',function(){
  52. label.hasClass(_.invalidCl)
  53. &&_.validateFu(label)
  54. })
  55. label.find('.'+_.errorCl+',.'+_.emptyCl).css({display:'block'}).hide()
  56. })
  57. _.success=$('.'+_.successCl,_.form).hide()
  58. },
  59. isRequired:function(el){
  60. return !el.hasClass(_.notRequiredCl)
  61. },
  62. isValid:function(el){
  63. var ret=true
  64. $.each(_.rx,function(k,d){
  65. if(el.is(k))
  66. ret=d.rx.test(el.find(d.target).val())
  67. })
  68. return ret
  69. },
  70. isEmpty:function(el){
  71. var tmp
  72. return (tmp=el.find(_.targets).val())==''||tmp==el.data('defVal')
  73. },
  74. validateFu:function(el){
  75. el.each(function(){
  76. var th=$(this)
  77. ,req=_.isRequired(th)
  78. ,empty=_.isEmpty(th)
  79. ,valid=_.isValid(th)
  80.  
  81. if(empty&&req)
  82. _.showEmptyFu(th.addClass(_.invalidCl))
  83. else
  84. _.hideEmptyFu(th.removeClass(_.invalidCl))
  85.  
  86. if(!empty)
  87. if(valid)
  88. _.hideErrorFu(th.removeClass(_.invalidCl))
  89. else
  90. _.showErrorFu(th.addClass(_.invalidCl))
  91. })
  92. },
  93. getValFromLabel:function(label){
  94. var val=$('input,textarea',label).val()
  95. ,defVal=label.data('defVal')
  96. return label.length?val==defVal?'nope':val:'nope'
  97. }
  98. ,submitFu:function(){
  99. _.validateFu(_.labels)
  100. if(!_.form.has('.'+_.invalidCl).length)
  101. $.ajax({
  102. type: "POST",
  103. url:_.mailHandlerURL,
  104. data:{
  105. name:_.getValFromLabel($('.name',_.form)),
  106. email:_.getValFromLabel($('.email',_.form)),
  107. phone:_.getValFromLabel($('.phone',_.form)),
  108. fax:_.getValFromLabel($('.fax',_.form)),
  109. state:_.getValFromLabel($('.state',_.form)),
  110. message:_.getValFromLabel($('.message',_.form)),
  111. owner_email:_.ownerEmail,
  112. stripHTML:_.stripHTML
  113. },
  114. success: function(){
  115. _.showFu()
  116. }
  117. })
  118. },
  119. showFu:function(){
  120. _.success.slideDown(function(){
  121. setTimeout(function(){
  122. _.success.slideUp()
  123. _.form.trigger('reset')
  124. },_.successShow)
  125. })
  126. },
  127. controlsFu:function(){
  128. $(_.controls,_.form).each(function(){
  129. var th=$(this)
  130. th
  131. .bind('click',function(){
  132. _.form.trigger(th.data('type'))
  133. return false
  134. })
  135. })
  136. },
  137. showErrorFu:function(label){
  138. label.find('.'+_.errorCl).slideDown()
  139. },
  140. hideErrorFu:function(label){
  141. label.find('.'+_.errorCl).slideUp()
  142. },
  143. showEmptyFu:function(label){
  144. label.find('.'+_.emptyCl).slideDown()
  145. _.hideErrorFu(label)
  146. },
  147. hideEmptyFu:function(label){
  148. label.find('.'+_.emptyCl).slideUp()
  149. },
  150. init:function(){
  151. _.form=_.me
  152. _.labels=$('label',_.form)
  153.  
  154. _.preFu()
  155.  
  156. _.controlsFu()
  157.  
  158. _.form
  159. .bind('submit',function(){
  160. if(_.validate)
  161. _.submitFu()
  162. else
  163. _.form[0].submit()
  164. return false
  165. })
  166. .bind('reset',function(){
  167. _.labels.removeClass(_.invalidCl)
  168. _.labels.each(function(){
  169. var th=$(this)
  170. _.hideErrorFu(th)
  171. _.hideEmptyFu(th)
  172. })
  173. })
  174. _.form.trigger('reset')
  175. }
  176. }
  177. _.me||_.init(_.me=th.data({forms:_}))
  178. typeof o=='object'
  179. &&$.extend(_,o)
  180. })
  181. }
  182. })(jQuery)
  183. $(window).load(function(){
  184. $('#contact-form').forms({
  185. ownerEmail:'#'
  186. })
  187. })


Formularz przesyła zmienne name,fax,phone,message,state,email do skryptu powyżej a później przeysła sformatowene dane do kolejnego skryptu PHP

  1. <?php
  2. $owner_email = $_POST["owner_email"];
  3. $headers = 'From:' . $_POST["email"];
  4. $subject = 'A message from your site visitor ' . $_POST["name"];
  5. $messageBody = "";
  6.  
  7. if($_POST['name']!='nope'){
  8. $messageBody .= '<p>Visitor: ' . $_POST["name"] . '</p>' . "\n";
  9. $messageBody .= '<br>' . "\n";
  10. }
  11. if($_POST['email']!='nope'){
  12. $messageBody .= '<p>Email Address: ' . $_POST['email'] . '</p>' . "\n";
  13. $messageBody .= '<br>' . "\n";
  14. }else{
  15. $headers = '';
  16. }
  17. if($_POST['state']!='nope'){
  18. $messageBody .= '<p>State: ' . $_POST['state'] . '</p>' . "\n";
  19. $messageBody .= '<br>' . "\n";
  20. }
  21. if($_POST['phone']!='nope'){
  22. $messageBody .= '<p>Phone Number: ' . $_POST['phone'] . '</p>' . "\n";
  23. $messageBody .= '<br>' . "\n";
  24. }
  25. if($_POST['fax']!='nope'){
  26. $messageBody .= '<p>Fax Number: ' . $_POST['fax'] . '</p>' . "\n";
  27. $messageBody .= '<br>' . "\n";
  28. }
  29. if($_POST['message']!='nope'){
  30. $messageBody .= '<p>Message: ' . $_POST['message'] . '</p>' . "\n";
  31. }
  32.  
  33. if($_POST["stripHTML"] == 'true'){
  34. $messageBody = strip_tags($messageBody);
  35. }
  36.  
  37. try{
  38. if(!mail($owner_email, $subject, $messageBody, $headers)){
  39. throw new Exception('mail failed');
  40. }else{
  41. echo 'mail sent';
  42. }
  43. }catch(Exception $e){
  44. echo $e->getMessage() ."\n";
  45. }
  46. ?>


Cały problem polega na tym, że skrypt forms,js wysyla wszystnie zmienne poza zdefiniowanym w nim adresem odbiorcy (adres@webmastera.com). Firebug pokazuje ze zmienna POST " ownerEmail" zawiera nie adres a znak '#'.
Co w tym skrypcie nawala?

glh
A ten fragment kodu co robi?
  1. $(window).load(function(){
  2. $('#contact-form').forms({
  3. ownerEmail:'#'
  4. })
  5. })
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.