How to call php function from anchor tag?

喜欢而已 提交于 2019-12-13 00:18:34

问题


I am using a sweetalert popup. I am calling the function from anchor tag but it not taking any action. Would you help me out in this?

  <link rel="stylesheet" href="assets/css/sweetalert2.min.css">
  <script src="assets/scripts/sweetalert2.min.js"></script>
  <script src="assets/vendor/jquery/jquery.min.js"></script>
  echo ' <td class="btn-action"><a href="javascript:void(0);" onClick="record_delete('.$p_user_id.')" class="btn-delete"><i class="fa fa-times" aria-hidden="true"></i></a></td>';



  <script>
  function record_delete(id){
    swal({
     title: 'Are you sure?',
     text: "You want to move this record",
     type: 'warning',
     showCancelButton: true,
     confirmButtonColor: '#3085d6',
     cancelButtonColor: '#d33',
     confirmButtonText: 'Yes, move it!'
    },function () {
        window.location='process.php?function=p_delete&p_Id='+id+'';
    }); 
  }
  </script>

回答1:


You need to quote p_user_id:

echo '<td class="btn-action"><a href="javascript:void(0);" 
          onClick="record_delete(\''.$p_user_id.'\')" class="btn-delete">
      <i class="fa fa-times" aria-hidden="true"></i></a></td>';



回答2:


Identifier starts immediately after numeric literal.

This happens only when string in broken somewhere.

In this case it can only happen on 2 cases:

1) $p_user_id passed in the js function contains a single quote or double quote. For this Can you please share the id passed?

2) Your php statements are not wrapped inside php tags. For this please try:

<?php echo ' <td class="btn-action"><a href="javascript:void(0);" onClick="record_delete('.$p_user_id.')" class="btn-delete">asdfa</a></td>';?>



回答3:


change your anchor to be like this

<a href="javascript:record_delete("'.$p_user_id.'");return false'" class="btn-delete">


来源:https://stackoverflow.com/questions/43719877/how-to-call-php-function-from-anchor-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!