In my code i have done an ajax script with json and saved the variable you can see below. After that i'm trying to use the variable in my if statement, which is coded in php. Is it possible to do that?
//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>";
?>
So yes, the only question is if it is possible to use $trstatus_1 in my IF STATEMENT. and how :)
BR
Alex
UPDATE
<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>
Is this correct?
BR
In my code i have done an ajax script with json and saved the variable you can see below. After that i'm trying to use the variable in my if statement, which is coded in php. Is it possible to do that?
//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>";
?>
So yes, the only question is if it is possible to use $trstatus_1 in my IF STATEMENT. and how :)
BR
Alex
UPDATE
<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>
Is this correct?
BR
Share Improve this question edited Oct 2, 2013 at 7:15 PeterP asked Oct 1, 2013 at 12:58 PeterPPeterP 713 silver badges12 bronze badges 7- 4 No, it is not. PHP is server side and Javascript is client side. Javascript will run after PHP finished. You can only transfer a value from Javascript to PHP by requesting a new PHP from the server – devnull69 Commented Oct 1, 2013 at 12:59
- @alex java !== javascript – Lud Commented Oct 1, 2013 at 13:01
- @devnull69 There is no "Javascript", and the ECMAScript implementation is client-side, and PHP is server-side here. – PointedEars Commented Oct 1, 2013 at 13:04
- indeed, granted. For matters of pleteness: There are ECMAScript implementations that are server side. But in this example I assumed you are using a client side implementation monly called "Javascript" :-) – devnull69 Commented Oct 1, 2013 at 13:07
- it would appear that the your JavaScript variable is invalid as it has a '$' at the beginning.. I'm not 100% sure that that's not right but I wouldn't do it myself... – Bill Commented Oct 1, 2013 at 13:38
5 Answers
Reset to default 3Short answer: No.
What are you trying to do? You could try adding those buttons with javascript instead of PHP, or you can reload the page, posting the variable to use in PHP
JavaScript solution:
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
Note: if you need the ID to be generated in PHP, create the element, then modify it, like so:
//again, assuming jQuery
$('#images .tooltip1 img').src = 'ball_' + color + '_glossy.png';
$('#images .tooltip1 span').innerHTML = color.toUpperCase();
PHP Code executes in Server side. Javascript code executes in Client Side.
You can populate Javascript variable from PHP as server side script is executed first but the reverse is not possible.
Not it's not possible as Javascript runs on the client and PHP on the server. But you could post your javascript variable to that PHP page and get the variable value to be used in PHP.
you can just reduce the php part to the following line
echo "<a id='".$id1."' class='tooltip1' href='#'><img src='#' alt='' width='70' height='50' style='position: relative; left: 0px;' /><span></span></a>";
and then move the if on the javascript side and modify the markup based on the value of trstatus.
<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>";
?>