有人可以幫我看看這個代碼明顯的東西嗎?問題似乎是嵌入式JavaScript代碼沒有被執行,因此PHP代碼也被跳過了。 HTML和PHP文件都在根目錄中。AJAX代碼沒有執行
的HTML/JS:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery AJAX test form</title>
<script src="js/jquery.min.js"></script>
<link href="css/bootstrap.min.css" rel="stylesheet">
<script src="js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<!-- Contacts -->
<div id="contacts">
<div class="row">
<!-- Alignment -->
<div class="col-sm-offset-3 col-sm-6">
<p> </p>
<p>Some header message here</p>
<p> </p>
<form name="contact" class="well" id="contact">
<legend>Contact Form</legend>
<div class="control-group">
<div class="controls">
<input type="text" class="form-control" placeholder="Name" id="name" />
<p class="help-block"></p>
</div>
</div>
<div class="control-group">
<div class="controls">
<input type="email" class="form-control" placeholder="Email" id="email" required/>
</div>
</div>
<div class="control-group">
<div class="controls">
<textarea rows="10" cols="100" class="form-control" placeholder="Message" id="message" style="resize:none"></textarea>
</div>
</div>
<div id="success"> </div>
<button type="submit" class="btn btn-primary pull-right" id="submit">Send</button>
<button type="reset" class="btn btn-default pull-right" id="res">Reset</button>
<br />
</form>
</div>
</div>
</div>
</div>
<script>
$(function() {
$("button#submit").click(function(event){
event.preventDefault();
$.ajax({
type: "POST",
url: "process.php",
data: $('form.contact').serialize(),
success: function(){
alert("success");
},
error: function(){
alert("failure");
}
});
});
});
</script>
</body>
</html>
PHP:
<?php
if (isset($_POST['name'])) {
$name = strip_tags($_POST['name']);
$email = strip_tags($_POST['Email']);
$message= strip_tags($_POST['message']);
echo "Name =".$name."</br>";
echo "Email =".$email."</br>";
echo "Message =".$message."</br>";
echo "<span class=\"label label-info\" >your message has been submitted .. Thank you</span>";
}
?>
嘗試'submit'事件,而不是'click'事件 – 2014-08-28 15:41:17
不能確定,但似乎你只是定義一個函數在那裏,這不叫,所以代碼沒有加載。你嘗試用'$(document).ready(function(){'? – cgf 2014-08-28 15:41:24
@cgf,'$(function()')完全等價於$(document).ready函數()'。見:http://api.jquery.com/ready/ – Sparky 2014-08-28 16:00:03