我想你將不得不將的擴展名改爲.php
,如果你打算在每個頁面的邊欄中加入contact form
。
更改爲.php
擴展不會使你的HTML代碼中運作的任何區別,它只是使您能夠POST
表單數據進行處理......所以,儘管..
要使用AJAX做。 。 index.html
<form id="upload" action="" method="post">
Name:<input type="text" id="name" name="name"><br><br>
Email:<input type="email" id="email" name="email"><br><br>
<input type="submit" value="Upload" class="submit" />
</div>
</form>
<div id="message"></div>
爲了您ajax.js文件:
$(document).ready(function (e) {
$("#upload").on('submit',(function(e) {
e.preventDefault();
$.ajax({
url: "upload.php", // Url to which the request is send
type: "POST", // Type of request to be send, called as method
data: new FormData(this), // Data sent to server, a set of key/value pairs (i.e. form fields and values)
contentType: false, // The content type used when sending data to the server.
cache: false, // To unable request pages to be cached
processData:false, // To send DOMDocument or non processed data file it is set to false
success: function(data) // A function to be called if request succeeds
{
$('#message').html = "success";
}
});
}));
});
一第二你upload.php
文件:你在PHP回聲
<?php
$name = $_POST['name'];
$email = $_POST['email'];
//etc depending upon your form inputs. You can store this in your database.
?>
的東西都可以在success:function(data)
在js
文件中獲得。
對於HTML文件,您只需將ajax.js
加入script tag
即可。
是打算做服務器的任何頁面處理需要是.php而不是.html。換句話說,如果它只是一個靜態html頁面,您可以將其保留爲.html。 – developerwjk 2015-02-10 20:43:10
如果在任何頁面上沒有處理PHP,則不需要將所有'.html'全部更改爲'.php'。只有用來處理'mail()'的東西。但是,如果你想,你可以;只要代碼是好的,它就不會有太大的區別。 – 2015-02-10 20:43:59
只需在每個頁面上添加html表單的內容 - 並使用ajax/javascript/jquery – 2015-02-10 20:49:16