基于下拉选项加载内容的PHP表单 [英] PHP form that loads content based on dropdown option

查看:68
本文介绍了基于下拉选项加载内容的PHP表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于客户项目,我已经在此处询问了有关此问题的问题.这些答案可能不是我想要的,所以我将尝试使这个问题尽可能详细.

I have already asked a question regarding this issue here, for a client project. Those answers may not be what I am looking for, so I will try to make this question as detailed as possible.

我有一个包含4个主要页面的表格.第一页要求提供个人信息.单击提交将带您进入第二页.第二页有一个包含项目编号的下拉列表.假设它们是A,B,C,D,E.

I have a form which has 4 main pages. The first page asks for personal information. Clicking on Submit takes you to a second page. Second page has a dropdown list containing item numbers. Let's say they are A, B, C, D, E.

当访客选择一个项目并单击提交时,我希望下一页显示基于上面选择的项目的内容:

When a visitor chooses an item and clicks submit, I want the next page to show content based on the item chosen above:

  • 如果访问者选择选项A,则该页面应加载html页面 a.html 或php页面 a.php .
  • 如果他选择选项B,则该页面应加载html页面 b.html 或php页面 b.php .对于其他项目,依此类推,每个项目都有不同的页面.
  • If visitor chooses option A, the page should load the html page a.html or php page a.php.
  • If he selects option B, the page should load the html page b.html or php page b.php. And so on for other items, each one having a different page.

我不太了解PHP.我正在WordPress网站上构建此表单.

I don't know PHP in detail. I am building this form in a website on WordPress.

推荐答案

您可以通过多种方式解决此问题,但在这种情况下,我建议使用两种方式:PHP或JS.

You can get around this problem in many ways, but in this case I would recommend two ways: PHP or JS.

您将需要

1)表格

<form action="loadPage.php" method="POST" name="theForm" id="theForm">
<select form="theForm" name="selectedPage">
  <option value="page_1">Page 1</option>
  <option value="page_2">Page 2</option>
  ...
</select>
<input type="submit" value="Load page" />
</form>

2)一个PHP处理程序

2) A PHP handler

//loadPage.php
<?php
$requested_page = $_POST['selectedPage'];

switch($requested_page) {
  case "page_1":
    header("Location: page_1.php");
  break;
  case "page_2":
    header("Location: page_2.php");
  break;
  default :
  echo "No page was selected";
  break;
}
?>

解决方案2,JavaScript

假设您要在不使用PHP的情况下显示不同的页面,那么该怎么选择呢? JavaScript.对我来说,这更易于实现,并且更加用户友好":

Solution 2, Javascript

Let's say that you'd like to display different pages without using PHP, then what to choose? JavaScript. This is, to me, more easy to implement and much more "user-friendly" :

任务:以相同的形式显示不同的页面

Task: Display different pages from the same form

<!DOCTYPE html>
<html>
<head>
<!-- Amazing stuff goes here :) -->
<script>
function load_page() {
    var selected_page = document.getElementById("selected_page").value;
    if (selected_page != "") {
        window.location.href = selected_page
        //Please note that the value recived,
        //in this case selected_page,
        //should be a valid url!
        //Therefore the value of the
        //<option> tag should be itself 
        //a url !
        //ex.: <option value="page.php"> is valid
        //<option value="page_1"> is not valid
    }
}
</script>
</head>
<body>
<form action="#">
    <select id="selected_page">
        <option value="page_1.php">Page 1</option>
        <option value="page_2.php">Page 2</option>
        ...
    </select>
    <button onclick="load_page()">Load it ! </button>
</form>
</body>
</html>

这篇关于基于下拉选项加载内容的PHP表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆