【发布时间】:2009-10-22 03:14:34
【问题描述】:
我被难住了。我正在使用 jquery 和 ajax 将表单中的某些字段发布到数据库。
这是一个“编辑表单” - 所以所有字段都预先填充了 mysql 数据库中存在的数据。我正在传递来自 4 个字段的输入,它仅适用于其中 2 个。这是 HTML
<form id="editSubmit" method="post" name="editSubmit" action="" enctype="multipart/form-data">
<input type="hidden" id="newsID" name="newsID" value="<?=$newsID;?>">
<input type="hidden" id="authorID" name="authorID" value="<?=$news['editorID'];?>">
<label for="postTitle">Title</label><br />
<input type="text" name="postTitle" id="postTitle" class="text" size="20" value="<?=$postTitle;?>"/><br />
<label for="postText">Post Text</label><br />
<textarea name="postText" id="postText" class="textarea"><?=$postText;?></textarea> <br /><br />
<button type="submit">Submit Edit </button>
<input type="button" onClick="closeEdit()" value="Cancel">
</form>
现在这是我用来将其发布到页面的代码。
$("form#editSubmit").submit(function() {
// we want to store the values from the form input box, then send via ajax below
var newsID = $('#newsID').attr('value');
var postTitle = $('#postTitle').attr('value');
var postText = $('#postText').attr('value');
postText = postText.replace(/&/g,'%26');
var authorID = $('#authorID').attr('value');
$.ajax({
type: "POST",
url: "news/editNews.php",
data: "newsID="+ newsID + "&postTitle="+ postTitle + "&postText=" + postText + "&authorID=" + authorID,
success: function(){
$('form#editSubmit').fadeOut('slow');
$('div.success').fadeIn();
}
}); // End .ajax function
return false;
}); //End submit function()
此代码已成功通过 authorID 和 newsID 发送,但 postTitle 或 postText 没有运气。我看不出我做错了什么。也许我错过了什么?
另外,我是 ajax/jquery 的新手 - 如果代码中的某些内容看起来很奇怪,我将不胜感激。走到这一步,经过了很多试验和错误。
【问题讨论】: