2017-02-24 51 views
2

我遇到了一個問題,通過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!"; 
} 
+0

,而不是'$( '#筆記')ATTR( 'VAL')'使用'$( '#筆記')VAL()。 ' –

回答

1

代替$('#note').attr('val')使用$('#note').val()

爲什麼?下面的原因給出: -

console.log($('#note').attr('val')); 
 
console.log($('#note').val());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input id = "note" value = "2">

+0

感謝回覆anant,我已經改變它使用.val(),但是仍然沒有消息被設置...可能是一個腳本問題? – oldbie

+0

我很抱歉,這是我的錯誤錯誤鍵入我的數據庫中的表列名稱!對不起,有困擾你(但感謝Val()提示! – oldbie

+0

做了很多謝謝;) – oldbie