Warning: Cannot use a scalar value as an array in /home/admin/public_html/forum/include/fm.class.php on line 757

Warning: Invalid argument supplied for foreach() in /home/admin/public_html/forum/include/fm.class.php on line 770
Форумы портала PHP.SU :: Версия для печати :: Вызов формы при повторном вызове (ajax)
Форумы портала PHP.SU » » HTTP и PHP » Вызов формы при повторном вызове (ajax)

Страниц (1): [1]
 

1. soltx - 06 Сентября, 2014 - 19:30:45 - перейти к сообщению
Привет, есть сайт promojet.su есть всплывающая форма, работает на ajax,
после того как удачано отправили форму, выскакивает сообщение "спасибо" и если закрыть сообщение и опять вызвать форму не обновляя страницу, то формы не будет, а только сообщение "спасибо" .

Так вот вопрос, как вызвать по новой форму, не обновляя страницу.

код js:

CODE (javascript):
скопировать код в буфер обмена
  1.  
  2. $(document).ready(function() {
  3.        
  4.         //if submit button is clicked
  5.         $('#form').submit(function () {        
  6.                
  7.                 //Get the data from all the fields
  8.                 var name = $('input[name=name]');
  9.                 var email = $('input[name=email]');
  10.                 var tel = $('input[name=tel]');
  11.                
  12.        
  13.  
  14.                 //Simple validation to make sure user entered something
  15.                 //If error found, add hightlight class to the text field
  16.                 if (name.val()=='') {
  17.                         name.addClass('hightlight');
  18.                         return false;
  19.                 } else name.removeClass('hightlight');
  20.                 if (email.val()=='') {
  21.                         email.addClass('hightlight');
  22.                         return false;
  23.                 } else email.removeClass('hightlight');
  24.                
  25.                 if (tel.val()=='') {
  26.                         tel.addClass('hightlight');
  27.                         return false;
  28.                 } else tel.removeClass('hightlight');
  29.                
  30.                
  31.                
  32.                
  33.                
  34.                
  35.                
  36.                 //organize the data properly
  37.                 var data = 'name=' + name.val() +  '&email=' + email.val() + '&tel=' +
  38.                 tel.val();
  39.                
  40.                 //disabled all the text fields
  41.                
  42.                
  43.                 //show the loading sign
  44.                 $('.loading').show();
  45.                
  46.                 //start the ajax
  47.                 $.ajax({
  48.                         //this is the php file that processes the data and send mail
  49.                         url: "process.php",    
  50.                        
  51.                         //GET method is used
  52.                         type: "GET",
  53.  
  54.                         //pass the data                
  55.                         data: data,            
  56.                        
  57.                         //Do not cache the page
  58.                         cache: false,
  59.                        
  60.                         //success
  61.                         success: function (html) {                             
  62.                                 //if process.php returned 1/true (send mail success)
  63.                                 if (html==1) {                                 
  64.                                         //hide the form
  65.                                                                                
  66.                                        
  67.                                         //show the success message
  68.                                        
  69.                                        
  70.                                        
  71.                      
  72.                                                 $('.blk-data .form1').fadeOut('slow');
  73.                                                 $('.loading').fadeOut('slow');
  74.  
  75.                                         $('.done').fadeIn('slow');
  76.                                        
  77.                                 //if process.php returned 0/false (send mail failed)
  78.                                 } else alert('Sorry, unexpected error. Please try again later.');                              
  79.                         }              
  80.                 });
  81.                
  82.                 //cancel the submit button default behaviours
  83.                 return false;
  84.         });    
  85. });    



код php

PHP:
скопировать код в буфер обмена
  1. ]//Retrieve form data.
  2. //GET - user submitted data using AJAX
  3. //POST - in case user does not support javascript, we'll use POST instead
  4. $name = ($_GET['name']) ? $_GET['name'] : $_POST['name'];
  5. $email = ($_GET['email']) ?$_GET['email'] : $_POST['email'];
  6. $tel = ($_GET['tel']) ?$_GET['tel'] : $_POST['tel'];
  7.  
  8.  
  9. //flag to indicate which method it uses. If POST set it to 1
  10. if ($_POST) $post=1;
  11.  
  12. //Simple server side validation for POST data, of course, you should validate the email
  13. if (!$name) $errors[count($errors)] = 'Please enter your name.';
  14. if (!$email) $errors[count($errors)] = 'Please enter your email.';
  15. if (!$tel) $errors[count($errors)] = 'Please enter your tel.';
  16.  
  17.  
  18. //if the errors array is empty, send the mail
  19. if (!$errors) {
  20.  
  21.         //recipient
  22.         $to = 'Your Name <hello@domain.ru>';   
  23.         //sender
  24.         $from = $name . ' <' . $email . '>';
  25.        
  26.         //subject and the html message
  27.         $subject = 'Сообщение с сайта PromoJet  ';     
  28.         $message = '
  29.         <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  30.         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  31.         <html xmlns="http://www.w3.org/1999/xhtml">
  32.         <head></head>
  33.         <body>
  34.         <table>
  35.                 <tr><td><strong>Имя:</strong></td><td>' . $name . '</td></tr>
  36.                 <tr><td><strong>Email:</strong></td><td>' . $email . '</td></tr>
  37.                 <tr><td><strong>Телефон:</strong></td><td>' . $tel . '</td></tr>
  38.                
  39.         </table>
  40.         </body>
  41.         </html>';
  42.  
  43.         //send the mail
  44.         $result = sendmail($to, $subject, $message, $from);
  45.        
  46.         //if POST was used, display the message straight away
  47.         if ($_POST) {
  48.                 if ($result) echo 'Thank you! We have received your message.';
  49.                 else echo 'Sorry, unexpected error. Please try again later';
  50.                
  51.         //else if GET was used, return the boolean value so that
  52.         //ajax script can react accordingly
  53.         //1 means success, 0 means failed
  54.         } else {
  55.                 echo $result;  
  56.         }
  57.  
  58. //if the errors array has values
  59. } else {
  60.         //display the errors message
  61.         for ($i=0; $i<count($errors); $i++) echo $errors[$i] . '<br/>';
  62.         echo '<a href="/">Back</a>';
  63.         exit;
  64. }
  65.  
  66.  
  67. //Simple mail function with HTML header
  68. function sendmail($to, $subject, $message, $from) {
  69.         $headers = "MIME-Version: 1.0" . "\r\n";
  70.         $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
  71.         $headers .= 'From: ' . $from . "\r\n";
  72.        
  73.         $result = mail($to,$subject,$message,$headers);
  74.        
  75.         if ($result) return 1;
  76.         else return 0;
  77. }
2. Viper - 06 Сентября, 2014 - 20:03:11 - перейти к сообщению
soltx кнопка закрыть каким раком работает? Угадывать?
Сама форма где и откуда берется?

 

Powered by ExBB FM 1.0 RC1