473,397 Members | 2,084 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,397 software developers and data experts.

onChange of listbox to update link?

I have a PHP page that generates a list box with several options in it.

I would like to have a "view" link next to the list box. When the user
changes the contents of the list box, I would like the view link to
update to point to the image representing that selection. So for
example, if the list box held names of images I would want the view link
to always point at the correct image.

To further complicate this matter, the selections of the listbox are in
PHP variables.

I basically have little to no experience with javascript and could use
some guidance in figuring this out.

Thanks.

Jason
jl*****@NoSpamAustin.rr.com
Jul 23 '05 #1
1 9650
John Doe wrote:
I have a PHP page that generates a list box with several options in it.

I would like to have a "view" link next to the list box. When the user
changes the contents of the list box, I would like the view link to
update to point to the image representing that selection. So for
example, if the list box held names of images I would want the view link
to always point at the correct image.
You just need to change the href of the link behind the 'view'
button/link.

To further complicate this matter, the selections of the listbox are in
PHP variables.
Browsers do not understand PHP. You must write HTML so that the
JavaScript can use the values. You could put the link values
into the document in a meta tag, or as an array in a script
element, but I think using the value attribute of an option is
better.

I basically have little to no experience with javascript and could use
some guidance in figuring this out.


Here is a script that offers two solutions - the first I think
is better. If the user has JavaScript enabled, they click the
'View' button and are taken to the new location (image,
whatever). If not, the selected URL is passed to the server,
where you can return the new link and send them to the new link
anyway.

The second is more what you have asked for, but if JS is not
enabled, then the link should by default go somewhere that
explains why the link doesn't work.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Select</title>
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-1">
<script type="text/javascript">

function gotoLink(s){
var newLink = s[s.selectedIndex].value;

// If no selection made
if (newLink == '') {
alert("Please select a destination");
return false;
}

// If a selection has been made, go there
document.location = newLink;
}

function updateLink(s,p) {
var a;
if (document.getElementById){
a = document.getElementById(p);
} else if (document.all){
a = document.all[p];
}

// Update the link based on selection
a.href = s[s.selectedIndex].value;
a.innerHTML = s[s.selectedIndex].text;
}

</script>
</head>
<body>
<!-- uses a form. If JS enabled, takes user to link
when 'view' button clicked. If JS not enabled,
form submitted to server, can handle new link there.
-->
<form action="" onsubmit="
gotoLink(this.linkSelect);
return false; // If JS enabled, stops form submission;
">
<table>
<tr><td>
<select name="linkSelect">
<option value="" selected>Pick a destination</option>
<option value="http://www.google.com">Google</option>
<option value="http://www.yahoo.com">Yahoo</option>
</select>
</td><td>
<input type="submit" value="View">
</td></tr>
</table>
</form>

<!-- Changes the href of 'view' link onchange. Does not use
a form
-->
<table><tr><td>
<select onchange="updateLink(this,'picLink');">
<option value="" selected>Pick a destination</option>
<option value="http://www.google.com">Google</option>
<option value="http://www.yahoo.com">Yahoo</option>
</select>
</td><td>
<a id="picLink"
href="enableJS.html">Select a destination</a><br>
</td></tr>
</table>
</body>
</html>


--
Rob
Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Lee Mundie | last post by:
Hi there, Simple problem here but can't seem to fix it! Okay, I have a select list from which people choose avatars... the list is option values ie. <option>Worm</option> ...
3
by: NewmanBT | last post by:
As you can see from the code below, several textboxes will be dynamically created and each will be tied to an org. The ChangeComment function should allow for an update to the database whenever...
2
by: Alpha | last post by:
How do I change the selected item in a listbox according to a Combox's selected item on the same form? The Combox is from a different table in the same dataset as the Listbox uses. Combox's...
3
by: James Radke | last post by:
Hello, I was curious, is it possible to create drag and drop interface from listbox to listbox on a webform application (i.e. vb.net)? Basically the application would need to load multiple...
6
by: Mark | last post by:
Hello. I have a listbox whose rowsource is set to a saved query (call it "qry_customer_list.") When I add customers to my database, I call the listbox Requery method so that the listbox will...
3
by: Kevin Walzer | last post by:
I'm trying to set the active item in a Tkinter listbox to my application's currently-defined default font. Here's how I get the fonts loaded into the listbox: ...
1
by: sklett | last post by:
I've setup the simplest of tests; A form with a ListBox and a Button. The ListBox is bound to a List<string>. When you click the Button, a new string is added to the list. I can't for the life...
3
by: deejayquai | last post by:
Hello Simple one this I guess, but I'm quite stuck at the moment. I would like to update the records displayed in my listbox (lstStudents) using criteria selected from my combo (cboForm) in a...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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.