有人可以解释或告诉我为什么我的Ajax表单不会发布吗?我正在使用智能模板引擎。
表格
<form action="/ajax/update_details.php" method="post" id="detailsform">
<input type="text" name="city" value="{$profile_user.city}" />
<input type="text" name="profile_message" value="{$profile_user.profile_message}" />
<textarea name="about_me" cols="53" rows="5" class="submit_form_textfield">{$profile_user.about_me}</textarea>
<textarea name="hobbies" cols="53" rows="5" class="submit_form_textfield">{$profile_user.hobbies}</textarea>
<input type="hidden" name="action" value="user_details" />
<input type="submit" class="submit_form_button" value="Update Details" />
<a class="success" style="display: none; color: green; text-align: centera;">Your details have been updated</a>
</form>
的Javascript
{literal}
<script type="text/javascript">
$("#detailsform").submit(function() {
var url = "/ajax/update_details.php";
$.ajax({
type: "POST",
url: url,
data: $("#detailsform").serialize(),
success: function(data)
{
$(".success").show(1000);
$(".success").fadeOut(5000);
}
});
return false;
});
</script>
{/literal}
并且update_details.php
<?php
session_start();
require_once('../vars.php');
require_once('../includes/user.class.php');
$user = new User();
if (isset($action) && $action=='user_details' && isset($_SESSION['loggeduser_id'])){
$city = preg_replace("/[^a-z]/i","",$_POST['city']);
$profile_message = preg_replace("/[^a-z]/i","",$_POST['profile_message']);
$about_me = ($_POST['about_me']);
$hobbies = ($_POST['hobbies']);
$user->update($_SESSION['loggeduser_id'],array("city" => $city,"profile_message" => $profile_message,"about_me" => $about_me,"hobbies" => $hobbies));
}
?>
请有人帮助我,因为我是新手编码,我们将非常感激
答案 0 :(得分:3)
您的代码不会等待加载页面,除非您的脚本在</body>
$(document).ready(function() {
之前正确,您需要使用$(function(){
或<script type="text/javascript">
$(document).ready(function() {
$("#detailsform").submit(function(){
// etc
});
});
</script>
或任何其他有效语法。 NB,它也需要关闭。
{{1}}
答案 1 :(得分:0)
添加文档就绪处理程序。
$(function(){
$("#detailsform").submit(function() {
//your stuff here....
});
});