2013-10-18 37 views
0

我要開始說我並沒有真正的網頁開發經驗,所以如果這個問題聽起來很愚蠢或解決方案很明顯,但我似乎無法找到它在這個網站或谷歌。基本上我有一個HTML表單,並希望將數據發佈到一個PHP腳本,將數據發送到特定的電子郵件地址。這是HTML代碼:Ajax文章將參數添加到主頁鏈接

<form id="contact-form" name="contactform" action=""> 
    <fieldset> 
    <p class="contact-name"> 
     <input id="name" type="text" placeholder="Full Name" value="" name="name"/> 
     <label class="error" for="name" id="name_error">This field is required.</label> 
    </p> 
    <p class="contact-email"> 
     <input id="email" type="text" placeholder="Email Address" value="" name="email"/> 
     <label class="error" for="email" id="email_error">This field is required.</label> 
    </p>  
    <p class="contact-message"> 
     <textarea id="message" placeholder="Your Message" name="message" rows="15" cols="40"></textarea> 
     <label class="error" for="message" id="message_error">This field is required.</label> 
    </p> 
    <p class="contact-submit"> 
     <input type="submit" value="Submit" id="submit_button" class="button"> 
    </p> 
    </fieldset>   
</form> 

這是jQuery的& Ajax代碼來驗證數據:

$(document).ready(function() { 
    $('.error').hide(); 
    $(".button").click(function() { 
     //validate process 
     var name = $("input#name").val(); 
     if (name == "") { 
      $("label#name_error").show(); 
      $("input#name").focus(); 
      return false; 
     } 
     var email = $("input#email").val(); 
     if (email == "") { 
      $("label#email_error").show(); 
      $("input#email").focus(); 
      return false; 
     } 
     var message = $("textarea#message").val(); 
     if (message == "") { 
      $("label#message_error").show(); 
      $("textarea#message").focus(); 
      return false; 
     } 
    }); 

    var dataString = 'name='+ name + '&email=' + email + '&message=' + message; 
    $.ajax({ 
     type: "POST", 
     url: "_include/php/contact.php", 
     data: dataString, 
    }); 
    return false; 

}); 

出現在這裏是不是JS代碼不運行的事實的問題,但後添加參數到當前的鏈接,如:?訪問http // website.com/index.html的名字=測試&電子郵件=電子郵件&消息=味精

但我需要它的參數添加到我的PHP文件,該文件位於_incl ude/php/contact.php

更新:所以我已經嘗試了各種答案,並感謝偉大的答覆,但我仍然卡住了。我可以選擇將表單的動作設置爲PHP文件,但這意味着我的頁面被刷新,這是我想要避免的。其他js腳本似乎沒有改變參數被添加到錯誤鏈接的事實..

+0

$( 「#聯繫形式」)。在( 「提交」,函數(事件){ /***檢查錯誤****/ /**如果正常***/ event.preventDefault(); $就({ 類型: 'POST', 網址: 'URL', 數據:{ $( '#接觸形式')序列(), } } }); – shafiq

回答

0

表單的「action」屬性需要設置到您想要提交表單的php頁面想要:

<form id="contact-form" name="contactform" action="/php/contact.php"> 
+0

除非他的意圖是使用AJAX而不是默認的表單提交,這是他的代碼看起來像。 – Barmar

+0

這個工作,但我的意圖是使用Ajax,因此頁面不必刷新。 –

+0

如果您希望使用AJAX並重寫默認表單提交行爲,請考慮使用['.ajaxForm'](http://malsup.com/jquery/form/) –

3

您的ajax請求不在$('.button').click()事件中。即使沒有點擊按鈕,也會觸發它。因此,現在該按鈕正在觸發具有action=""的本地表單提交,並因此將該表單數據附加爲GET請求。

更改javascript代碼,以便在$('form').submit()上應用formlogic而不是按鈕單擊事件;這將增加代碼中的邏輯,並通過返回false來禁用表單的完整本機功能;

+0

雖然這是正確的,但它不能解決我的問題。 –

-2

也許你需要event.preventDefault()

http://api.jquery.com/event.preventDefault/

+0

他返回'false',這相當於。 – Barmar

+0

在atomman的最後一個代碼示例中,您仍然沒有使用return false。 –

+0

@DaanOlislagers你不需要使用'return false','preventDefault'和'stopPropagation'產生一些功能。 [所以解釋問題](http://stackoverflow.com/questions/1357118/event-preventdefault-vs-return-false) – atomman

-1

我沒有測試它,但低於snipplet顯示的基本設置我正常使用。

$('#contact-form').submit(function(event){ 
    var form = $(this); 
    if (isContactFormValid()) { 
     postData(form.serialize()); 
    } 
    event.preventDefault(); 



    function isContactFormValid() { 
     //Some logic here 
     return true; 
    } 
    function postData(data) { 
     $.ajax({ 
      type: "POST", 
      url: "_include/php/contact.php", 
      data: data 
     }); 
    } 
}); 
+0

這仍然將參數添加到「主頁鏈接」,並且不執行聯繫人。 php –

+0

剛剛在控制檯嘗試過,而在SO。在這裏它試圖發佈到'http:// stackoverflow.com/_include/php/contact.php'。所以我需要更多的信息。你使用了哪些其他庫,jQuery版本,你使用'ajaxPrefilter'? – atomman

+0

在提交函數中使用return false而不是內部函數... –