Luke Litherland Luke Litherland - 1 year ago 62
jQuery Question

jQuery AJAX POST not passing data to a PHP script

So i have this jQuery:

$("#dropbin").droppable(
{
accept: '#dragme',
hoverClass: "drag-enter",
drop: function(event)
{
var noteid = "<?=isset($_POST['noteid']) ? $_POST['noteid'] : "" ?>";

if (confirm('Delete the note?')==true)
{
$('#dragme').hide();
debugger
$.ajax({
type: 'POST',
data: noteid,
datatype: 'json',
url: 'deleteNote.php',
success: function(result)
{
alert("Success");
}
});

window.location = "http://discovertheplanet.net/general_notes.php";
}
else
{
window.location = "http://discovertheplanet.net/general_notes.php";
}
}
});


and that includes this url:
url: 'deleteNote.php',


in deleteNote.php:

<?php

include "connectionDetails.php";

?>

<?php

if (isset($_POST['noteid']))
{
// $noteid2 = $_POST['noteid1'];

echo "You finally hit this bit, congratulations...";

// $stmt = "UPDATE Notes SET Deleted = GETDATE() WHERE NoteID = (?)";
// $params = $noteid2;

// $stmt = sqlsrv_query($conn, $stmt, $params);

// if ($stmt === false)
// {
// die( print_r(sqlsrv_errors(), true));
// }

}

else
{
echo "No Data";
}


?>


Now even in the URL if i run /deleteNote.php?noteid=25 it hits the "No Data" part of my PHP.

When i run in debugger it populates the variable noteid with a NoteID so that bit is working but the PHP file is saying its not set?

Answer Source

Let's look in your Ajax call:

$.ajax({
      type: 'POST',
      data: noteid,
      datatype: 'json',
      url: 'deleteNote.php',
      success: function(result)
          {
              alert("Success");
          }
    });

Looks nice, but you are sending post data with no id, you're just sending a value. Try this instead.

  $.ajax({
      type: 'POST',
      data: {
          noteid: noteid
      },
      datatype: 'json',
      url: 'deleteNote.php',
      success: function(result)
          {
              alert("Success");
          }
    });
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download