Jump to content

[SOLVED] Passing Javascript value to PHP! but how?


kucing

Recommended Posts

Dear friends,

 

I have search google and tested many examples and I found non of them working for me..

So now I need your help to solve this thing..

 

Actually what I wanted to do was save one value from javascript to my database but when doing that I don't want my page to refreash and also don't want any button asking me to submit then save!

 

Any thing will be best which do it automaticaly without clicking anywhere also could be ajax thing.

Or also if possible sending that value directly to $_SESSION will be best.

 

Here is my code example.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Java2php Test</title>

<script type="text/javascript">
	var N=5;
	var array = new Array()
	var items = list.getElementsByTagName("li")

	for (var i = 0, n = N; i < n; i++) {
		var item = items[i]
		array.push(myFnc(item))
	}
	alert(array); // <----- i want this array to forward to my php script
	return array.join('|')
</script>

</head>
<body>

<ul id="menu">
<li class="box" id=1>TEST1</li>
<li class="box" id=2>TEST2</li>
<li class="box" id=3>TEST3</li>
<li class="box" id=4>TEST4</li>
<li class="box" id=5>TEST5</li>
<li class="box" id=6>TEST6</li>
<li class="box" id=7>TEST7</li>
<li class="box" id=8>TEST8</li>
<li class="box" id=9>TEST9</li>
<li class="box" id=10>TEST10</li>
</ul>

</body>
</html>

Thanks

Any help is very much appreciated :)

K

Jesirose very thanks for a very quick response.:)

 

I also want to mention that I already have many .js running at a time and if I add one more .js file my site is really gonnd be very slow :(

 

So i was looking forward for a way which doesn't involve some complex but a easy way to pass a variable to php without my page get refreash.

 

Thanks again :)

K

Then you need to clean up your javascript, honestly. If your javascript files are so big that it slows down the site, you need to move all of your JS into one file, and possibly even compress it. The mootools framework is a very light one which is really great, so I think it could help with all your JS.

Here how I did that :)

 

 

 

<script type="text/javascript">
	var N=5;
	var array = new Array()
	var items = list.getElementsByTagName("li")

	for (var i = 0, n = N; i < n; i++) {
		var item = items[i]
		array.push(myFnc(item))
	}
	alert(array); // <----- i want this array to forward to my php script
	saveIT.innerHTML = '<IFRAME src="myTest.php?lid='+array+'" width="300" height="200" frameborder="0"></IFRAME>';
	return array.join('|')

    }
</script>

 

So this little thing saved my another 2 days.. lol

 

saveIT.innerHTML = '<IFRAME src="myTest.php?lid='+array+'" width="300" height="200" frameborder="0"></IFRAME>';

 

<div id="saveIT"></div>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.