繁体   English   中英

如何在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

Result.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">

Result.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 insted。 还可以使用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>

Result.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,则可以通过隐藏输入将其传递:并且您可以使用自己的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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM