...

View Full Version : Dynamic Selection Box That Writes An Answer



mrmbarnes
03-18-2004, 09:02 AM
Hi All

I am not that advanced with JavaScript but learning everyday.

I have a JavaScript that writes the selection of a dropdown box into a textbox.

I want to add it to a PHP page that will add this script for every result in the database.

If I have only 1 entry the script works fine:


<script language="JavaScript">function insertMenuItem(){

document.f.name.value = document.f.insertItem.options[document.f.insertItem.selectedIndex].text;
document.f.link.value = document.f.insertItem.value;

}</script>

<input type=text value='' name=link size=25>

<select name=insertItem><option value=layout-1.php?ID=4>Layout 1</option><option value=layout-2.php?ID=4>Layout 2</option></select> <input type=button value='Insert' onclick=insertMenuItem()>



If I add another it no longer works:



<script language="JavaScript">function insertMenuItem(){

document.f.name.value = document.f.insertItem.options[document.f.insertItem.selectedIndex].text;
document.f.link.value = document.f.insertItem.value;

}</script>

<input type=text value='' name=link size=25>

<select name=insertItem><option value=layout-1.php?ID=4>Layout 1</option><option value=layout-2.php?ID=4>Layout 2</option></select> <input type=button value='Insert' onclick=insertMenuItem()>

<select name=insertItem><option value=layout-1.php?ID=5>Layout 1</option><option value=layout-2.php?ID=5>Layout 2</option></select> <input type=button value='Insert' onclick=insertMenuItem()>



I need to get this script working so there is the option of having may dropdown boxes that will all write to the link box when the insert button is clicked.

Any help or suggestions would be great.

Many Thanks

homerUK
03-18-2004, 11:00 AM
the problem you have is that there is one function for both select fields which are both named the same....

try this:



<script language="JavaScript">
function insertMenuItem1()
{
document.f.name.value = document.f.insertItem1.options[document.f.insertItem1.selectedIndex].text;
document.f.link.value = document.f.insertItem1.value;
}
function insertMenuItem2()
{
document.f.name.value = document.f.insertItem2.options[document.f.insertItem2.selectedIndex].text;
document.f.link.value = document.f.insertItem2.value;
}
</script>
<form name="f">
<input type=text value='' name=link size=25>

<select name="insertItem1"><option value=layout-1.php?ID=4>Layout 1</option>
<option value=layout-2.php?ID=4>Layout 2</option></select>
<input type=button value='Insert' onclick=insertMenuItem1()>

<select name="insertItem2"><option value=layout-1.php?ID=5>Layout 1</option><option value=layout-2.php?ID=5>Layout 2</option></select> <input type=button value='Insert' onclick=insertMenuItem2()>
</form>


:)

mrmbarnes
03-18-2004, 11:24 AM
Great!!! Thanks for your help!

mrmbarnes
03-18-2004, 01:32 PM
Another Question.....

I have that working fine now however I want to make a button that will take you to the page listed in the result box.

The result box code is:

<input type=text value='' name=link size=35 class='form-text'>

I have tried a few things but can not get this working.

Any idea?

Thanks

homerUK
03-18-2004, 01:53 PM
you could write another function, something like



function doUrl()
{
if (document.f.link.value == "") {
alert("Please select a link before pressing this button");
return false;
} else {
document.location = document.f.link.value;
}
}


then make a button



<input type="button" value="go to link" onclick="return doUrl()">


Ive not tested this, but give it a try....

mrmbarnes
03-18-2004, 02:01 PM
That great. Thanks so much for your help.

Now this works if the page to link to is in the same folder but the pages aren't.

They are located 2 folders back - ../../

I have tried adding it but it broke the script.

Is it possible to make it look in any directory I want for the page?

Thanks once again.

mrmbarnes
03-18-2004, 02:03 PM
I would also like to have the link as a popup.

e.g. "javascript:winEdit('the link from the box');"

Can this be done?

homerUK
03-18-2004, 02:05 PM
you could set a variable in the function.. something like



function doUrl()
{
var path = "http://www.somedomain.com/folder/something/";
var path = "../../";

if (document.f.link.value == "") {
alert("Please select a link before pressing this button");
return false;
} else {
document.location = path + document.f.link.value;
}
}


again, I've not tried it... but it should work!!

homerUK
03-18-2004, 02:06 PM
javascript:winEdit(document.f.link.value) might work....

mrmbarnes
03-18-2004, 02:16 PM
That didn't quite work but from that I have worked it out.

Thanks for your help and from this I have learnt quite a bit about this sort off thing.

Thanks once again!!!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum