使用PHP从下拉选项中单击下一步后转到下一步

时间:2018-06-08 06:06:04

标签: php

我想制作一个GPA计算器PHP网页。我为这份工作选择了三页。在第1页,从下拉选项中选择学期,然后单击下一步并转到选定的学期。我只编写了第一个进入下一个但失败了。我是PHP的初学者。代码如下:

 <form action="PHP/semester.php" method = "POST">
      Select Semester :
      <select name = "semesterwise">
       <option value="">Select</option>
        <option value="1">1st Semester</option>
        <option value="2">2nd Semester</option>
        <option value="3">3rd Semester</option>
        <option value="4">4th Semester</option>
        <option value="5">5th Semester</option>
        <option value="6">6th Semester</option>
        <option value="7">7th Semester</option>
        <option value="8">8th Semester</option>
  </select>
<input type="submit" value="Next">
</form>`

php code
      <?php
  if(isset($_POST['submit'])) {
    if(isset($_POST['semesterwise']))
    {
        if(strcmp($_POST['semesterwise'],"1") == 0)
        {
            header("Location: 1st.php");
        }
        elseif(strcmp($_POST['semesterwise'],"2" == 0))
        {
            header("Location: 2nd.php");
        }
        -------------------------------------

    ?>

2 个答案:

答案 0 :(得分:0)

以下是一个如何进行此操作的简单示例。

第1页:

<form action="" method = "POST">
      Select Semester :
      <select name = "semesterwise">
       <option value="">Select</option>
        <option value="1">1st Semester</option>
        <option value="2">2nd Semester</option>
        <option value="3">3rd Semester</option>
        <option value="4">4th Semester</option>
        <option value="5">5th Semester</option>
        <option value="6">6th Semester</option>
        <option value="7">7th Semester</option>
        <option value="8">8th Semester</option>
  </select>
<input name="submit" type="submit" value="Next">
</form>

<?php

  if(isset($_POST)) {

    if(isset($_POST['semesterwise']) && $_POST['semesterwise']){
      //Set the id variable to the semester in the url.
      header('Location: semesterPage.php?id=' . $_POST['semesterwise']);
      exit();    

    }

  }

?>  

semesterPage.php将如下所示:

<?php

//Get the id variable from the url.
if(isset($_GET['id']) && $_GET['id']){

  $id = $_GET['id'];
  echo $id . ' is your semester that you selected on page 1.';

  //Do an sql query to get the semester info and display it here.

}

?>

答案 1 :(得分:0)

HTML页面index.html

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <form action="semester.php" method ="POST">
      Select Semester :
      <select name ="semesterwise">
       <option value="">Select</option>
        <option value="1">1st Semester</option>
        <option value="2">2nd Semester</option>
        <option value="3">3rd Semester</option>
        <option value="4">4th Semester</option>
        <option value="5">5th Semester</option>
        <option value="6">6th Semester</option>
        <option value="7">7th Semester</option>
        <option value="8">8th Semester</option>
  </select>
    <input type="submit" name="submit" value="Next">
</form>
</body>
</html>

在同一目录中创建一个semester.php的PHP文件,该文件包含以下代码:

<?php
    if(isset($_POST['submit'])) {
        if(isset($_POST['semesterwise']))
        {
            switch($_POST['semesterwise']){
                case 1:
                    header("Location: 1st.php");
                    break;
                case 2:
                    header("Location: 2st.php");
                    break;
            }
        }
    }

?>

我刚创建了两个页面,只需要根据需要创建8个或更多