By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,883 Members | 955 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,883 IT Pros & Developers. It's quick & easy.

declaring variable in onchange to use in php

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
"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.