Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Mar 2004
    Location
    London
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Dynamic Selection Box That Writes An Answer

    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

  • #2
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post
    the problem you have is that there is one function for both select fields which are both named the same....

    try this:

    Code:
    <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>

  • #3
    New to the CF scene
    Join Date
    Mar 2004
    Location
    London
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Great!!! Thanks for your help!

  • #4
    New to the CF scene
    Join Date
    Mar 2004
    Location
    London
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #5
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post
    you could write another function, something like

    Code:
    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

    Code:
    <input type="button" value="go to link" onclick="return doUrl()">
    Ive not tested this, but give it a try....

  • #6
    New to the CF scene
    Join Date
    Mar 2004
    Location
    London
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #7
    New to the CF scene
    Join Date
    Mar 2004
    Location
    London
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I would also like to have the link as a popup.

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

    Can this be done?

  • #8
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post
    you could set a variable in the function.. something like

    Code:
    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!!

  • #9
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post
    javascript:winEdit(document.f.link.value) might work....

  • #10
    New to the CF scene
    Join Date
    Mar 2004
    Location
    London
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!!!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •