473,396 Members | 1,982 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 2581
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: R.G. Vervoort | last post by:
I am using a javafunction (onclick in select) in which i am calling a function in php (thats why i send this to both php and javascript newsgroups). in the onclick i call the function...
3
by: Paul Eghbal | last post by:
Hi all, I'm trying to use the following script: <script language="javaScript"> function setrepto(){ document.aForm.repno.value = document.aForm.rep.options.value; } </script>
8
by: xxgeorge | last post by:
hi all, I'm writing a function that, say, loop through input fields in a form and set default values to it. Some fields are dependent thus I have some onChange scripts to kick off...
4
by: Dan | last post by:
Can anyone offer suggestions on how to do this or if it is possible? I have a form that uses a drop down box and 2 text fields. What I am trying to do is have the value of each text box set by...
2
by: joltman | last post by:
OK, this is kind of hard to explain, so I'll do my best: I have a form where I have a row where there could be multiple entries, so I have a link where it will dynamically add another row like it,...
1
by: Maarten Terlingen | last post by:
Sample: ClassA: ClassB ClassB: ClassC ClassA x = new Class C I want to know the declaring type of C. x.GetType() returns C x.GetType().BaseType returns B x.GetType().BaseType.BaseType...
5
by: Daz | last post by:
Hi everyone. Another simple question which should hopefully warrent a simple answer. I would like a function to be run whenever a variable is updated. I have tried binding the onchange event...
8
by: SM | last post by:
I've always wonder if there is diference when declaring and initializing a varible inside/outside a loop. What's a better practice? Declaring and initializing variables inside a loop routine,...
0
by: David C | last post by:
I am getting this error in the Databound event of a GridView and I am having a difficult time debugging. I do not get the error on every record that I edit in the GridView. The error is occurring...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.