簡體   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