3

我有三个 php 脚本。main.php questions.php 和 values.php

这是代码

主文件

<html>
    <head>
        <title></title>
    </head>

    <body>
        <h1>Be Prepare for the battle</h1>
        <?php
            $strTitle = "Begin";
            $strLink = "<a href = 'question.php?ques_id=1'>" . $strTitle ."</a>";     
            echo $strLink;
        ?>
    </body>
</html>

问题.php

<?php
require_once('../connect.php');
$quesSQL = mysql_query("SELECT * FROM  `questions` WHERE  `ques_id`=". $_GET["ques_id"]);

if(!mysql_num_rows($quesSQL) >= 1)
{
    die('Complete.');
}

$next = $_GET["ques_id"];

while($row = mysql_fetch_array($quesSQL)) {
    $id = $row['ques_id'];
    $strTitle = $row['ques_title'];
    echo "<li>" . $strTitle . "</li><br/>";         
}   

$optSQL = mysql_query("SELECT `options`,`values` FROM questions_options WHERE " . $id . "= ques_id");
echo "<form action=\"values.php\" method=\"POST\">";

while($row = mysql_fetch_array($optSQL) ) {
    $strOptions = $row['options'];  
    $strValues = $row['values'];                            
    echo "<input type =\"radio\" name =\"valueIn\" value=" . $strValues . " />" . $strOptions . "<br/>";
}   
echo "</form>";
$strTitle = "<input type =\"submit\" value=\"Next\">";
$next = $next + 1;
$strLink = "<a href = 'values.php?ques_id=" . $next . "'>" . $strTitle ."</a>";
echo $strLink;

mysql_close();                  
?>

值.php

<?php
require_once('../connect.php'); 
$input = $_POST['valueIn'];
$ansSQL = mysql_query("SELECT `answer` FROM questions WHERE 1-".$_GET["ques_id"]."= ques_id");          

$marks = 0;         
if($input == $ansSQL)
{
    $marks = $marks+1;
}
else
{
    $marks = $marks+0;
}               
echo $marks;                            
?>

现在的问题是我必须将一个值从第二个脚本(questions.php)传递给第三个脚本(values.php)。它来自单选按钮名称值“ valueIn ”中的 <form> 部分。但我不能那样做。因为我在第二个脚本的末尾发送了另一个带有 $strLink 变量的值 ques_id。那么我该怎么做呢?

4

4 回答 4

1

我不确定您为什么使用链接来处理表单中可能包含的内容。正如 anusha 所说,您应该像这样为 ques_id 使用隐藏的输入字段

问题.php

<?php
    require_once('../connect.php');
    $quesSQL = mysql_query("SELECT * FROM  `questions` WHERE  `ques_id`=". $_GET["ques_id"]);

    if(!mysql_num_rows($quesSQL) >= 1)
    {
    die('Complete.');
}

$next = $_GET["ques_id"];

while($row = mysql_fetch_array($quesSQL)) {
    $id = $row['ques_id'];
    $strTitle = $row['ques_title'];
    echo "<li>" . $strTitle . "</li><br/>";         
}   

$optSQL = mysql_query("SELECT `options`,`values` FROM questions_options WHERE " . $id . "= ques_id");
echo "<form action=\"values.php\" method=\"POST\">";

while($row = mysql_fetch_array($optSQL) ) {
    $strOptions = $row['options'];  
    $strValues = $row['values'];                            
    echo "<input type =\"radio\" name =\"valueIn\" value=" . $strValues . " />" . $strOptions . "<br/>";
}

$next = $next + 1;
$strLink = '<input type="hidden" name="ques_id" value="'.$next.'">';
echo $strLink;

    $strTitle = "<input type =\"submit\" value=\"Next\">";
    echo $strTitle;   
echo "</form>";

mysql_close();                  
?>

然后这两个变量都可以通过 $_POST 在下一步中使用,如下所示

$input = $_POST['valueIn'];
$ques_id = $_POST['ques_id'];
于 2012-12-19T05:55:44.797 回答
1

您可以使用隐藏输入,例如 Mike 的答案,或者您仍然可以使用 GET 参数,如下所示:

问题.php

<?php

// .........
// .........
// .........
// .........
// add / change your code for this part
$next = (int) $next;
$optSQL = mysql_query("SELECT `options`,`values` FROM questions_options WHERE ques_id = " . $next);
echo '<form action="values.php?ques_id=' . ($next+1) . '" method="POST">';
while($row = mysql_fetch_array($optSQL) ) {
    $strOptions = $row['options'];
    $strValues = $row['values'];
    echo '<input type="radio" name ="valueIn" value="' . $strValues . '" />' . $strOptions . '<br/>';
}
echo '<input type="submit" value="Next">';
echo "</form>";
mysql_close();

// end change
?>

值.php

<?php
// add / change your code for this part
$_GET["ques_id"] = (int) $_GET["ques_id"];
$ansSQL = mysql_query("SELECT `answer` FROM questions WHERE ques_id = " . ($_GET["ques_id"]-1));
// end change
// .........
// .........
// .........
// .........
于 2012-12-19T06:07:36.213 回答
0

您可以使用“a”标签添加多个参数。像

 "<a href = 'values.php?ques_id=".$next." & ques_id1=1'>" . $strTitle ."</a>"
于 2012-12-19T05:09:05.370 回答
-1

您还可以对变量使用隐藏的输入字段$question_id并提交表单

于 2012-12-19T05:17:23.153 回答