javascript - 'Unexpected token ;' when trying to pass PHP variable in jQuery AJAX request - Stack Overflow

So im trying to run a PHP script that sets a deleted field in the database to poplulate if you drag a c

So im trying to run a PHP script that sets a deleted field in the database to poplulate if you drag a certain text element to the droppable area.

At the moment i have this droppable area:

<div class="dropbin" id="dropbin" >
    <span class="fa fa-trash-o noSelect hover-cursor" style="font-size: 20pt; line-height: 225px;">&nbsp;</span>
</div>

and this draggable text:

<div id='dragme' data-toggle='modal' data-target='#editNoteNameModal' class='display-inline'>" . $data['NoteName'] . "</div>

The area im having an issue with is this:

$("#dropbin").droppable
  ({
    accept: '#dragme', 
    hoverClass: "drag-enter",
    drop: function(event) 
    {
      var noteid = <?php if(isset($_POST['noteid'])){ echo $_POST['noteid'];} ?>;
      var deletedby = <? if(isset($_SESSION['username'])){ echo $_SESSION['username'];} ?>
      var data = {noteid1: noteid, deletedby1: deletedby};

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

        window.location = ".php";
      }
      else
      {
        window.location = ".php";
      }
    }
  });

EDIT: The line i get the error on is:

var noteid = <?php if(isset($_POST['noteid'])){ echo $_POST['noteid'];} ?>;

Im currently getting an "Unexpected token ;" and its stopping the droppable from working.

Just a side note, if i run it without the variables it hits everything apart from:

url: 'deleteNote.php',

Also inside deleteNote.php is this incase it helps:

<?php

include "connectionDetails.php";

?>

<?php

if (isset($_POST['noteid1'], $_POST['deletedby1'])) 
{
    $noteid2 = $_POST['noteid1'];
    $deletedby2 = $_POST['deletedby1'];

    // echo "Hello...". $noteid;

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

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

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

else
{
    echo "No Data";
}


?>

(I deliberatley don't have deletedby in the database just yet, ignore that)

Could anyone possibly help me to get this to work?

So im trying to run a PHP script that sets a deleted field in the database to poplulate if you drag a certain text element to the droppable area.

At the moment i have this droppable area:

<div class="dropbin" id="dropbin" >
    <span class="fa fa-trash-o noSelect hover-cursor" style="font-size: 20pt; line-height: 225px;">&nbsp;</span>
</div>

and this draggable text:

<div id='dragme' data-toggle='modal' data-target='#editNoteNameModal' class='display-inline'>" . $data['NoteName'] . "</div>

The area im having an issue with is this:

$("#dropbin").droppable
  ({
    accept: '#dragme', 
    hoverClass: "drag-enter",
    drop: function(event) 
    {
      var noteid = <?php if(isset($_POST['noteid'])){ echo $_POST['noteid'];} ?>;
      var deletedby = <? if(isset($_SESSION['username'])){ echo $_SESSION['username'];} ?>
      var data = {noteid1: noteid, deletedby1: deletedby};

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

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

EDIT: The line i get the error on is:

var noteid = <?php if(isset($_POST['noteid'])){ echo $_POST['noteid'];} ?>;

Im currently getting an "Unexpected token ;" and its stopping the droppable from working.

Just a side note, if i run it without the variables it hits everything apart from:

url: 'deleteNote.php',

Also inside deleteNote.php is this incase it helps:

<?php

include "connectionDetails.php";

?>

<?php

if (isset($_POST['noteid1'], $_POST['deletedby1'])) 
{
    $noteid2 = $_POST['noteid1'];
    $deletedby2 = $_POST['deletedby1'];

    // echo "Hello...". $noteid;

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

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

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

else
{
    echo "No Data";
}


?>

(I deliberatley don't have deletedby in the database just yet, ignore that)

Could anyone possibly help me to get this to work?

Share Improve this question edited Jul 27, 2017 at 10:38 Luke Litherland asked Jul 27, 2017 at 10:34 Luke LitherlandLuke Litherland 2255 silver badges16 bronze badges 12
  • what is that debugger? is that mented or is a mistake? – Exprator Commented Jul 27, 2017 at 10:39
  • 1 var noteid = <?php if(isset($_POST['noteid'])){ echo $_POST['noteid'];} ?>; apart from potential quoting issues, take a moment to think about what the result of that line will look like, if that POST parameter was not set ... – C3roe Commented Jul 27, 2017 at 10:40
  • that is essentially a break point in the developer window in the console so i can see whether it is hitting that part of the code or not – Luke Litherland Commented Jul 27, 2017 at 10:40
  • So the long JS script is inline code in a document that is always loaded via a POST request? Because if not, it's always going to end up as var noteid = ; – user5734311 Commented Jul 27, 2017 at 10:42
  • 1 If the ajax success fired, it did successfully request the resource though. I always test my API and AJAX calls independently; if you change the type to GET you can simply put deleteNote.php?nodeid1=123&deletedby1=abc in the browser to test it. – user5734311 Commented Jul 27, 2017 at 10:50
 |  Show 7 more ments

1 Answer 1

Reset to default 5

Try to add quotes in these lines and add php after <? in second line:

var noteid = "<?php if(isset($_POST['noteid'])){ echo $_POST['noteid'];} ?>";
var deletedby = "<?php if(isset($_SESSION['username'])){ echo $_SESSION['username'];} ?>";

OR

var noteid = "<?=isset($_POST['noteid']) ? $_POST['noteid'] : "" ?>";
var deletedby = "<?=isset($_SESSION['username']) ? $_SESSION['username'] : "" ?>";

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745579061a4634144.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信