Page 1 of 1

passing variables between PHP pages

Posted: Sat Apr 02, 2005 9:37 am
by sn202
Hi all,

Right I'm making an assessment and feedback system, which allows the instructor to create questions and the feedback for each answer given by the student. Now that works but what I'm having problems with is displaying the feedback for the students answer.

I'm using PHP version 4.3.2 with a MySQL backend. All data is stored in the MySQL database and pages created dynamically. Now the problem is that when the student selects the answer from "show_test.php", the page "show_feedback.php" is called, (code below) here the appropriate feedback is shown according to "testno" and "questionno" my problem is that I need to pass the "testno" and "questionno" from the previous page.

I am trying to do this using the PHP GET command and passing the variable in the URL. Now this would work fine if I knew what the values of "testno" and "questionno" were and could just write something like:

"action=show_feedback.php?testno=1&questionno=1"

However, as I do not know the values of "testno" and "questionno" I need to pass the variable instead of the data, so (in theory):

"action=show_feedback.php?testno=$testno&questionno=$questionno"

However, I know this won't work and was wondering if anyone could tell me the correct syntax or method to use here.

Regards,

Simon.

------------show_feedback.php------------------

Code: Select all

<?php 
$answerno     = $_POST['answer']; 
$question     = $_GET['questionno']; 
$testno        = $_GET['testno']; 
#connect to database 
$conn = @mysql_connect( "****", "****", "****" ) 
or die( "could not connect" ); 
#select the specified database 
$rs = @mysql_select_db ( "db_sn202", $conn ) 
or die( "could not select database" ); 
#create the sql query 
Switch($answerno) 
{ 
    Case '1' : 
    $sql="select feedback from question where testno = 'testno' and questionno = '$question'"; 
    $rs = mysql_query( $sql, $conn ) 
    or die( mysql_error() ); 
    #write data 
    while( $row = mysql_fetch_array( $rs ) ) 
    { 
        echo("" . $row["feedback"] ); 
    } 
    break; 

    Case '2' : 
    $sql="select feedback2 from question where testno = '$testno' and questionno = '$question'"; 
    $rs = mysql_query( $sql, $conn ) 
    or die( mysql_error() ); 
    #write data 
    while( $row = mysql_fetch_array( $rs ) ) 
    { 
        echo("" . $row["feedback2"] ); 
    } 
    break; 

    Case '3' : 
    $sql="select feedback3 from question where testno = '$testno' and questionno = '$question'"; 
    $rs = mysql_query( $sql, $conn ) 
    or die( mysql_error() ); 
    #write data 
    while( $row = mysql_fetch_array( $rs ) ) 
    { 
        echo("" . $row["feedback3"] ); 
    } 
    break; 

    Case '4' : 
    $sql="select feedback4 from question where testno = '$testno' and questionno = '$question'"; 
    $rs = mysql_query( $sql, $conn ) 
    or die( mysql_error() ); 
    #write data 
    while( $row = mysql_fetch_array( $rs ) ) 
    { 
        echo("" . $row["feedback4"] ); 
    } 
    break; 
} 
?>

Posted: Sat Apr 02, 2005 9:52 am
by John Cartwright
firstly

Code: Select all

Case '1' :     $sql="select feedback from question where testno = 'testno' and questionno = '$question'";
should be

Code: Select all

Case '1' :     $sql="select feedback from question where testno = '".$testno."' and questionno = '$question'";
You there missing the $
On a side note, you generally should always escape your variables so it is easier to read.

thirdly,

Code: Select all

$answerno = $_POST['answer']; 
$question = $_GET['questionno']; 
$testno   = $_GET['testno'];
Your url looks like http://www.domain.com?question=1&testno=4

and answer is submitted through a form, while the URL is maintained correct?

Secondly, you don't really need a switch here, you could do something like this

Code: Select all

//since there is no feedback 1, must change 1 to blank
$newno = ($answerno == 1 ? '' : $answerno);

$sql = "SELECT `feedback".$newno."` FROM `question` WHERE `testno` = '".$testno."' AND `questionno` = '$question'";

$result = mysql_query($sql) or die(mysql_error());

if (mysql_num_rows($result) > 0)
{
     while ($row = mysql_fetch_assoc($result))
     {
          echo $row['feedback'.$newno].'<br />';
     }
}
else
{
     echo 'No Feedback Found';
}

Posted: Sat Apr 02, 2005 9:55 am
by feyd
our php tags support labeling..

you can do "show_feedback.php?testno=$testno&questionno=$questionno". It can be done in various ways. Here are a few: Assume that $result is the results from a query for the questions and user's answers involved in $testNo
  • Code: Select all

    while($row = mysql_fetch_assoc($result))
    {
      echo 'Question:&lt;br /&gt;' . $row&#1111;'question_text'] . '&lt;br /&gt;';
      echo 'Your answer:&lt;br /&gt;' . $row&#1111;'answers_text'] . '&lt;br /&gt;';
      echo '&lt;form action=&quote;show_feedback.php?testno=' . $testNo . '&amp;amp;questionno=' . $row&#1111;'question_id'] . '&quote; method=&quote;get&quote;&gt;&lt;button value=&quote;View feedback&quote; /&gt;&lt;/form&gt;';
    }
  • Using the previous code, swapping line 5 to

    Code: Select all

    echo '&lt;a href=&quote;show_feedback.php?testno=' . $testNo . '&amp;amp;questionno=' . $row&#1111;'question_id'] . '&quote;&gt;Show Feedback&lt;/a&gt;';
  • etcetera...

Posted: Sat Apr 02, 2005 11:18 am
by sn202
Hi,

Firstly I've made the changes recommended by Phenom. And have tried both methods outlined by Feyd, however I am having problems with these. The first method of echoing the form and using a submit button doesn't pass the variables as I am getting this as the URL:

"show_feedback.php?Submit=Submit"

However the Link method passes the variables "testno" and "questionno" as I'm getting the url:

"show_feedback.php?testno=1&questionno=1"

However obviously isn't submitting the form and as a result isn't passing the "anwer" selected from the drop down box.

Kinda need something in between, ;) any ideas?

Simon.

Posted: Sat Apr 02, 2005 11:23 am
by feyd
post your code.

Posted: Sat Apr 02, 2005 6:45 pm
by sn202
Hi,

Sorted it, simply using hidden fields, thanks for the help.

Regards,

Simon.