javascript - onclick variable is not passing to a form -
i'm newbie javascript , i've read assume there better way approach, here goes.
i have form onclick such when clear image clicked on in table cell, background pic of cell changes (showing has been selected, or unselected if clicked again). works, want define variable when form's submit button pressed, form variable passed showing whether or not user selected option.
the functional javascript found on internet:
var curpic1 = 0; window.onload=function() { document.getelementbyid('clear-kayangan').onclick=function() { curpic1 = (curpic1 == 0)? 1 : 0; document.getelementbyid('td_kayangan').style.backgroundimage = (curpic1 == 1)? 'url("pics/map-kayangan-lake-selected.png")' : 'url("pics/map-kayangan-lake.png")'; } }
my plan write if statement stating if curpic1 == 1
(the background image chosen shows user has selected), create global variable pass on php in form:
$spot1 = "<script>document.write(spot1);</script>"; if ($spot1 == 1) echo "<input name='spot1' type='hidden' value='1' />";
for testing purposes tried:
$spot1=$_post['spot1']; if ($spot1=="1") echo "<b>this works</b><br>";
and works if use if $spot1==0
, whereby @ top of javascript function tried following:
int spot1; spot1=0; var spot1=0;
and within javascript function tried:
int spot1 = 1; spot1 = 1; window.spot1 = 1;
i haven't yet tried if statement because i'm trying test things if work. read supposed able establish global variable within function. added various options above 1 @ time. works spot=0
, assume problem is not being set global variable within function? or perhaps there better approach this?
on submit hidden form variables passed page , used there.
declare hidden variable in form, , set it's value 0 or 1 in onclick fuction of image. when form submitted, value passed server
Comments
Post a Comment