oldbie
oldbie

Reputation: 59

jQuery ajax not posting values to php script

I am having a problem with posting a value to my php script via jQuery/ajax. I have searched around looking for a solution but cannot seem to figure it out why i am not getting the value posted.

here's my code.

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>

post-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!";
}

Upvotes: 2

Views: 231

Answers (1)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72299

Instead of $('#note').attr('val') use $('#note').val()

Why? the reason is given below:-

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">

Upvotes: 1

Related Questions