如何从表单传递查询参数';s在php中的action属性


How to pass query parameters from form's action attribute in php?

我想用PHP将查询参数从一个页面传递到另一个页面
我正在使用表单的action属性而不是超链接标记重定向。

<html>
  <head>
    <title>Question 1</title>.       
  </head>
   <body>
      <form method="GET" action="Result.php?question=1">
 <b>Question 1. What is the full form of PHP?<b> <br>
        A)<input type="radio" name="q1" value="a"> 
           Pre HyperText Processor<br>
        B)<input type="radio" name="q1" value="b"> 
           Post HylerText Processor<br>
       C)<input type="radio" name="q1" value="c"> 
           Personal HyperText Pages<br>
        D)<input type="radio" name="q1" value="d"> 
           HyperText Preprocessor<br>
        <input type="submit" name="g" value="Go">
   </form>
</body>

我得到以下错误:

Undefined index:question in Result.php on line 2

结果.php

<?php
$score=0;
$q=$_GET["question"];
if($q)
{
   if($_GET['g']!=null)
   {
   switch($q)
    {
       case 1: $answer1='c';
               if($_GET["q1"]==answer1)
                 ++$score;
               break;
       default: print " try again";
     }
   }
}

为什么我没有得到在查询参数中传递的值?

最后,我找到了解决这个问题的方法。事实证明,要访问使用表单的action属性传递的值,需要使用$_GET。至于其他值,应该使用$_POST。类似这样的东西-

<form method=POST" action="Result.php?question=1">

结果.php

$q=$_GET["question"];
...
 if($_POST['g']!=null)
 ... 
    if($_POST["q1"]==$answer)
...

您无法取回$_GET['question']。您正在取回q1。因此,你所做的将有一个类似的网址:

Result.php?q1=a&g=Go"

因此,您必须将Result.php设置为

$q=$_GET["q1"];
if($q)
{
switch($q)
{
   case 'a': print "question 1";
           break;
 }
}

如果你想让你的网址有问题,你需要这样做:

  <form method="GET" action="Result.php">
 <b>Question 1. What is the full form of PHP?<b> <br>
    A)<input type="radio" name="question1" value="a"> 
       Pre HyperText Processor<br>
    B)<input type="radio" name="question1" value="b"> 
       Post HylerText Processor<br>
   C)<input type="radio" name="question1" value="c"> 
       Personal HyperText Pages<br>
    D)<input type="radio" name="question1" value="d"> 
       HyperText Preprocessor<br>
    <input type="submit" name="g" value="Go">

请记住,这是一个快速修复方法,但不要使用GET。按照指示使用POST。还可以使用FireBug查看如何获取或发布表单。

编辑

最好使用POST,否则它会覆盖你的url。试试这个,如果这是你想要的,请告诉我。

<html>
<head>
<title>Question 1</title>.
</head>
<body>
<form method="POST" name="form" action="Result.php?question=1">
<b>Question 1. What is the full form of PHP?<b> <br>
        A)<input type="radio" name="q1" value="a">
        Pre HyperText Processor<br>
        B)<input type="radio" name="q1" value="b">
        Post HylerText Processor<br>
        C)<input type="radio" name="q1" value="c">
        Personal HyperText Pages<br>
        D)<input type="radio" name="q1" value="d">
        HyperText Preprocessor<br>
        <input type="submit" name="g" value="Go">
</form>
</body>

结果.php

<?php
$q=$_GET["question"];
if($q)
{
$PostedValue = $_POST['q1'];
switch($PostedValue)
{
    case 'a': echo "Pre HyperText Processor";
        break;
    case 'b': echo "Post HylerText Processor";
        break;
    case 'c': echo "Personal HyperText Pages";
        break;
    case 'd': echo "HyperText Preprocessor";
        break;
    }
}
?>

或者,如果你想使用方法GET,你可以通过hiddin输入来传递它:您还可以使用自己的Result.php代码。

<html>
<head>
<title>Question 1</title>.
</head>
<body>
<form method="GET" name="form" action="Result.php">
    <input type="hidden" name="question" value="1">
<b>Question 1. What is the full form of PHP?<b> <br>
    A)<input type="radio" name="q1" value="a">
    Pre HyperText Processor<br>
    B)<input type="radio" name="q1" value="b">
    Post HylerText Processor<br>
    C)<input type="radio" name="q1" value="c">
    Personal HyperText Pages<br>
    D)<input type="radio" name="q1" value="d">
    HyperText Preprocessor<br>
    <input type="submit" name="g" value="Go">
</form>
</body>