简体   繁体   中英

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

I want to pass a query parameter from one page to another in PHP.
I am redirecting using form's action attribute, instead of hyperlink tag.

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

I am getting below error:

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";
     }
   }
}

Why am I not getting the values passed in query parameters?

Finally, I found my way out of this problem. Turns out that to access a value passed using form's action attribute, $_GET is to be used. And as for the other values, $_POST should be used. Something like this-

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

Result.php

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

You are not getting $_GET['question'] back. You are getting q1 back. So what you have made will have a url like:

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

So you have to set your Result.php at

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

If you want your url to have question insted you need to do it like this:

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

Keep in mind that this is a quick fix but do not use GET. Use POST insted. Also use FireBug to see how you get or post forms.

EDIT

It's better to use POST otherwise it will overwrite your url. Try this and let me know if it's what you want.

<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;
    }
}
?>

OR if you want to use Method GET you can pass it through a hiddin input: And you can use your own Result.php code.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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