0

Okay, I have this form and I want to submit a page in the value and a directory in the action so when the option is selected it opens the page thats in the directory.

<form method="post" action="pages/">
<ul class="pageitem">
    <li class="textbox"><span class="header">Select a specific year</span>
    </li>
    <li class="select"><select name="pages/">
        <option value="1"></option>
        <option value="2007.php">2007</option>
        <option value="2008.php">2008</option>
        <option value="2009.php">2009</option>
        <option value="2010.php">2010</option>
        <option value="2011.php">2011</option>
        <option value="2012.php">2012</option>
        </select><span class="arrow"></span> 
    </li>
    <li class="button">
        <input name="Submit input" type="submit" value="Submit input" />
    </li>
</ul>
</form>
Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
Intecpsp
  • 352
  • 2
  • 4
  • 12

3 Answers3

0

With jQuery:

$('select').change(function () {
    $(this).closest('form').attr('action', 'pages/' + $('option:selected', this).val());
});

with PHP (short version - note you should remove the trailing slash on name="pages/")

header('Location: /pages/' + $_POST['pages']);
Brian Cray
  • 1,277
  • 1
  • 7
  • 19
0
<?php
$valid_pages = array("2007","2008","2009","2010","2011","2012");
if(isset($_POST['pages']) && in_array($_POST['pages'], $valid_pages))
{
    header("Location: /pages/" . $_POST['pages'] . '.php', true, 303);
    exit;
}
?>

and

<form method="post" action="pages">
    <ul class="pageitem">
        <li class="textbox"><span class="header">Select a specific year</span>
        </li>
        <li class="select"><select name="pages">
            <option value="1"></option>
            <option>2007</option>
            <option>2008</option>
            <option>2009</option>
            <option>2010</option>
            <option>2011</option>
            <option>2012</option>
            </select><span class="arrow"></span> 
        </li>
        <li class="button">
            <input name="Submit input" type="submit" value="Submit input" />
        </li>
    </ul>
    </form>
sierrasdetandil
  • 421
  • 1
  • 9
  • 16
Shahrokhian
  • 1,100
  • 13
  • 28
0

GOT IT! And I don't need a submit button. I had a friend help me with it. Here is the form:

<script type="text/javascript" src="pages/select.js"></script>
<form method="post" action="pages">
<ul class="pageitem">
    <li class="textbox"><span class="header">Select a specific year</span>
    </li>
    <li class="select">
    <select id="select1" onchange="goToPage('select1')">
        <option selected="selected"></option>
        <option value="pages/2007.php">2007</option>
        <option value="pages/2008.php">2008</option>
        <option value="pages/2009.php">2009</option>
        <option value="pages/2010.php">2010</option>
        <option value="pages/2011.php">2011</option>
        <option value="pages/2012.php">2012</option>
    </select>
    <span class="arrow"></span> 
    </li>
</ul>
</form>

And here is the select.js:

function goToPage( id ) {

   var node = document.getElementById( id );

   if( node &&
     node.tagName == "SELECT" ) {

     window.location.href = node.options[node.selectedIndex].value;

   }


}
Intecpsp
  • 352
  • 2
  • 4
  • 12