在PHP代码中添加javascript变量以解密JSON
基本上我有PHP代码和javascript代码的组合。我的mySQL数据是使用CodeIgniter加密的,因此要加载json格式的数据(查看和编辑),我需要再次解密它。我的问题是如何使我的“$x”变量动态 谢谢在PHP代码中添加javascript变量以解密JSON,javascript,php,json,ajax,encryption,Javascript,Php,Json,Ajax,Encryption,基本上我有PHP代码和javascript代码的组合。我的mySQL数据是使用CodeIgniter加密的,因此要加载json格式的数据(查看和编辑),我需要再次解密它。我的问题是如何使我的“$x”变量动态 谢谢 function edit_person(id) { save_method = 'update'; $('#form')[0].reset(); $('#modal_form').modal({backdrop: 'st
function edit_person(id)
{
save_method = 'update';
$('#form')[0].reset();
$('#modal_form').modal({backdrop: 'static', keyboard: true, show: true });
<?php
$x = 13; //<== **i need to make this $x dynamic based on "edit_person(id)"** //
$url = "http://myurlhere.com/main/ajax_edit/".$x;
$datax = file_get_contents($url);
$string = json_decode($datax, TRUE);
?>
$.ajax({
url : "<?php echo site_url('main/ajax_edit')?>/" + id,
type: "GET",
dataType: "JSON",
success: function(data)
{
$('[name="id"]').val(data.id);
// ** below code "firstName" is my decryption requirement ** //
$('[name="firstName"]').val("<?php echo $this->encryption->decrypt($string['firstName']); ?>");
$('#modal_form').modal('show');
},
error: function (jqXHR, textStatus, errorThrown)
{
alert('Error get data from ajax');
}
});
}
功能编辑人员(id)
{
save_方法='update';
$('#form')[0].reset();
$(“#modal_form”).modal({background:'static',键盘:true,show:true});
$.ajax({
url:“/”+id,
键入:“获取”,
数据类型:“JSON”,
成功:功能(数据)
{
$('[name=“id”]').val(data.id);
//**以下代码“firstName”是我的解密要求**//
$('[name=“firstName”]').val(“”);
$('modal#u form')。modal('show');
},
错误:函数(jqXHR、textStatus、errorshown)
{
警报(“从ajax获取数据时出错”);
}
});
}
您可能混淆了服务器端代码和客户端代码。
简单来说:首先,客户端向服务器发送请求。目标PHP代码仅在服务器端执行。然后它生成一个HTML文件,其中包含您的JS代码。此文件发送到客户端,仅在客户端执行。那时,在客户端,只有JS代码,不再有PHP代码。所有PHP代码都会被某些值替换,或者被删除或忽略
如果您想从JS代码中访问一些PHP功能,必须从客户端向服务器发送请求。正如您使用AJAX调用所做的那样
因此,为了使$x动态化,必须调用一些PHP代码并将ID作为参数传递
以一种非常简单的方式,您可以通过以下方式实现:
$.ajax({
url : "your url to some file.php/?id=" + id,
type: "GET",
})
一些file.php
<?php
$x = $_GET["id"]; //<== $_GET["id"] will return the value of the parameter "id" in the url
?>
从这里开始,您应该阅读更多关于AJAX调用、输入和验证的信息,以保护您的请求。感谢您的回复。捕获所需PHP“$x”变量的最简单方法可能是获取javascript函数id。如何在“function edit_person(id)”中获取javascript“id”并将其用作PHP变量。您无法在PHP代码中捕获JS函数id,因为它们是不同的实例。想象一下,就像两个人在通过电子邮件交流一样。人A是PHP,人B是JS。如果您想在PHP中使用ID,那么JS必须向PHP发送一个get请求,ID作为参数。然后,您可以执行一个PHP脚本,该脚本将获取该参数并对其进行处理。