0

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?

Code_maniac
  • 270
  • 1
  • 4
  • 16

2 Answers2

3

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)
...
Code_maniac
  • 270
  • 1
  • 4
  • 16
0

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>
Puya Sarmidani
  • 1,226
  • 9
  • 26
  • I have edited the question. Both the q1 and question parameters are required to be passed to the Result page. – Code_maniac Dec 15 '15 at 13:32
  • You have entirely changed the code in Result.php page. This is not what I intend to do. Please read the question again. Why am I unable to pass the parameter question in the URL via form( I don't want to use the hyperlink tag)? Thanks for trying. – Code_maniac Dec 15 '15 at 13:55
  • please try the last code. This will return the url like this ?question=1&q1=a&g=Go – Puya Sarmidani Dec 15 '15 at 13:57
  • If the method is "get" and the action is an HTTP URI, the user agent takes the value of action, appends a `?' to it, then appends the form data set, encoded using the "application/x-www-form-urlencoded" content type. Maybe one could percent-encode the action-URL to embed the question mark and the parameters, and then cross one's fingers to hope all browsers would leave that URL as it (and validate that the server understands it too). But I'd never rely on that. By the way: it's not different for non-hidden form fields. For POST the action URL could hold a query string though. – Puya Sarmidani Dec 15 '15 at 14:07