简体   繁体   English

如何处理表单中多个提交按钮的不同目标?

[英]how to handle different targets for multiple submit buttons in a form?

In index.php I have a form: 在index.php中,我有一种形式:

<form name="UserLocation" action="propForSale.php" method="GET">

Once the user types in the name of a location in the text box on the form, they click on the "For Sale" submit button: 用户在表单上的文本框中输入地点名称后,点击“待售”提交按钮:

<input type="submit" value="For Sale" />

This takes the user to propForSale.php page as specified in the action attribute. 这会将用户带到action属性中指定的propForSale.php页面。 But I now have another button on the form in index.php: 但是我现在在index.php中的表单上有另一个按钮:

<input type="submit" value="To Rent" />

I now want the user to be directed to "propToRent.php" when they click on the "To Rent" button. 现在,我希望用户在单击“租用”按钮时将其定向到“ propToRent.php”。

At the moment, it take the user to "propForSale.php" when they click on the "To Rent" button. 此刻,当用户单击“出租”按钮时,它将带至“ propForSale.php”。 This is probably because it is specified in the action attribute of the form to go to that particular page. 这可能是因为它是在表单的action属性中指定的,以转到该特定页面。 But how do I get it to go to "propToRent.php"? 但是我如何去“ propToRent.php”呢?

Thanks in advance 提前致谢

Add a name to the submit-buttons. 在提交按钮上添加一个名称。 Then you can check $_POST for the value and redirect the user. 然后,您可以检查$_POST的值并重定向用户。

Do it with a drop-down menu with 2 options. 使用带有2个选项的下拉菜单来执行此操作。 To-Rent or For Sale, then use the current page index.php and check for if 待出租或待售,然后使用当前页面index.php并检查是否

(isset $POST('location')) { your submit code } (isset $ POST('location')){您的提交代码}

You can have two buttons and use Javascript to change the action of form and submit it to different pages, but I think the drop-down menu is the simpler solution. 您可以有两个按钮,并使用Javascript更改表单的操作并将其提交到不同的页面,但是我认为下拉菜单是更简单的解决方案。 Anyway, to use two buttons you'll need something like: 无论如何,要使用两个按钮,您将需要:

<form id="userLocation" action="" method="get">
<p>
    <label for="field1">Field 1:</label>
    <input type="text" id="field1" name="field1" />
</p>
<p>
    <label for="field2">Field 2:</label>
    <input type="text" id="field2" name="field2" />
</p>    
<p>
    <input type="button" value="For Sale" onclick="submitForm('forSale.php')" />
    <input type="button" value="To Rent" onclick="submitForm('toRent.php')" />
</p>
</form>
<script type="text/javascript">
function submitForm(action) {
    form = document.getElementById('userLocation');

    form.action = action;
    form.submit();
}
</script>

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

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