2

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!";
}
1
  • instead of $('#note').attr('val') use $('#note').val() Commented Feb 24, 2017 at 22:32

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the reply anant, i have changed it to use .val(), however there is still no message being set... possibly a script problem?
My apologies anant, it was my mistake mistyped the table column name in my database! Sorry to have troubled you (but thanks for the Val() tip!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.