使用PHP显示用户输入


Use PHP to display user inputs

我想做的是使用PHP在一个单独的页面上显示用户输入,但当我处理表单时,没有显示任何输入值。我不得不使用javascript进行输入验证,这很好,但PHP页面不起作用。这是我的代码:HTML和Javascript

survey.hp:

  <!DOCTYPE html>
  <html>
  <head>
    <title>Survey Page</title>
    <link rel="stylesheet" type="text/css" href="mystylepage.css" />
    <script type="text/javascript">
    function validate()
    {
        if (document.information.name.value == "" )
        {
            alert("Please provide your name!" );
            document.information.value.focus();
            return false;
        }
        if (document.information.email.value == "")
        {
            alert("Please provide an email!");
            document.information.value.focus();
            return false;
        }
        if (document.information.major.value == false)
            {
                alert("Please enter your major");
                document.information.value.focus();
                return false;
            }
        return true;
    }
    </script>
  </head>
  <div id="bodycolor">
  <body>
  <div id="container">
  <div id="menu">Menu: 
  <a class="menu" href="index.htm">Home</a> | <a class="menu"                                          
   `href="faculty.htm">Faculty</a> | 
   <a class="menu" href="courses.htm">
  Courses</a> | <a class="gradecalculator" href="gradecalculator.htm">Grade      
  Calculator</a> | <a class="survey" href="survey.htm">Survey</a>         
  </div>
  </div>
  <div id="header">
  <h1><center>CSE Center for Health Information Technology</center></h1>
  <br>
  <div id="links">
  <center><a href="https://www.kennesaw.edu/"><img src="KSU Logo.jpg"      
   alt="Logo" style="width:100px;height:100px;" /></a></center>
  <br>
   <center><a href="http://ccse.kennesaw.edu/">College of Computing and   
   Software Engineering</a></center>
  </div>
  </div>
  <h1>Please enter you information below</h1>
  <form name="information" method="POST" action="action.php"    
  onsubmit="return     
   validate();">
    <table width="500" border="0">
    <tr>
    <td width="150" bgcolor="#99CCFF"><strong>Name</strong></td>
    <td><input type="text" name="name" size="20" maxlength="20" /></td>
    </tr>
    <tr>
    <td bgcolor="#99CCFF"><strong>Email</strong></td>
  <td><input type="text" name="email" size="20" maxlength="20" /></td>
  </table>
  <p> Major:
    <input name="major" type="radio" value="Information Technology" />
    Information Technology
    <input name="major" type="radio" value="Software Engineering" />
    Software Engineering
    <input name="major" type="radio" value="Computer Science" />
    Computer Science</p>
  <p>
  <input type="submit" value="Process" />
  </p>
   <p id="msg"></p>
  </form>
 </body>
</html>

这是PHP页面:

action.php

 <!DOCTYPE html>
<html>
<head>
    <title>Survey Information Processing</title>
</head>
<body>
<h1>Form data</h1>
<p>Name: <?php echo $_POST["name"]?></p>
<p>Email: <?php echo $_POST["email"]?</p>
<p>Major: <?php echo $_POST["major"]?</p>

</body>
</html>

我是PHP的新手,所以非常感谢您的帮助!

您的表单方法应该是POST。

 <form name="information" method="POST" action="action.php" onsubmit="return validate();">

并在提交按钮中添加一个名称:

<input type="submit" value="Process" name="submit" />

此外,如果条件为:,则可以添加此项

<?php 
  echo "<pre>";
  print_r($_POST);   // Check if you're getting any POST data
  echo "</pre>";
?>
<?php if (isset($_POST['name'])) { ?>
 <h1>Form data</h1>
 <p>Name: <?php echo $_POST["name"]; ?> 
 </p>
 <p>Email: <?php echo $_POST["email"]; ?> 
 </p>
 <p>Major: <?php echo $_POST["major"]; ?> 
 </p>
<?php } ?>

希望这能有所帮助。

所以问题是你的方法是get,但你试图通过后访问

更改

method="GET" 

method="POST"

在您的表单上。