1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Form action="" changed based on value from dropdown inside said form

Discussion in 'JavaScript' started by PoPSiCLe, Sep 16, 2009.

  1. #1
    I have a small form, with a single dropdown - it selects a record from a database, and on submit the action is just "#" (or rather action="index.php") - I want to be able to add a ?hn=value_from_select before the action is submitted, so the actual url of the page changes to the direct link.

    I understand that for this I need ajax, but I'm a bit at a loss as to how I would do this.
     
    PoPSiCLe, Sep 16, 2009 IP
  2. ASTURIAS

    ASTURIAS Member

    Messages:
    239
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    33
    #2
    I need more details, but as far as I understood, you are trying to submit a form to a specific option/location selected on the dropdown menu?, please send me more details via PM, and I will be able to help...
     
    ASTURIAS, Sep 17, 2009 IP
  3. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #3
    Well. I have a form, with the following code:

    
    <form method="post" action="<?php echo $domain.$page_path; ?>index.php">
    HTML:
    Inside that form I have a select dropdown, which pulls information into <option>s from the database, like this:

    
    <option value="<?php echo $row['name']; ?>"><?php echo $human_name; ?></option>
    
    HTML:
    What I want, is for the value of the option selected (the $row['name']) to be appended to the action-part of the form - hence creating something like this:

    
    <form method="post" action="<?php echo $domain.$page_path; ?>index.php<?php echo $row['name']; ?>">
    
    HTML:
    The above doesn't work, of course, since it has the following problems: the first time a user loads the page, it will be empty - the second time a user uses the drop-down, that value will be populated by the FORMER choice - not the current one. Hence the url will be wrong.

    Does it make more sense now?
     
    PoPSiCLe, Sep 17, 2009 IP
  4. Mastermaniac

    Mastermaniac Peon

    Messages:
    128
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Here is the Code!

    <form name="f">
    </form>
    <select onchange="javascript:document.f.action=this.options[selectedIndex].value">
    <option value="index.php">Index</option>
    <option value="game.php">Games</option>
    <option value="menu.php">Menu</option>
    <option value="forum.php">Forum</option>
    </select>
    
    Code (markup):
     
    Mastermaniac, Sep 18, 2009 IP
  5. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #5
    Thanks. That works, almost. Problem is that I need to add to the action already defined - not replace it entirely. Hence the
    <select onchange="javascript:document.f.action=this.options[selectedIndex].value">
    HTML:
    needs to be something like this:

    <select onchange="javascript:document.f.action=<?php echo $domain.$page_path; ?>index.php?hn=this.options[selectedIndex].value"> <- which of course doesn't work - so how would I do this with the added php-variables? or just add the selected value to the already existing action.

    Nevermind, I figured it out!

    onchange="javascript:document.f.action=action + '?hn=' + this.options[selectedIndex].value" <- works just fine.
     
    Last edited: Sep 18, 2009
    PoPSiCLe, Sep 18, 2009 IP
  6. Mastermaniac

    Mastermaniac Peon

    Messages:
    128
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Your Welcome Guys! :)
     
    Mastermaniac, Sep 18, 2009 IP