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

眉间皱痕 提交于 2020-01-01 19:40:36

问题


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" />

script:

$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 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.

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

thanks Kumar


回答1:


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']




回答2:


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



回答3:


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




回答4:


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


来源:https://stackoverflow.com/questions/19905702/how-to-pass-php-value-from-one-file-to-another-through-java-script

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