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?

5
  • Going to need to see the form html as well Commented Dec 15, 2015 at 12:20
  • 1
    @Steve check the edit, I just formatted it properly so it would be displayed Commented Dec 15, 2015 at 12:23
  • @AlexAndrei Ah, good catch, i completely missed that in my earlier edit Commented Dec 15, 2015 at 12:24
  • 1
    @Code_maniac "not working" is not a descriptive problem - please describe exactly whats happening. Also, you need to edit your question to include the complete form html, including the submit button Commented Dec 15, 2015 at 12:26
  • @Steve I have edited the question. Commented Dec 15, 2015 at 13:10

2 Answers 2

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)
...
Sign up to request clarification or add additional context in comments.

Comments

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>

4 Comments

I have edited the question. Both the q1 and question parameters are required to be passed to the Result page.
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.
please try the last code. This will return the url like this ?question=1&q1=a&g=Go
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.