如何通过java脚本将php值从一个文件传递到另一个文件 [英] how to pass php value from one file to another through java script

查看:104
本文介绍了如何通过java脚本将php值从一个文件传递到另一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Concrete-5 CMS,我在将值表单视图传递给controller时遇到了问题。在我的应用程序中,我使用以下代码来显示员工角色。

I am working with Concrete-5 CMS, I have an issue in passing value form view to controller.In my application I am using following code for displaying employee role.

foreach($rd as $data){
    echo "<tr><td>".$data[role_name]."</td><td>".$data[role_description]."</td><td><a href=".$this->action('edit', $data['role_id']).">Edit</a></td><td>".$ih->button_js(t('Delete'), "deleteRole('".$data['role_id']."')", 'left', 'error')."</td></tr>";
}
<input type="hidden" name="rno" id="rno" />

脚本:

$delConfirmJS = t('Are you sure you want to remove this Role?'); ?>

<script type="text/javascript">
    function deleteRole(myvar) {
    var role = document.getElementById('rno');
    role.value = myvar;
    if (confirm('<?php echo $delConfirmJS ?>')) {
        $('#rolelist').submit();
        //location.href = "<?php echo $this->url('/role/add_role/', 'delete', 'myvar')?>";
    }
}
</script>

html代码
我通过传递edit_id进行编辑操作来编辑操作。但是,如果删除我应该要求一个构造,所以我使用java脚本来符合它并调用href位置和所有。

html code I did edit operation by passing role_id through edit action. But, In case of delete i should ask for a conformation, so I use java script to conform it and call the href location and all.

但我不知道如何将role_id传递给脚本并传递给我的控制器。如何实现这个任务?

But i don't know how to pass the role_id to script and pass to my controller. how to achieve this task?

谢谢
Kumar

thanks Kumar

推荐答案

您可以使用ajax调用将值传递给服务器。
请参阅以下代码。这里我们使用确认框来获取用户确认。

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.");
       }
     });
  }
}

在delete.php中,您可以获取员工ID使用 $ _ POST ['emp_id']

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

这篇关于如何通过java脚本将php值从一个文件传递到另一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆