当前位置: 动力学知识库 > 问答 > 编程问答 >

jquery - PHP: change page action of a form by url in select

问题描述:

hy, i have a problem with a form. i know the question is simple but i can not have a solution. Well, this is my form:

<form id="search" method="post" action="cerca_redirect2.php" >

<select id="tipo" name="tipo"class="chzn-select" style="width:165px;" tabindex="1" >

<option value="http://case.vortigo.it/vendita-immobili/index.php"> Vendita</option>

<option value="http://case.vortigo.it/affitto-immobili/index.php">Affitto</option>

</select>

<input id="field" name="field" type="text" value=""/>

<input id="submit" type="submit" value="" />

</form>

my goal is when i select "Vendita" and i submit the form i have to go to the url in the select "Vendita", for each select. someone can help me? thanks

网友答案:

In the server side php code, do something like this

if (isset($_POST['tipo']) && !empty($_POST['tipo']))
{
   header('Location: ' . $_POST['tipo']);
}

Note: This is a very basic version, you will want to ensure the url is valid by either maintaining a list of urls on the server, or something similar.

网友答案:

There are many different ways, but you can for example use following: See the onsubmit part in the form definition


<form id="search" method="post" action="cerca_redirect2.php" onsubmit="this.action=document.getElementById('tipo')[document.getElementById('tipo').selectedIndex].value" >
网友答案:

If I understan your question correctly, you need a way to change the action value to the selected option's value

this is how to do that

$(document).ready(function(){
    $("#tipo").on("change", function(){
        $("#search").attr("action", $(this).val());
    });
});

DEMO: http://jsfiddle.net/6ybMP/

网友答案:

You want to change the action of the form based on the select? The following should be along the lines of what you want.

$('#tipo').on('change', function() {
    var newAction = this.val();
    $('#search').prop('action', newAction);
}

UPDATE

You will want to wrap this code in $(document).ready() so that the event will be registered after the DOM has been loaded.

Like so:

$(document).ready(function() {
    $('#tipo').on('change', function() {
        var newAction = this.val();
        $('#search').prop('action', newAction);
    }
});
分享给朋友:
您可能感兴趣的文章:
随机阅读: