如何访问php脚本中的javascript变量?
如果我想在php脚本变量中保存一个javascript变量值,比如说如何访问php脚本中的javascript变量?,javascript,php,Javascript,Php,如果我想在php脚本变量中保存一个javascript变量值,比如说var a,而不使用post/get方法或ajax,我应该怎么做 <script> var count=3; $("#add_driver").click(function () { $( "#add_driver_section").replaceWith( "<div class='wrap-input100 validate- input bg1 rs1-wrap-input100'
var a
,而不使用post/get方法或ajax,我应该怎么做
<script>
var count=3;
$("#add_driver").click(function () {
$( "#add_driver_section").replaceWith( "<div class='wrap-input100 validate-
input bg1 rs1-wrap-input100' > <span class='label-input100'>Gender</span>
<div class='contact100-form-radio m-t-15'> <input class='input-radio100'
id='male-radio"+ count +"' type='radio' name='type-product"+ count +"'
value='male' checked='checked' > <label class='label-radio100' for='male-
radio"+ count +"'> Male </label></div><div class='contact100-form-
radio'> <input class='input-radio100' id='female-radio"+ count +"'
type='radio' name='type-product"+ count +"' value='female' > <label
class='label-radio100' for='female-radio"+ count +"' > Female </label>
</div></div>");
count++;}
);
</script>
var计数=3;
$(“#添加驱动程序”)。单击(函数(){
$(“#添加#驱动程序#节”)。替换为(“性别
男女
");
计数+++;}
);
我编辑了我的问题,让你们对我想要实现的目标有更多的看法。在这里,我首先要检查是否设置了type product“+count+
。如果设置了,那么我希望单选按钮的selected
属性选择值。
<script>
var a = "variable a";
</script>
<?php
$a = "<script>document.write(a)</script>";
echo $a;
?>
var a=“变量a”;
请尝试使用此代码将js变量保存在php中。变量
Cookies
可用于在服务器和浏览器之间共享数据,因为它们可以从侧面访问。可以使用JAVASCRIPT()将数据设置为cookie可以使用PHP中的$\u COOKIE
变量在服务器上访问。您可以在javascript中使用PHP变量,但是,不要将其放在.js文件中,而应该放在.PHP或.html文件中
<script>
var a = '<?php echo myvariable ?>';
</script>
var a=“”;
但是,如果您想动态存储变量以备将来使用,可以使用cookies来存储变量
function setCookie(name,value,days) {
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days*24*60*60*1000));
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + (value || "") + expires + "; path=/";
}
function getCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name) {
document.cookie = name+'=; Max-Age=-99999999;';
}
函数setCookie(名称、值、天数){
var=”;
如果(天){
变量日期=新日期();
date.setTime(date.getTime()+(天*24*60*60*1000));
expires=“;expires=“+date.toutString();
}
document.cookie=name+“=”+(值| |“”)+expires+“path=/”;
}
函数getCookie(名称){
变量nameEQ=name+“=”;
var ca=document.cookie.split(“;”);
对于(变量i=0;i
您可以使用如下代码:
function createhtmlcode(){
var a = getCookie('typeprod');
var myhtml = '<p>html code here';
if (a) {
myhtml += <input type='radio' name='type-product"+ count +"' value='female' selected >
}else{
myhtml += <input type='radio' name='type-product"+ count +"' value='female'>
}
myhtml += 'your next code here</p>';
return myhtml;
}
$(document).ready(function(){
setCookie('typeprod',true,7); //7 day, use this to store your variable to memory/cookies.
$("#add_driver").click(function () {
var htmlcode = createhtmlcode();
$( "#add_driver_section").replaceWith(htmlcode);
};
});
函数createhtmlcode(){
var a=getCookie('typeprod');
var myhtml='html代码在这里';
如果(a){
myhtml+=
}否则{
myhtml+=
}
myhtml+=“您在此处的下一个代码”;
返回myhtml;
}
$(文档).ready(函数(){
setCookie('typeprod',true,7);//7天,使用它将变量存储到内存/cookies中。
$(“#添加驱动程序”)。单击(函数(){
var htmlcode=createhtmlcode();
$(“#添加#驱动程序#节”)。替换为(htmlcode);
};
});
为什么不想使用post/get方法或ajax?我可以使用ajax,但我不想要非常复杂或冗长的代码。你能使用jquery吗?@RyDog是的,只要不太复杂。可能会重复