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 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    How to go to the site when clicking the submit button in form?

    "Help!"

    Please help. I'm a newbie and i can't solve this problem.

    I need html, php or javascript codes in solving this. I want to go to the site when the submit form was clicked.
    Here's my code:

    Code:
    <form action="<?php echo home_url('/'); ?>" method="get">
                                                 
    <select>
    <option selected="selected">Choose Site</option>
    <option value="url1">Site 1</option>
    <option value="url2">Site 2</option>
    <option value="url3">Site 3</option>
    <option value="url4">Site 4</option>
    </select> 
                            
    <button type="submit"></button>
    
    </form>
    Any help will be much appreciated. Thanks!

  • #2
    Regular Coder
    Join Date
    Apr 2004
    Posts
    298
    Thanks
    0
    Thanked 23 Times in 23 Posts
    How many possible destination sites do you anticipate having?

  • #3
    New Coder
    Join Date
    Feb 2013
    Location
    Liverpool
    Posts
    70
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Can you just send it to your /index.php (or whatever your page is) and then have your variables appended onto the URL such as:

    Code:
    http://yoururl.com/index.php?var=somevalue&var2=anothervalue&var3=morevalue
    Would that not work?
    Working for Edge Three Sixty doing Drupal Development

  • #4
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,093
    Thanks
    23
    Thanked 594 Times in 593 Posts
    You need two scripts
    HTML script:
    Code:
    <form action="test.php" method="post">
    	<select name="TheForm" >
    		<option selected="selected">Choose Site</option>
    		<option value="http://www.codingforums.com">codingforums</option>
    		<option value="http://www.yahoo.com/">yahoo</option>
    		<option value="https://www.google.com">google</option>
    	</select>
    	<button type="submit">Submit</button>
    </form>
    And the PHP script it calls (test.php):
    PHP Code:
    <?php
    if($_POST["TheForm"]){
        
    $site $_POST["TheForm"];
        
    header("Location: ".$site);
    }
    ?>
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #5
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts
    @Litebearer,
    Thanks for your reply, I need to this in approximately 15 below sites.

  • #6
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts
    @sunfighter

    Thanks for your reply,

    Is it possible to post directly the url in the url tab of the browser without going to any files like what you've gave (test.php)?

    Thanks again,

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Not with PHP it isn't.
    You could probably use JS for that, but then I'd have to beg the question as to why you'd bother with using a form instead of choosing a hyperlink?
    You don't need two scripts. You can use a self posting one.
    I would use one script and expand it.
    PHP Code:
    <?php
    $aValidLinks 
    = array(
        
    'Site Display Name' => 'siteurl',
        
    'Site Display Name 2' => 'siteurl 2',
        
    //. . .
    );

    if (isset(
    $_GET['selSite']))
    {
        if (
    in_array($_GET['selSite'], $aValidLinks))
        {
            
    header('Location: ' $_GET['selSite']);
            exit();
        }
        else
        {
            
    printf("%s is an invalid selection." PHP_EOLhtmlspecialchars($_GET['selSite']));
        }
    }

    ?>
    <!-- . . . -->
    <form method="get" action="<?php echo $_SERVER['SCRIPT_NAME'];?>">
        <select name="selSite">
            <optgroup label="Select Site">
    <?php
    foreach ($aValidLinks AS $title => $url)
    {
        
    printf('            <option value="%s">%s</option>' PHP_EOL$url$title);
    }
    ?>
            </optgroup>
        </select>
        <input type="submit"/>
    </form>
    Or something like that.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #8
    Regular Coder
    Join Date
    Apr 2004
    Posts
    298
    Thanks
    0
    Thanked 23 Times in 23 Posts
    a javascript version of what you seek...
    http://blogs.msdn.com/b/lisawoll/arc...19/217427.aspx
    you can always use php to populate the list
    -------
    I concur with Fou as to simple links that you can populate with php

  • Users who have thanked litebearer for this post:

    TLWH05 (07-08-2013)

  • #9
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts
    @litebearer,

    Thanks for the link. Although there's a few revision in those codes but it helps me to solved my problem.

    Thanks 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
    •