Click here to Skip to main content
15,922,015 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm a newbie to Javascript and from what I've read I assume there is a much better way to my approach, but here it goes.

I have a form with onclick such that when a clear image is clicked on in a table cell, the background pic of the cell changes (showing that it has been selected, or unselected if clicked again). That all works, now I want to define some variable so that when the form's Submit button is pressed, a form variable is passed showing whether or not the user selected that option.

The functional javascript I found on the internet:

JavaScript
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 was to write an IF statement stating if curPic1 == 1 (the background image chosen shows that the user has selected), I would create a Global variable which I could then pass on to php in the form:

PHP
$spot1 = "<script>document.write(spot1);</script>";
if ($spot1 == 1) echo "<input name='spot1' type='hidden' value='1' />";


For testing purposes I tried:

PHP
$spot1=$_POST['spot1'];
if ($spot1=="1") echo "This really works<br>";


and it works if I use If $spot1==0, whereby at the top of my javascript function I tried the following:

JavaScript
spot1=0;
var spot1=0;


and within the javascript function itself I tried:

JavaScript
spot1 = 1;
window.spot1 = 1;


I haven't yet tried the IF statement because I'm just trying to test things if they work. I read that I was supposed to be able to establish a Global variable from within a function. I added the various options above one at a time. It works with spot=0, so I assume the problem is that it is not being set as a Global variable within the function? Or perhaps there is a better approach to all this?

On Submit the hidden form variables will eventually be passed to another page and used there.

What I have tried:

Spent hours trying to learn from other examples but I guess I'm just too new at javascipt. Read some tutorials as well. Thank you for your help.
Posted
Updated 31-Jul-17 1:15am
Comments
SrikantSahu 29-Jul-17 3:45am    
I dont think, you will be able to post a javascript variable to PHP , unless you put the varaiable value in some input or some other html controls. You can also do an ajax call where you can pass the variable as a json Key-pair value.

1 solution

Since it's a form, create an input with attribute 'hidden'. Set it's value, via DOM (document.getElementById('hidden_input_id').value='something') as the target of your onclick() event.

Take advantage of caching information via the 'hidden' input - it's really useful.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900