【发布时间】:2017-11-26 22:38:23
【问题描述】:
我正在尝试使用 $_REQUEST 从 url 获取一个 php 变量到 javascript,以便我可以通过 Ajax 发送它。
在我的页面顶部我有:
<?php
include_once('../php/connection.php');
include_once('../php/getDiagnosis.php');
$pid = $_REQUEST['pid'];
?>
在 Java 脚本部分我有:
<script src="../js/history.js"></script>
在 history.js 中:
var addHistory = function()
{
var patient_medication = $("#patient_medicationn").val();
var disease = $("#disease").val();
var patient_side_effect = $("#patient_side_effect").val();
var pid = '<?php echo $pid;?>';
console.log(pid);
if(disease=="select")
{
$("#disease").css('border-color', 'red');
$("#disease").focus();
}
else
{
$.ajax({
url: '../php/history.php',
data: {pid: pid, patient_medication: patient_medication, disease: disease, patient_side_effect: patient_side_effect},
type: 'POST',
dataType: 'TEXT',
success:function(resp)
{
},
error:function(resp)
{
alert("Information have not been added, please try again");
}
})
}
}
$(document).ready(function()
{
$("#add_history").on('click', addHistory);
$("#patient_medication").on('keypress', function(event)
{
if(event.which==13)
{
$("#add_history").click();
}
})
$("#patient_side_effect").on('keypress', function(event)
{
if(event.which==13)
{
$("#add_history").click();
}
})
});
控制台中的结果是:
【问题讨论】:
-
你的JS文件可能没有在服务器端编译,所以PHP代码没有执行。
-
有办法解决吗?
-
有一个很好的方法来解决它 - 请检查我的答案。
-
请确保您的 history.php 检查发出请求的用户是否有权查看他们要求的历史记录。否则任何人都可以通过拥有 pid(或猜测它)来获取历史记录。您可以使用会话变量来做到这一点。
标签: javascript php jquery pdo