declaring variable in onchange to use in php

R

R.G. Vervoort

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
 
M

Michael Winter

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
 
G

Grant Wagner

R.G. Vervoort said:
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 <[email protected]>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_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-developer/upgrade_2.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top