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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.