我在尝试将 textarea 的值发送到 mysql 时遇到问题。
输入postusername
的值发送成功,但textarea的值没有发送。
当它工作时(我不知道为什么它停止了),第一次发送了值,但是如果我尝试在不刷新页面的情况下发送另一个值,则再次发送之前的值而不是新值插入文本区域。
现在,textarea 在警报窗口中返回一个空值,并且只在 mysql 中记录输入 postusername
。
<form id="newpostform" method="post" style="width:90%;margin-left:auto;margin-right:auto;margin-top:-35px;margin-bottom:10px;font-family:Calibri,Arial;">
<textarea class="editbook" name="newpostcontent"></textarea>
<div style="margin-top:10px;margin-right:25px;">
<input type="text" value="<?php echo $username0; ?>" name="postusername">
<input class="Button" style="float:right;margin-bottom:10px;margin-left:10px;" type="button" value="Preview">
<input class="Button" style="float:right;margin-bottom:10px;margin-left:10px;" type="button" value="Save draft">
<input id="sendButton" class="Button" style="float:right;margin-bottom:10px;" type="button" value="Send">
</div>
</form>
<script>
$(function () {
$("input#sendButton").on("click", function () {
$.post('newpost.php', $("form#newpostform").serialize(), function (data) {
alert($("textarea[name=newpostcontent]").val());
});
});
});
</script>
newpost.php
require("../db_info.php");
mysql_query("
INSERT INTO
post (
author,
content
)
VALUES (
'".$_POST['postusername']."',
'".$_POST['newpostcontent']."'
)"
);
如果我这样做,例如:
mysql_query("
INSERT INTO
post (
author,
content
)
VALUES (
'".$_POST['postusername']."',
'Why doesn't this work?'
)"
);
$_POST['postusername']
和短语“Why doesn't this work?”正确写在sql中。
感谢您的关注。
最佳答案
这样试试
var content=$("textarea[name=newpostcontent]").val(),
username=$("input[name=postusername]").val();
$.post('newpost.php', {newpostcontent:content,postusername:username}, function (data) {
alert(content);
});
希望这能行得通
关于php - jquery ajax 上的 Textarea 没有发送到 mysql,只是输入字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20485687/