简体   繁体   English

PHP:通过选择中的URL更改表单的页面操作

[英]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. 我的目标是当我选择“ Vendita”并提交表格时,对于每个选择,我都必须进入选择“ Vendita”中的网址。 someone can help me? 有人可以帮助我吗? thanks 谢谢

In the server side php code, do something like this 在服务器端php代码中,执行以下操作

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. 注意:这是一个非常基本的版本,您将希望通过维护服务器上的URL列表或类似内容来确保URL有效。

有很多不同的方法,但是您可以例如使用以下方式:请参阅表单定义中的onsubmit部分


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

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. 您将需要将此代码包装在$(document).ready()以便在加载DOM之后注册该事件。

Like so: 像这样:

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

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/ 演示: http : //jsfiddle.net/6ybMP/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM