469,936 Members | 2,413 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,936 developers. It's quick & easy.

declaring variable in onchange to use in php

is it possible to declare a variable in the onchange event of a <select> and
use it later in php

I would like to put the value of the selected option (in <select>) in a
variable and later when i activate a php function use this variable

Looks inpossible but who knows maybe it is possible
roy
Jul 23 '05 #1
2 2373
On Thu, 15 Apr 2004 23:45:42 +0200, R.G. Vervoort
<ro**********@royvervoort.nl> wrote:
is it possible to declare a variable in the onchange event of a <select>
and use it later in php

I would like to put the value of the selected option (in <select>) in a
variable and later when i activate a php function use this variable

Looks inpossible but who knows maybe it is possible


You'll have to send it to the server using either the search (query) part
of the URI, or POSTed in a form. Server-side languages cannot obtain the
values of script variables directly as that data only exists on the client.

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 23 '05 #2
"R.G. Vervoort" wrote:
is it possible to declare a variable in the onchange event of a <select> and
use it later in php

I would like to put the value of the selected option (in <select>) in a
variable and later when i activate a php function use this variable

Looks inpossible but who knows maybe it is possible

roy


<select onchange="sendToServer(this);">

<script type="text/javascript">
function sendToServer(sel) {
(new Image()).src = 'path/to/your/file.php?theValue=' +
sel.options[sel.selectedIndex].value;
}
</script>

You have no way of knowing whether or not the call succeeded using this method.
You can achieve some degree of error checking by having the file.php retrieve
and send an image of specific dimensions back to the client, then you could
interrogate those dimensions to determine whether the call succeeded or not:

<script type="text/javascript">
function sendToServer(sel) {
var i = new Image();
i.onload = imgOnLoad;
i.onerror = imgOnError;
i.src = 'path/to/your/file.php?theValue=' +
sel.options[sel.selectedIndex].value;
// file.php would do whatever with the value passed and then
// return an image that is 10 pixels wide if everything succeeded
// and an image that is 20 pixels wide if anything failed
}
function imgOnLoad() {
if (this.width == 10) {
alert('Success');
} else if (this.width == 20) {
alert('Failure');
}
}
function imgOnError() {
alert('Call to set theValue on the server failed');
}
</script>

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by R.G. Vervoort | last post: by
8 posts views Thread by xxgeorge | last post: by
1 post views Thread by Maarten Terlingen | last post: by
5 posts views Thread by Daz | last post: by
reply views Thread by David C | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.