2016-09-29 89 views
2

我已經在stackoverflow中搜索的問題,並發現很少的答案,但非他們幫助我。所以我再次發佈它。我的PHP的形式在本地主機POST方法工作正常,但相同的代碼是不是在服務器(Linux服務器)工作,我不知道實際的問題是什麼。請幫助PHP POST metod不與jQuery的Ajax或無Ajax工作

  1. 形式,我想提交
<form class="forma" id="forma" method="POST" action="contact-form.php" name="myForm" autocomplete="off"> 
    <div class="formWrapper"> 

    <div class="group"> 
     <input type="text" name="name" id="name"> 
     <span class="highlight"></span> 
     <span class="bar"></span> 
     <label>Name</label> 
     <p class="error-red"></p> 

    </div> 

    <div class="group"> 
     <input type="text" name="eMail" id="eMail"> 
     <span class="highlight"></span> 
     <span class="bar"></span> 
     <label>Email</label> 
     <p class="error-red"></p> 
    </div> 

    <div class="group"> 
     <input type="text" name="mobileNo" id="mobileNo"> 
     <span class="highlight"></span> 
     <span class="bar"></span> 
     <label>Mobile no.</label> 
     <p class="error-red"></p> 
    </div> 

    <div class="group"> 
     <input type="text" name="message" id="message"> 
     <span class="highlight"></span> 
     <span class="bar"></span> 
     <label>Message</label> 

    </div> 

    <div class="captchaWrapper"> 
     <img src="captcha.php?rand=<?php echo rand(); ?>" id="captchaimgCont" style="margin-right: 14px;" alt="captcha"> 


     <a href="javascript: refreshCaptcha();" class="clickTorefreshAnchor"> 
     <i class="fa fa-refresh clickTorefresh" id="caprefresh"> 

             </i> 
     </a> 

     <div class="captchaInputWrapper group"> 
     <input type="text" id="6_letters_code" name="captcha"> 
     <span class="highlight"></span> 
     <span class="bar"></span> 
     <label>Enter the code here:</label> 
     <p class="error-red"></p> 

     </div> 
    </div> 


    <div class="submit submitBtn" id="consubmit"> 
     <button type="submit" name="sendmail" id="sendmail" class="sendMailBtn">Submit 
     </button> 
    </div> 

    </div> 

    <div class="clear"></div> 

</form> 
  • 我的jquery Ajax代碼
  • jQuery(document).ready(function() { 
        jQuery("#forma").submit(function(e) { 
    
        e.preventDefault(); 
        e.stopImmediatePropagation(); 
    
        jQuery('#sendmail').html('Please wait...'); 
    
        jQuery.ajax({ 
         type: "post", 
         url: './contact-form.php', 
         data: jQuery("#forma").serialize(), 
         dataType: "json", 
         success: function(data) { 
    
         jQuery('p.error-red').html(''); 
    
    
         if ('success' == data.result) { 
    
    
          //jQuery("#forma")[0].reset(); 
    
          //jQuery('#response').append('<p class="successMessage">' + data.msg + '</p>'); 
          // jQuery('#sendmail').html('Submit'); 
          window.location.href = "https://www.mrpmediaworks.com/thanks.php"; 
    
         } 
    
         if ('error' == data.result) { 
    
          var arr = data.msg; 
          var element_arr = data.fieldname; 
          count = '0'; 
          jQuery.each(arr, function(index, value) { 
          if (value.length != 0) { 
    
           jQuery('input[name=' + element_arr[count] + ']').parent('.group').find('p.error-red').html(value); 
          } 
          count++; 
          }); 
          jQuery('#sendmail').html('Submit'); 
    
         } 
    
         } 
        }); 
    
        return false; 
        }); 
    }); 
    
  • 我的PHP代碼

  • <?php 
         session_start(); 
         require_once('config.php'); 
    
         $email = ''; 
         $fieldname = array(); 
         $msg = array(); 
         $result = ''; 
         $count = ''; 
         $name = $_Post["name"]; 
         $contact = $_Post["mobileNo"]; 
         $email = $_Post["eMail"]; 
         $msg_query = $_Post["message"]; 
         $captcha = $_Post["captcha"]; 
    
         echo $name; 
         die(); 
    ?> 
    

    只是測試代碼

    相同的代碼工作與獲得

    我已嘗試刪除行動形式和可能的組合,但注意工作。 編輯後

     4. .htaccess file 
           # browser requests PHP 
          RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php 
          RewriteRule ^/?(.*)\.php$ /$1 [L,R=301] 
    
          # check to see if the request is for a PHP file: 
          RewriteCond %{REQUEST_FILENAME}\.php -f 
          RewriteRule ^/?(.*)$ /$1.php [L] 
    

    這是我的htaccess文件的唯一內容

    +0

    你的表單在哪裏,我在你的代碼中看不到。 –

    +0

    重複的http://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php –

    +0

    @AnkitShah不是真的重複。他需要返回具有data.result屬性的json – mplungjan

    回答

    1

    我認爲你已經搞亂了你的htaccess文件。如果可能的話在這裏粘貼你的.htaccess文件代碼。

    編輯我的回答

    賓果發現你已經發布的數據聯繫-form.php的。嘗試從您的Ajax請求刪除擴展名爲.php你從你的文件,但在Ajax請求刪除PHP擴展的問題。試試看,這將有助於。

    +0

    我已經添加了我的htaccess代碼請看看,請告訴我它最新錯誤。希望你能幫助 – Logan9798

    +0

    感謝@pawan它的工作..你救了我的命。 – Logan9798

    1

    只是要一個函數來處理Ajax請求。然後處理功能內的請求。要這樣做,

    從窗體中刪除操作。並將請求發送到上的功能提交請參閱下面的例如。

    <form class="forma" id="forma" method="POST" action="javascript:void(0);" name="myForm" autocomplete="off" onsubmit="sendContactRequest();"> 
    

    然後〜sendContactRequest()〜將處理你的ajax請求。

    function sendContactRequest(){ 
         jQuery('#sendmail').html('Please wait...'); 
         $.post("contact-form.php",jQuery("#forma").serialize(),function(result,status,xhr) { 
          if(status.toLowerCase()=="error".toLowerCase()) { 
           alert("An Error Occurred.."); // or do something that you can handle the error request. 
          }else { //success response 
          } 
         }); 
        } 
    

    請試試這個。在我的情況下,它是完美的工作。