Results 1 to 2 of 2

Thread: Form Values

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2008
    Posts
    68

    Question Form Values

    Hello gang!

    I've been a member of VB Forums for many years, mostly in the VB.NET section, but I'm now really getting into the whole webdesign thing.

    I know this is for PHP, but I'm wondering if anyone can suggest another site for HTML, CSS, PHP help that is as warm, friendly and helpful as VB Forums.


    What I'm trying to do is get a value from a Select (List/Menu) and append it to a website link.
    Code:
    List/Menu has Cat, Dog, Bunny.
    
    If Cat is selected then my link button would be:  
    www.mysite.com/cat.html
    But I can't figure out how to get the value from the List/Menu.

    I'm great with VB.NET, not to shabby with CSS and HTML, but there's still a lot I need to learn.

    Any suggestions would be wonderful!!

  2. #2
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: Form Values

    Hiya.

    Without bringing Javascript into the picture, the form will need to be submitted before you can get its values. So you could have your selector, submit the form, and then populate your link with the appropriate value; little example:
    PHP Code:
    <?php
    if(isset($_POST['animal']) && !empty($_POST['animal'])){
      
    ?><a href="http://mysite.com/<?php echo $_POST['animal'];?>">Click here</a><?php
    }
    ?>

    <form method="POST" action="">
    <select name="animal">
    <option value="">Pick one</option>
    <option value="cat.html">Cat</option>
    <option value="dog.html">Dog</option>
    <option value="bunny.html">Bunny</option>
    </select>
    <input type="submit"/>
    </form>
    PHP stores form data in global arrays; either $_POST or $_GET depending on the "method" you use in your form. When you first land on the page, PHP checks for the presence of your form data, and that'll be false, so nothing gets output by the if-statement. The form will be output, and you can pick from the selector and submit the form (an empty action attribute on the form tag will have the form submit to the same page it's on). The page will reload, but the form data will be present now, and a link will be output from the if-statement (followed by the same form again).

    If you want to achieve this sort of thing without submitting the form (and performing a full page reload), you'll need to add some Javascript.

Posting Permissions

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



Click Here to Expand Forum to Full Width