Php Jquery Onclick将数据发送到新打开的窗口
我知道这是一个常见的问题,但我找不到任何答案。 我有一个包含链接的PHP文件。单击该链接时,我想在新窗口中打开另一个页面,在这个新打开的页面上,我想获得上一页上单击的链接的ID属性 当前页面='patientreg.php'Php Jquery Onclick将数据发送到新打开的窗口,php,javascript,jquery,post,Php,Javascript,Jquery,Post,我知道这是一个常见的问题,但我找不到任何答案。 我有一个包含链接的PHP文件。单击该链接时,我想在新窗口中打开另一个页面,在这个新打开的页面上,我想获得上一页上单击的链接的ID属性 当前页面='patientreg.php' 要在新窗口中打开的新页面='patient\u history.php' 将数据从当前页面发布到新加载的页面 当前页面代码: HTML: <a id="37" onclick="phistory(this.id);" href="#">View history&
要在新窗口中打开的新页面='patient\u history.php'
将数据从当前页面发布到新加载的页面 当前页面代码: HTML:
<a id="37" onclick="phistory(this.id);" href="#">View history</a>
function phistory(myVar) {
var myurl = '../main/patient_history.php';
$.post(myurl,{data:myVar},function(){});
window.open(myurl,'_blank');
}
$phid = $_REQUEST["data"];
echo phid;
var pid=0;
function phistory(myVar) {
pid=myVar;
window.open("../main/patient_history.php");
}
pid=window.opener["pid"];
alert(pid); //Just to check that it's working
新页面代码:
<a id="37" onclick="phistory(this.id);" href="#">View history</a>
function phistory(myVar) {
var myurl = '../main/patient_history.php';
$.post(myurl,{data:myVar},function(){});
window.open(myurl,'_blank');
}
$phid = $_REQUEST["data"];
echo phid;
var pid=0;
function phistory(myVar) {
pid=myVar;
window.open("../main/patient_history.php");
}
pid=window.opener["pid"];
alert(pid); //Just to check that it's working
问题是它没有在下一页上回显数据,但我可以在当前页面的Firebug response选项卡中看到完整的新页面的回显。试试这个。对于JavaScript
onclick
处理程序:
function phistory(myVar) {
var myurl = '../main/patient_history.php?data='+myVar;
window.open(myurl);
}
然后,在PHP文件中:
$phid = $_REQUEST["data"];
echo $phid;
您的问题是您误解了jQuery的.post()
方法的使用。它只是检索页面(使用指定的POST参数)——它不会修改页面,这样当您在新窗口中打开页面时,它将具有您指定的ID参数。我切换了它,使它不再使用jQuery的.post()
,而是通过使用?data=[YOUR-ID]
打开新窗口来使用GET参数。您会注意到,PHP基本保持不变,但我更改了echo语句,在变量名之前包含了$
符号。这可能只是问题中的一个输入错误,但如果代码中有输入错误,这就是问题的一部分
另一种可能是完全删除查询字符串中的ID,而是将其作为全局变量存储在第一页上,然后使用第二页上的window.opener
属性检索该ID,如下所示:
第一页:
<a id="37" onclick="phistory(this.id);" href="#">View history</a>
function phistory(myVar) {
var myurl = '../main/patient_history.php';
$.post(myurl,{data:myVar},function(){});
window.open(myurl,'_blank');
}
$phid = $_REQUEST["data"];
echo phid;
var pid=0;
function phistory(myVar) {
pid=myVar;
window.open("../main/patient_history.php");
}
pid=window.opener["pid"];
alert(pid); //Just to check that it's working
第二页(JavaScript):
<a id="37" onclick="phistory(this.id);" href="#">View history</a>
function phistory(myVar) {
var myurl = '../main/patient_history.php';
$.post(myurl,{data:myVar},function(){});
window.open(myurl,'_blank');
}
$phid = $_REQUEST["data"];
echo phid;
var pid=0;
function phistory(myVar) {
pid=myVar;
window.open("../main/patient_history.php");
}
pid=window.opener["pid"];
alert(pid); //Just to check that it's working
试试这个。对于JavaScript
onclick
处理程序:
function phistory(myVar) {
var myurl = '../main/patient_history.php?data='+myVar;
window.open(myurl);
}
然后,在PHP文件中:
$phid = $_REQUEST["data"];
echo $phid;
您的问题是您误解了jQuery的.post()
方法的使用。它只是检索页面(使用指定的POST参数)——它不会修改页面,这样当您在新窗口中打开页面时,它将具有您指定的ID参数。我切换了它,使它不再使用jQuery的.post()
,而是通过使用?data=[YOUR-ID]
打开新窗口来使用GET参数。您会注意到,PHP基本保持不变,但我更改了echo语句,在变量名之前包含了$
符号。这可能只是问题中的一个输入错误,但如果代码中有输入错误,这就是问题的一部分
另一种可能是完全删除查询字符串中的ID,而是将其作为全局变量存储在第一页上,然后使用第二页上的window.opener
属性检索该ID,如下所示:
第一页:
<a id="37" onclick="phistory(this.id);" href="#">View history</a>
function phistory(myVar) {
var myurl = '../main/patient_history.php';
$.post(myurl,{data:myVar},function(){});
window.open(myurl,'_blank');
}
$phid = $_REQUEST["data"];
echo phid;
var pid=0;
function phistory(myVar) {
pid=myVar;
window.open("../main/patient_history.php");
}
pid=window.opener["pid"];
alert(pid); //Just to check that it's working
第二页(JavaScript):
<a id="37" onclick="phistory(this.id);" href="#">View history</a>
function phistory(myVar) {
var myurl = '../main/patient_history.php';
$.post(myurl,{data:myVar},function(){});
window.open(myurl,'_blank');
}
$phid = $_REQUEST["data"];
echo phid;
var pid=0;
function phistory(myVar) {
pid=myVar;
window.open("../main/patient_history.php");
}
pid=window.opener["pid"];
alert(pid); //Just to check that it's working
尝试此操作,以便使用表单将数据发送到页面,您可以在锚元素中获得更详细的内容: HTML: 编辑 如果需要为每个故事打开一个新窗口,可以使用元素id的名称创建一个新窗口:
$('.see-history').bind('click', function(event) {
var id = $(this).data('idref');
try {
window.open('about:blank', 'mynewwindow_' + id);
$('#myform').find('#data').val(id);
$('#myform').attr('target', 'mynewwindow_' + id).submit();
} catch(e) {}
return false;
});
尝试此操作,以便使用表单将数据发送到页面,您可以在锚元素中获得更详细的内容: HTML: 编辑 如果需要为每个故事打开一个新窗口,可以使用元素id的名称创建一个新窗口:
$('.see-history').bind('click', function(event) {
var id = $(this).data('idref');
try {
window.open('about:blank', 'mynewwindow_' + id);
$('#myform').find('#data').val(id);
$('#myform').attr('target', 'mynewwindow_' + id).submit();
} catch(e) {}
return false;
});
但老兄,我不能用这种方式使用查询,因为通过手动更改url中的值,可以查看一些其他患者的历史,这是不安全的。对不起,我不知道。怎么做?它足够安全吗?@kaushil说真的,你在写病历程序吗?或者它只是一个演示。确保安全的唯一方法是使用ssl、登录和sessions@Tosh-说得好。欺骗帖子几乎和欺骗GET一样容易,甚至会话也可能被劫持。对于如此敏感的数据,SSL是有序的。但是,伙计,我不能以这种方式使用查询,因为通过手动更改url中的值,可以查看其他一些患者的历史,这是不安全的。对不起,我不知道。如何才能做到这一点?它足够安全吗?@kaushil说真的,你在写病历程序吗?或者它只是一个演示。确保安全的唯一方法是使用ssl、登录和sessions@Tosh-说得好。欺骗帖子几乎和欺骗GET一样容易,甚至会话也可能被劫持。对于如此敏感的数据,SSL是有序的。如果您将
数据
包装在“$”中会怎么样?post(myurl,{“data”:myVar},function(){});
另外,如果您不使用它,则不必提供成功函数function(){}
它是[]指示的可选参数
文档中的括号:如果将数据
包装到“<代码>$.post(myurl,{“数据”:myVar},function(){})代码>此外,如果未使用success函数,则不必提供该函数function(){}
它是文档中的[]
括号指示的可选参数: