如何在单选按钮选择上加载多个PHP页面
how to load multiple php page on radio button selection
我有1个html页面包含2个单选按钮和2个不同的php页面page1.php和page2.php,我想加载page1.php在选择第一个单选按钮和加载page2.php在选择第二个单选按钮
任何JS代码的例子或片段将帮助我很多。
下面是HTML代码
<td align="center" class="bgcolor_03">
<form>
<input type="radio" class="gender" name="group" id="group" value="Sec" />
Secondary Section
<input type="radio" class="gender" name="group" id="group" value="SSec" />
Seniour Secondary Section
<br />
<input type="submit" value="Done" />
</form>
</td>
提前感谢。
这是您的PHP解决方案。确保将HTML文件类型更改为PHP。解释:在这个例子中,我在顶部检查表单是否提交。如果是,我检查单选按钮是否被选中,并将用户重定向到您的页面。
<?php
if (isset($_POST['group'])) { // "group" is mutual name of radio buttons
switch ($_POST['group']) {
case 'Sec': // Value of first radio button
header('Location: page1.php');
break;
case 'SSec': // Value of second radio button
header('Location: page2.php');
break;
default:
break;
}
}
?>
<td align="center" class="bgcolor_03">
<form action="" method="POST" >
<input type="radio" class="gender" name="group" id="group" value="Sec" />
Secondary Section
<input type="radio" class="gender" name="group" id="group2" value="SSec" />
Seniour Secondary Section
<br />
<input type="submit" value="Done" />
</form>
</td>
在你的Html文件中声明2div:
<div id="page1"></div>
<div id="page2"></div>
使用JQuery,像这样做:
function displayPage1()
{
$.ajax({
url: 'page1.php',
type: 'GET',
success : function(data) {
$('div#page2').html("");
$('div#page1').html(data);
},
error : function(data) {
//Do something
}
});
}
function displayPage2()
{
$.ajax({
url: 'page2.php',
type: 'GET',
success : function(data) {
$('div#page1').html("");
$('div#page2').html(data);
},
error : function(data) {
//Do something
}
});
}
在单选按钮上添加click事件以调用正确的函数
这取决于你希望页面如何加载。
-
你想通过ajax请求将页面加载到当前包含单选按钮的页面
-
你想在选择单选按钮后点击提交按钮,然后让正确的页面自行加载。
既然你要求JS代码,我假设你要选择1
你的html看起来像这样
<input type="radio" id="launch-page1" class="page-launcher" />
<input type="radio" id="launch-page2" class="page-launcher" />
<div id="hold-page1"></div>
<div id="hold-page2"></div>
你的JS(请使用JQuery)将是这样的。
$(function(){
$(".page-launcher").click(function () {
if ( $(this).attr("id") === "launch-page1" ) {
window.location.replace(path_to_page1);
} else {
window.location.replace(path_to_page2);
}
}
});
干杯!
首先,如果为多个元素使用相同的ID,这不是一个好的做法。因此,我将表单,尤其是单选按钮更改为:
HTML<form>
<input type="radio" class="gender" name="group" id="group1" value="Sec" />
Secondary Section
<input type="radio" class="gender" name="group" id="group2" value="SSec" />
Seniour Secondary Section
<br />
<input type="submit" value="Done" />
</form>
我相信下面的jQuery可以给你想要的。
jQuery$('input[type=submit]').click(function () {
if($('#group1').prop('checked') == true){
document.location.href = 'url here';
}
if($('#group2').prop('checked') == true){
document.location.href = 'url here';
}
});
相关文章:
- 如何通过php页面将数据库值检索到jquery自动完成框中
- 通过php页面中的js强制下载txt
- 如何将字符串值从php页面发送到java脚本页面
- 倒计时计时器应该持续两个php页面
- 使用GoogleMapApi保存标记-重定向到.php页面
- php页面,该页面在清除值后接受输入
- 如何将JavaScript变量的数据传递到另一个php页面
- 从jqueryajax加载php页面卸载当前页面
- 如何摆脱"压缩/块”;PHP页面底部的注释
- 在AJAX调用中获取重定向的php页面名称
- 将文本值从html页面传递到php页面
- 将javascript变量传递到下一个php页面
- 将变量传递到php页面,作为Angular中的状态
- 将php页面URL中的变量传递给JavaScript和另一个php
- 如何使用javascript验证表单中的数据,然后调用php页面
- 将不属于表单的HTML页面中的值传递给php页面
- 将php页面的一个部分渲染为HTML和CSS后生成PDF,并考虑PDF分页符
- 应该如何将密码从纯html/javascript应用程序发送到php页面
- 在htm-php页面中打印Javascript函数
- 从 PHP 表中选择值并将其传递给另一个 PHP 页面