最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Uncaught ReferenceError: id is not defined at HTMLButtonElement.onclick - Stack Overflow

programmeradmin6浏览0评论

I'm trying to code a detailed ticket information from database that could be deleted by the admin. I want to use jquery ajax to remove it once the user have click on the button

my codes are :

`

<form class="white" method="post" action="" id="frmBA2N29F">
<span class="col3">BA2N29F</span>
<span class="col3">Medan(KNO)</span>
<span class="col3">Jakarta (CGK)</span>
<span class="col3">BA</span>
<span class="col2">BA001</span>
<span class="col4">2016-01-01</span>
<span class="col3">05:00:00</span>
<span class="col3">07:00:00</span>
<span class="col1">60</span>
<span class="col3">560000.00</span>
<span class="col3">560000.00</span>
<span class="col3">280000.00</span>
<button class = "delete" type="button" id="deleteBA2N29F" onClick = "delete_row(BA2N29F);">delete</button>
</form>

`

my javascript function :

<script type="text/javascript" src="../../JS/jquery-3.1.1.min.js"></script>
    <script>
function delete_row(id)
{
 $.ajax
 ({
  type:'post',
  url:'delete-ticket.php',
  data:{
   delete_row:'delete_row',
   row_id:id,
  },
  success:function(response) {
   if(response=="success")
   {
    var row=document.getElementById("frm"+id);
    row.parentNode.removeChild(row);
   }
  }
 });
}
</script>

and my delete-ticket.php code :

<?php
include_once '../../database.php';
if(isset($_POST['delete_row']))
{
 $row_no=$_POST['row_id'];
 mysqli_query($connection,"delete from flight where flight_id='$row_no'");
 echo "success";
 exit();
}
?>

The message that I got from my browser is

tiket-list.php:98 Uncaught ReferenceError: BA2N29F is not defined
    at HTMLButtonElement.onclick

So what's the error in my code?

I'm trying to code a detailed ticket information from database that could be deleted by the admin. I want to use jquery ajax to remove it once the user have click on the button

my codes are :

`

<form class="white" method="post" action="" id="frmBA2N29F">
<span class="col3">BA2N29F</span>
<span class="col3">Medan(KNO)</span>
<span class="col3">Jakarta (CGK)</span>
<span class="col3">BA</span>
<span class="col2">BA001</span>
<span class="col4">2016-01-01</span>
<span class="col3">05:00:00</span>
<span class="col3">07:00:00</span>
<span class="col1">60</span>
<span class="col3">560000.00</span>
<span class="col3">560000.00</span>
<span class="col3">280000.00</span>
<button class = "delete" type="button" id="deleteBA2N29F" onClick = "delete_row(BA2N29F);">delete</button>
</form>

`

my javascript function :

<script type="text/javascript" src="../../JS/jquery-3.1.1.min.js"></script>
    <script>
function delete_row(id)
{
 $.ajax
 ({
  type:'post',
  url:'delete-ticket.php',
  data:{
   delete_row:'delete_row',
   row_id:id,
  },
  success:function(response) {
   if(response=="success")
   {
    var row=document.getElementById("frm"+id);
    row.parentNode.removeChild(row);
   }
  }
 });
}
</script>

and my delete-ticket.php code :

<?php
include_once '../../database.php';
if(isset($_POST['delete_row']))
{
 $row_no=$_POST['row_id'];
 mysqli_query($connection,"delete from flight where flight_id='$row_no'");
 echo "success";
 exit();
}
?>

The message that I got from my browser is

tiket-list.php:98 Uncaught ReferenceError: BA2N29F is not defined
    at HTMLButtonElement.onclick

So what's the error in my code?

Share asked Nov 28, 2016 at 17:51 VincentTheonardoVincentTheonardo 3311 gold badge6 silver badges19 bronze badges 3
  • 1 make it string ....... – Mahi Commented Nov 28, 2016 at 17:53
  • 1 It should be delete_row('BA2N29F'); in your onClick. – abhishekkannojia Commented Nov 28, 2016 at 17:54
  • thanks for the solution, never knew that this simple mistake can drive me crazy for an hour – VincentTheonardo Commented Nov 28, 2016 at 18:02
Add a ment  | 

2 Answers 2

Reset to default 1

Your argument to delete_row() function should be wrapped in quotes since it is a string literal otherwise it'll be interpreted as a variable which would throw a undefined variable error since it is not defined anywhere.

Change your line to this:

<button class = "delete" type="button" id="deleteBA2N29F" onClick = "delete_row('BA2N29F');">delete</button>  

Your button's onclick function's variables must be assigned with a quotes because it's value is String.

发布评论

评论列表(0)

  1. 暂无评论