[英]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.