Yii:将Javascript变量传递给PHP
我的javascript块中有一个元素,我想将其值传递给php。 变量是 $('#datefirst').val() 及 $('#datelast').val() 我的代码是(应该给出我所谈论的内容的想法)Yii:将Javascript变量传递给PHP,php,javascript,yii,Php,Javascript,Yii,我的javascript块中有一个元素,我想将其值传递给php。 变量是 $('#datefirst').val() 及 $('#datelast').val() 我的代码是(应该给出我所谈论的内容的想法) $('#datefirst').val(); $('#datelast').val(); window.location=“”; 有什么想法可以帮助我完成这个壮举吗?PHP在服务器上运行。它(通常)生成一个HTML文档(可能带有嵌入式JavaScript)。然后将其交付给客户机。至此,PH
$('#datefirst').val();
$('#datelast').val();
window.location=“”;
有什么想法可以帮助我完成这个壮举吗?PHP在服务器上运行。它(通常)生成一个HTML文档(可能带有嵌入式JavaScript)。然后将其交付给客户机。至此,PHP脚本已经完成 客户端获取文档并运行任何JavaScript 将数据返回服务器的唯一方法是发出新的HTTP请求 您可能希望执行以下操作:
window.location = "some.php?datefirst=" + encodeURIComponent(datefirst) + "&datelast=" + encodeURIComponent(datelast);
…然后在some.php中处理提交的数据。如果您使用window.location将其跳转到新页面,为什么不继续使用javascript并将变量添加到跳转到的URL的末尾?比如
Window.location = "<code>"+"?datefirst"+('#datefirst').val()+"&datelast="+('#datelast').val();
不能将js值分配给php。 你可以像这样实现重定向
var firstdate=$('#datefirst').val();
var lastdate=$('#datelast').val();
var url = "<?php echo CController::createAbsoluteUrl('export/motheradmission',
array('datefirst'=>'#firstdate','datelast'=>'#lastdate')) ?>";
url = url.replace('#firstdate',firstdate);
url = url.replace('#lastdate',lastdate);
window.location.href=url;
var firstdate=$('#datefirst').val();
var lastdate=$('#datelast').val();
var url=“”;
url=url.replace(“#firstdate”,firstdate);
url=url.replace(“#lastdate”,lastdate);
window.location.href=url;
请看一看。由于原始脚本将浏览器重定向到另一个URL,因此Ajax只会使事情复杂化,在这种情况下没有任何好处。
var firstdate=$('#datefirst').val();
var lastdate=$('#datelast').val();
var url = "<?php echo CController::createAbsoluteUrl('export/motheradmission',
array('datefirst'=>'#firstdate','datelast'=>'#lastdate')) ?>";
url = url.replace('#firstdate',firstdate);
url = url.replace('#lastdate',lastdate);
window.location.href=url;