how to pass php value from one file to another through java script

拜拜、爱过 提交于 2019-12-04 19:40:58

You can pass value to server using ajax calls. See the following code. Here We use a confirm box to get user confirmation.

function deleteEmployee(empId){
   var confirm=confirm("Do you want to delete?");
   if (confirm)
   {
     var url = "path/to/delete.php";
     var data = "emp_id="+empId;
     $.ajax({
       type: "POST",
       url: "otherfile.php",
       data: data ,
       success: function(){         
          alert("Employee deleted successfully.");
       }
     });
  }
}

In delete.php you can take the employee id by using $_POST['emp_id']

You can do it easily by using jquery

var dataString = 'any_variable='+ <?=$phpvariable?>;        
$.ajax({
type: "POST",
url: "otherfile.php",
data: dataString,
success: function(msg){         
    // msg is return value of your otherfile.php
}
}); //END $.ajax

I would add an extra variable in to the delete link address. Preferrably the ID of the row that you need to be deleted.

I don't know Concrete-5 CMS. But, i am giving you the general idea
I think, you are using some button on which users can click if they want to delete role.

<td>".$ih->button_js(t('Delete'), "deleteRole('".$data['role_id']."')", 'left', 'error')."</td>

My suggestion,
add onClick to button
onClick="deleteEmployee(roleId);" // roleId - dynamic id of the role by looping over

Frankly speaking dude, i dont know how you will add this to your button that i guess there would surely be some way to simply add this to existing html.

And now, simply use Sajith's function

// Sajith's function here
function deleteEmployee(empId){

    var confirm=confirm("Do you want to delete?");
    if (confirm){
        var url = "path/to/delete.php";
        var data = "emp_id="+empId;
        $.ajax({
            type: "POST",
            url: "otherfile.php",
            data: data ,
            success: function(){         
                alert("Employee deleted successfully.");
            }
        });
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!