我在使用 ajax 将 textarea 内容发布到我的数据库时遇到问题。我尝试使用 malsap 的 jquery.form.js 插件以及良好的 ole fashon ajax,但都无法将值发布到我的 textareas 中。这是我的代码:
HTML:
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript" src="javascript/jquery.form.js"></script>
<form id="defaultemail-ebook-form">
<label for="defaultemail-ebook">Email with ebook:</label><br>
<textarea id="defaultemail-ebook" name="defaultemail-ebook"></textarea>
<br>
<div id="defaultemail-ebook-submit-div">
<input type="button" id="defaultemail-ebook-submit" value="Set Email">
</div>
</form>
默认邮件.php
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$defaultemailebook = $_REQUEST['defaultemail-ebook'];
if (isset($_GET['defaultemail-ebook'])) {
mysql_query("UPDATE default_emails SET email_ebook='". $defaultemailebook ."' WHERE id = '1'");
echo $defaultemailebook;
}
$defaultemailnoebook = $_REQUEST['defaultemail-no-ebook'];
if (isset($_GET['defaultemail-no-ebook'])) {
mysql_query("UPDATE default_emails SET email_no_ebook='". $defaultemailnoebook ."' WHERE id = '1'");
echo $defaultemailnoebook;
}
?>
阿贾克斯:
$(function() {
$("#defaultemail-ebook-submit").click(function(){
var data = $('#defaultemail-ebook-form').serialize();
$.ajax({
url: "defaultemails.php",
type: "POST",
data: data,
success: function() {
alert("Yay");
}
});
});
});
使用 .ajaxSubmit
$(function() {
$( "#defaultemail-no-ebook-submit" ).click(function() {
$("#defaultemail-no-ebook-form").ajaxSubmit({url: 'defaultemails.php', type: 'post'}).delay(100, function() {
$("#current-no-ebook-message").text($("#defaultemail-no-ebook").val());
alert("Email without ebook had been set!");
});
});
});
defaultemails.php 将在使用尾随名称访问时更新现有字段文本(defaultemails.php?defaultemail-ebook=Here+is+your+ebook),所以我不认为它是 php 文件。似乎由于某种原因,我使用 ajax 调用时未提交 defaultemail-ebook 的值。任何帮助将不胜感激!