+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: Javascript and links

  1. #1
    mbengi.bongi83 is offline x10Hosting Member mbengi.bongi83 is an unknown quantity at this point
    Join Date
    Aug 2011
    Posts
    8

    Question Javascript and links

    Hi Guys,

    I wonder if you can tell me if the following is possible, I have run out of steam here.

    I'm assuming that this requires Javascript??

    My friend's website has a button which links to his "admin" page, he was wondering if by adding a checkbox (or 2) and by clicking them then pressing the button the user would be directed to another url.

    Any help would be appreciated.

  2. #2
    cdh47315 is offline x10Hosting Member cdh47315 is an unknown quantity at this point
    Join Date
    Mar 2011
    Posts
    7

    Re: Javascript and links

    No, it's called a "form."

  3. #3
    ellescuba27 is online now x10 Sophmore ellescuba27 is an unknown quantity at this point
    Join Date
    Sep 2011
    Posts
    151

    Re: Javascript and links

    No Javascript required. In a form (follow the guy above me's link) you can use the <form> tag, and set the following attributes:

    method - can equal "post" or "get"
    If the attribute method equals post, the information in the form, like whether those two checkboxes are checked or not, will be hidden from the user. If set to get, then the information in the form will be visible to the user in the address bar. So the code is:
    method="post" or method="get"

    action - where to go to after the user presses the button.
    Once the user presses a button, normally called a submit button, the button makes the user go to another page. Set action to the page you want to go to. So the code is:
    action="webpage.html"

    After the form tag and it's attributes, add the checkboxes, like you suggested, or other stuff. You can see an example at the guy above me's link. Now add a submit button. You can do so by creating an <input> tag, with the type attribute set to "submit". Remember also to set the value attribute (to whatever words you want on the button) too. Once the user presses this button, it will redirect the user to the page that was set with the action attribute on the form tag. If the form action was set to "get" you can just take what the user submitted out of the address bar, but if you set it to "post", only server side scripts can get it.

    Now add a </form> tag.

    Example code:
    <form method="get" action="webpage.html">
    (insert checkboxes, textboxes and radiobuttons here)
    <input type="submit" value="Done!"></input>
    </form>

    In this code, the <form> tag has method set to "get" meaning that the information, like weather some checkboxes were checked, will be in the address bar when the user presses a submit button. The user changes textboxes, checkboxes and stuff, and then, presses the submit button which says "Done!". When the user presses that, the user will be at "webpage.html" on your site and the information will appear in the address bar after the address in a code (because the method was set to "get" in the form tag). You can take that code in the address bar using Javascript and use it wherever you like, or if you want to get advanced, use PHP.

    Forms are great for many uses. I'm sure you will find some!

  4. #4
    callumacrae's Avatar
    callumacrae is offline not alex mac callumacrae is just really nice
    Join Date
    Dec 2007
    Location
    Wellesbourne, England
    Posts
    5,162

    Re: Javascript and links

    It can either be done server-side, using PHP, or client-side, using JavaScript. I would recommend the latter, but I'll explain both possibilities:

    The HTML:

    HTML Code:
    <form action="goto.php" method="get" id="gotoform">
    <input type="submit" value="Go" />
    Go to: <input type="checkbox" name="goto" value="admin" checked="checked" /> admin &nbsp; <input type="checkbox" name="goto" value="home" /> home
    If you chose to use PHP, this is what you would put in goto.php:

    PHP Code:
    <?php

    if ($_GET['goto'] === 'admin') {
        
    header('Location: admin.php');
    } else if (
    $_GET['goto'] === 'home') {
        
    header('Location: index.php');
    } else {
        echo 
    'Invalid input.';
    }
    Using JavaScript:

    Code:
    document.getElementById('gotoform').addEventListener('submit', function(e) {
        for (var goto = '', i = 0; i < this.goto.length; i++) {
            if (this.goto[i].checked) {
                goto = this.goto[i].value;
                break;
            }
        }
        
        if (goto === 'admin') {
            document.location = 'admin.php';
        } else if (goto === 'home') {
            document.location = 'index.php';
        } else {
            alert('Invalid input');
        }
        e.preventDefault();
    });
    I can customise your phpBB board. Send me a PM.
    lynxphp - info, tutorials and scripts
    "A forum post should be like a skirt; long enough to cover the subject but short enough to keep things interesting."

  5. #5
    mbengi.bongi83 is offline x10Hosting Member mbengi.bongi83 is an unknown quantity at this point
    Join Date
    Aug 2011
    Posts
    8

    Re: Javascript and links

    Thanks very much guys,

    I think the Javascript option is what is needed here, after having spoken to my friend.

    Just another couple of points; Where does the Javascript go? I assume it's within the <form> tags?

    Also, if no checkbox is checked, rather than an alert, I assume that

    Code:
            alert('Invalid input');
    Can be changed to:

    Code:
            document.location = 'anotherlocation.php';
    Or do further changes need to be made?

  6. #6
    essellar's Avatar
    essellar is offline Community Advocate essellar has a spectacular aura about
    Join Date
    Feb 2010
    Location
    Toronto, Ontario, CA
    Posts
    1,153

    Re: Javascript and links

    The JavaScript can go just about anywhere on the page as long as the DOM is ready when it's called. The current "best practice" is to put scripts like this at the bottom of the HTML page, before the closing body tag. And yes, you can change the else clause in exactly the way you described.
    “Beware of bugs in the above code; I have only proved it correct, not tried it.” --Donald Knuth
    "It was as if its architects were given a perfectly good hammer and gleefully replied, 'neat! With this hammer, we can build a tool that can pound in nails.'" -- Alex Papadimoulis (on TheDailyWTF.com)

  7. #7
    callumacrae's Avatar
    callumacrae is offline not alex mac callumacrae is just really nice
    Join Date
    Dec 2007
    Location
    Wellesbourne, England
    Posts
    5,162

    Re: Javascript and links

    The else statement will only be called if you have done something wrong, and should only really be used for debugging.
    I can customise your phpBB board. Send me a PM.
    lynxphp - info, tutorials and scripts
    "A forum post should be like a skirt; long enough to cover the subject but short enough to keep things interesting."

  8. #8
    mbengi.bongi83 is offline x10Hosting Member mbengi.bongi83 is an unknown quantity at this point
    Join Date
    Aug 2011
    Posts
    8

    Re: Javascript and links

    Quote Originally Posted by essellar View Post
    The JavaScript can go just about anywhere on the page as long as the DOM is ready when it's called. The current "best practice" is to put scripts like this at the bottom of the HTML page, before the closing body tag. And yes, you can change the else clause in exactly the way you described.
    That's great, I will let my friend know.

    Just one thing, I assume the HTML will be different as my friend isn't using PHP? Also for the button, he is using a custom image with hover/visited states in CSS so I assume that

    Code:
    <input type="submit" value="Go" />
    Can be replaced with something like:

    Code:
    <input type="image" name="submit" src="image.jpg" width="126" height="18">
    Sorry to be a pest

  9. #9
    lehiligan_xvi97 is offline x10Hosting Member lehiligan_xvi97 is an unknown quantity at this point
    Join Date
    Oct 2011
    Posts
    1

    Re: Javascript and links

    it really helps to me also... thanks :D

  10. #10
    essellar's Avatar
    essellar is offline Community Advocate essellar has a spectacular aura about
    Join Date
    Feb 2010
    Location
    Toronto, Ontario, CA
    Posts
    1,153

    Re: Javascript and links

    Anything that acts to submit the form (inputs of type submit and image, as well as buttons with type submit --which is the default) will work, since the code is attached to the onsubmit event of the form rather than to the input/button itself.
    “Beware of bugs in the above code; I have only proved it correct, not tried it.” --Donald Knuth
    "It was as if its architects were given a perfectly good hammer and gleefully replied, 'neat! With this hammer, we can build a tool that can pound in nails.'" -- Alex Papadimoulis (on TheDailyWTF.com)

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. Need help about javascript
    By lnakuma in forum Programming Help
    Replies: 12
    Last Post: 05-07-2011, 03:31 AM
  2. Javascript help
    By espfutbol98 in forum Programming Help
    Replies: 16
    Last Post: 06-24-2009, 01:45 AM
  3. Javascript Help
    By goldy30 in forum Programming Help
    Replies: 2
    Last Post: 11-30-2008, 04:24 PM
  4. javascript and external javascript files problem
    By delon in forum Programming Help
    Replies: 6
    Last Post: 04-27-2008, 12:41 AM
  5. JavaScript Ads
    By picasa in forum Programming Help
    Replies: 0
    Last Post: 03-22-2008, 05:16 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers