在我的代码中,我用json完成了一个ajax脚本并保存了你可以在下面看到的变量。之后,我试图在我的 if语句中使用该变量,该变量在php中编码。有可能吗?
//Script variables
var $trstatus_1; //value = GROUP_3, GROUP_2, GROUP_1 or UNDEF
</script>
<?php
//Status
$id; //php variable
echo "<div id='images'>";
if($trstatus_1 == "GROUP_3"){
echo "<a id='".$id1."' class='tooltip1' href='#'><img src='ball_red_glossy.png' alt='' width='70' height='50' style='position: relative; left: 0px;' /><span>RED</span></a>";
}
else if($trstatus_1== "GROUP_2"){
echo "<a id='".$id1."' class='tooltip1' href='#'><img src='ball_orange_glossy.png' alt='' width='70' height='50' style='position: relative; left: 0px;' /><span>ORANGE</span></a>";
}
else if($trstatus_1 == "GROUP_1"){
echo "<a id='".$id1."' class='tooltip1' href='#'><img src='ball_yellow_glossy.png' alt='' width='70' height='50' style='position: relative; left: 0px;' /><span>YELLOW</span></a>";
}
else if($trstatus_1 == "UNDEF"){
echo "<a id='".$id1."' class='tooltip1' href='#'><img src='ball_blue_glossy.png' alt='' width='70' height='50' style='position: relative; left: 0px;' /><span>BLUE</span></a>";
}
echo "</div>";
?>
所以是的,唯一的问题是如果可以在我的IF STATEMENT中使用$ trstatus_1。以及如何:)
BR
亚历
更新
<script>
setInterval("yourAjaxCall()",10000);
function yourAjaxCall()
{
$.ajax(
{
type: "POST",
url: 'api.php', //the script to call to get data
data: {vtrstatus: $V_TR_STATUS}, //you can insert url argumnets here to pass to api.php
dataType: 'json', //data format
success: function(data) //on recieve of reply
{
var trstatus = data[0].group_class;
trstatus_1 = trstatus;
var trstatus_1;
//alert(trstatus_1);
}
});
};
</script>
<script>
//Script variables
var trstatus_1;
var color;
if( $trstatus_1 == 'GROUP_1' ){
color = 'red';
}else if ( $trstatus_1 == 'GROUP_2' ){
color = 'orange';
}else if ( $trstatus_1 == 'GROUP_3' ){
color = 'yellow';
}else if ( $trstatus_1 == 'UNDEF' ){
color = 'blue';
}
var el = "<a class='tooltip1' href='#'><img src='ball_" + color + "_glossy.png' alt='' width='70' height='50' style='position: relative; left: 0px;' /><span>" color.toUpperCase() "</span></a>";
$('#images').append(el);
</script>
<div id="images">
</div>
这是对的吗?
BR
答案 0 :(得分:3)
简短回答:不。
你想做什么?您可以尝试使用javascript而不是PHP添加这些按钮,或者您可以重新加载页面,发布变量以在PHP中使用
JavaScript解决方案:
var trstatus_1;
var color;
if( trstatus_1 == 'GROUP_1' ){
color = 'blue';
}elseif ( trstatus_1 == 'GROUP_2' ){
color = 'red';
}elseif ( trstatus_1 == 'GROUP_3' ){
color = 'green';
}elseif ( trstatus_1 == 'UNDEF' ){
color = 'yellow';
}//sorry for being too lazy to do the colours right
var el = "<a id='SeeNotesBelow' class='tooltip1' href='#'><img src='ball_" + color + "_glossy.png' alt='' width='70' height='50' style='position: relative; left: 0px;' /><span>" + color.toUpperCase() + "</span></a>"
//assuming jQuery
$('#images').append(el);
//also assuming #images already exists
注意:如果您需要在PHP中生成ID,请创建元素,然后对其进行修改,如下所示:
//again, assuming jQuery
$('#images .tooltip1 img').src = 'ball_' + color + '_glossy.png';
$('#images .tooltip1 span').innerHTML = color.toUpperCase();
答案 1 :(得分:0)
不是因为Javascript在服务器上运行客户端和PHP而不可能。但是您可以将您的javascript变量发布到该PHP页面并获取要在PHP中使用的变量值。
答案 2 :(得分:0)
PHP代码在服务器端执行。 Javascript代码在客户端执行。
您可以从PHP填充Javascript变量,因为首先执行服务器端脚本,但反过来是不可能的。
答案 3 :(得分:0)
你可以将php部分减少到以下行
echo "<a id='".$id1."' class='tooltip1' href='#'><img src='#' alt='' width='70' height='50' style='position: relative; left: 0px;' /><span></span></a>";
然后在javascript端移动if并根据trstatus的值修改标记。
答案 4 :(得分:0)
<script>
var $trstatus_1; //value = GROUP_3, GROUP_2, GROUP_1 or UNDEF
sessionSave('status',$trstatus_1);
function sessionSave(field,value)
{
val = $(value).val();
$.post("sessionpage.php", { name: field, value:val } );
}
</script>
//new page in php
sessionpage.php
<?php session_start();
$name=$_REQUEST['name'];
$value=$_REQUEST['value'];
$_SESSION[$name]=nl2br($value);
?>
<?php
// new variable
$trstatus_1=$_SESSION['status'];//value = GROUP_3, GROUP_2, GROUP_1 or UNDEF
//Status
$id; //php variable
echo "<div id='images'>";
if($trstatus_1 == "GROUP_3"){
echo "<a id='".$id1."' class='tooltip1' href='#'><img src='ball_red_glossy.png' alt='' width='70' height='50' style='position: relative; left: 0px;' /><span>RED</span></a>";
}
else if($trstatus_1== "GROUP_2"){
echo "<a id='".$id1."' class='tooltip1' href='#'><img src='ball_orange_glossy.png' alt='' width='70' height='50' style='position: relative; left: 0px;' /><span>ORANGE</span></a>";
}
else if($trstatus_1 == "GROUP_1"){
echo "<a id='".$id1."' class='tooltip1' href='#'><img src='ball_yellow_glossy.png' alt='' width='70' height='50' style='position: relative; left: 0px;' /><span>YELLOW</span></a>";
}
else if($trstatus_1 == "UNDEF"){
echo "<a id='".$id1."' class='tooltip1' href='#'><img src='ball_blue_glossy.png' alt='' width='70' height='50' style='position: relative; left: 0px;' /><span>BLUE</span></a>";
}
echo "</div>";
?>