2017-02-24 53 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