我遇到了一個問題,通過jQuery/ajax
發佈一個值到我的PHP腳本。我已經四處搜尋尋找解決方案,但似乎無法弄清楚爲什麼我沒有得到發佈的價值。jQuery ajax沒有發佈值到php腳本
這是我的代碼。
page.html中
<body>
input message:<p><input type="text" id="note" name="note" placeholder="enter something that made you feel this way"></p><br />
<p><button name="submitMessage">submit</button></p>
<script src="../js/jquery-3.1.1.js"></script>
<script src="../js/welcomeScript.js"></script>
<script> $(document).ready(function() {
$('[name=submitMessage]').on('click', function (e){
e.preventDefault();
$.ajax({
type: 'POST',
url: '../php/post-note.php',
data: {data: $('#note').attr('val')},
success: function(){
alert('added your note, you will now go to main app!');
window.location.href = "../home.php";
}
});
});
});
</script>
</body>
note.php後
session_start();
$note = $_POST['data'];
if(isset($note) && isset($_SESSION['username'])){
$username = $_SESSION['username'];
$sqlMessage = "UPDATE mt_tbl SET note = '$note' WHERE userName = '$username'";
mysqli_query($conn, $sqlMessage);
echo "note: ".$note. " added to the dB!";
}
,而不是'$( '#筆記')ATTR( 'VAL')'使用'$( '#筆記')VAL()。 ' –