Using php variable in javascript -
this question has answer here:
i know doing totally wrong , haven't got idea how this.. have javascript function , trying use php declare array teams if round equals 1. teams , round come parameters in function teamelement()
function teamelement(round, team, isready) { <?php if(round == 1) $round2_array[] = team) ?> document.write('<p>round: ' + round + '<br>team ' + team.name + '<br> ready ' + isready);
...
anyone can please?
many thanks.
taking @ code, part:
function teamelement(round, team, isready) { <?php if(round == 1) $round2_array[] = team) ?>
i see lack of basic knowledge: javascript client side scripting language, while php server side scripting language. php , javascript executed in 2 different moments (generally php first , javascript (without using ajax)). therefore cannot share variables that easily.
there ways, though, connect 2 of them:
to pass php variable javascript script use string interpolation or templating features of php:
var x = <?php echo $x; ?>;
just notice that considered very bad practice , should use ajax here well.
to pass javascript variable php script can use ajax (asynchronous javascript , xml), execute php script "in background", after page loaded. in regard suggest take @ nice jquery library provides helpful set of functions task.
Comments
Post a Comment