简体   繁体   English

如何根据使用单个表单选择的单选按钮来修复代码以转到不同页面

[英]How to fix the code to go for different pages according to radio button selected using single form

I want to go to different templates(page) whatever is selected(radio button) from a single form. 我想转到一个单独的表单中选择的(单选按钮)不同的模板(页面)。 I want to include just one button in my form. 我只想在表单中包含一个按钮。

Here as if someone selects template1 I will to page template1.php. 在这里,好像有人选择template1我将转到template1.php页面。 If I select template2 I will go to template2.php. 如果选择template2我将转到template2.php。

I have saved the below code as form.php 我将以下代码另存为form.php

<form method="POST" action=
                          "<?php if(($rb = $_POST('template'))=='1'){?>
                                          template1.php 
                           <?php } 
                            if(($rb = $_POST('template'))=='2'){?>
                                          template2.php 
                            <?php } ?>"
                            enctype="multipart/form-data"> 
         <label>First Name: </label>
         <input type="text" name="firstname">
         <label>Last Name: </label>
         <input type="text" name="lastname">
         <input type="radio" value="1" name="template">Template 1
         <input type="radio" value="2" name="template">Template 2
         <button type="submit" name="upload">POST</button>      

  </form>

Use JavaScript for this instead. 为此,请使用JavaScript。 PHP is a server-side language, which means that you have to submit the form to the server before PHP knows which option you selected. PHP是一种服务器端语言,这意味着您必须在PHP知道选择哪个选项之前将表单提交给服务器。 JavaScript can deal with this right when you change it instead, as it is a client-side language. 当您更改此权限时,JavaScript可以处理此权限,因为它是一种客户端语言。

Create an event-listener for a click on the input with the name of template . 创建一个事件侦听器,以单击名称为template的输入。 Then toggle the action accordingly to that. 然后相应地切换操作。

 $("input[name=template]").on("click", function() { var action = ""; switch($(this).val()) { case "1": action = 'template1.php'; break; case "2": action = 'template2.php'; break; } $(this).parent("form").prop("action", action); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form method="POST" action="" enctype="multipart/form-data"> <label>First Name: </label> <input type="text" name="firstname"> <label>Last Name: </label> <input type="text" name="lastname"> <input type="radio" value="1" name="template">Template 1 <input type="radio" value="2" name="template">Template 2 <button type="submit" name="upload">POST</button> </form> 

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

相关问题 使用按钮单击重定向到所选单选按钮上的不同页面 - redirect to different pages on selected radio button using button click 如何使用jquery或php根据先前选择的响应保持单选按钮的选择? - How to keep radio button selected according to previously selected response using jquery or php? 如果未选择单选按钮,如何修复警报? - How to fix alert if no radio button is selected? 如何通过单击按钮将表单发布到两个不同的页面? - How to post form to two different pages with a single button click? 如何使用Reactive表单动态设置单选按钮文本? 并选择*单选按钮值和问题标题* onSubmit - How to set radio button text dynamically using Reactive form? and and selected *radio button value and question title * onSubmit 如何使用两个单选按钮不同的字段修复单选按钮默认值 - How to fix radio button default with two radio button different field 如果选择了特定的单选按钮,如何显示表单 - How to show a form if a particular radio button is selected 在不同的单选按钮上调用不同的URL表单JavaScript - Call Different URL Form JavaScript on Different Radio button selected 如何在不同的单选按钮上打开不同的表单 - how to open different form on different radio button 根据所选单选按钮验证文本框 - Validation of text box according to selected radio button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM